me
me copied to clipboard
学习MacOS App (Part 5: AVFoundation and system arch)
MacOS和iOS的音视频框架主体都是围绕AVFoundation这个Framework,AVFoundation包括6个部分:
- Assets
- Playback
- Capture
- Editing
- Audio
- Speech
source: About Media Playback
跟随Learning AV Foundation来看第一个hello world,直接新建一个swift playground
import AVFoundation
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "你好, 朋友。Nice to meet you.")
utterance.voice = AVSpeechSynthesisVoice(language: "zh-CN")
synthesizer.speak(utterance)
运行以后会很流畅说出中英文,换成稍微复杂的一些长句还是不太行,差不多就这样吧。
Video Player
官方教程: Creating a Basic Video Player (macOS) | Apple Developer Documentation
if let url = URL(string: "https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8") {
playerView.player = AVPlayer(url: url)
}

注:
- 如果是播放http的url,则需要关闭ATS,同时添加NSDomainException, 参考swift - AVPlayer won't let me access URL - Stack Overflow
- 本地文件:
let url = URL(string: "file:///Users/nonocast/Movies/Teams.mp4")
,但一定要注意file access的权限
Thumbnail
通过AVAsset我们来指向这个mp4,然后再来获取截图,
f let url = URL(string: "file:///Users/nonocast/Movies/Teams.mp4") {
let asset = AVURLAsset(url: url)
print("duration: \(String(format:"%.1f", CMTimeGetSeconds(asset.duration)))s")
let assetSize = asset.tracks(withMediaType: .video)[0].naturalSize
print("resolution: \(assetSize)")
// thumbnail
let imageGen = AVAssetImageGenerator(asset: asset)
imageGen.appliesPreferredTrackTransform = true
do {
let cgImage = try imageGen.copyCGImage(at: CMTimeMake(value: 20, timescale: 1), actualTime: nil)
let image = NSImage(cgImage: cgImage, size: NSSize(width: cgImage.width, height: cgImage.height))
imageView.image = image
} catch {
print(error.localizedDescription)
}
}
具体参考: How to get thumbnail image from video URL in background swift
CALayer

如果你需要做个圆角裁切,这时你就需要用到CALayer
myView.wantsLayer = true
// myView.layer?.backgroundColor = NSColor.red.cgColor
myView.layer?.backgroundColor = NSColor(red: 1, green: 0, blue: 0, alpha: 0.3).cgColor
myView.layer?.cornerRadius = 15
然后你就会发现UIView只是一个抽象,绘图真正是通过CALayer,即Core Animation,通过Core Graphics,最终由Quartz2D进行绘制。这里就需要简单了解一下整个显示架构:


source: iOS son of Darwin? or is it iOS son of UNIX? · GitBook
随着iOS和MacOS不断的进化,大体抽象架构非常类似:
- Cocoa / Cocoa Touch (GUI)
- Media
- Core OS
- Core Service

所以很多时候都需要通过prefix来定位所在的Level:
- UI: UIKit
- NS: NextStep, means Cocoa
- CA: Core Animation
- CG: Core Graphics
- CF: Core Foundation
- AV: AV Foundation