你如何播放流媒体视频AVPlayer
?
import Cocoa
import AVKit
class StreamViewController: NSViewController {
var videoPlayer: AVPlayer!
@IBOutlet weak var moviePlayerView: AVPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear() {
super.viewWillAppear()
}
override func viewDidAppear() {
super.viewDidAppear()
openStream()
}
func openStream() {
completeOpenStream { (done) -> (Void) in
if done {
print("done!")
}
}
}
func completeOpenStream(completionHandler: @escaping (Bool) -> (Void)) -> Void {
DispatchQueue.global().async() {
let urlString = "http://samples.mplayerhq.hu/V-codecs/h264/interlaced_crop.mp4" // You can open it with QuickTime.
let url = URL(fileURLWithPath: urlString)
let avAsset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: avAsset)
self.videoPlayer = AVPlayer(playerItem: playerItem)
self.moviePlayerView.player = self.videoPlayer
completionHandler(true)
}
}
}
如果我运行它,应用程序不会崩溃,但我会得到一个断开的链接,如下所示。
我在这里阅读了几十个主题。其中一个说您必须使用后台线程运行它。另一种说法是 URL 必须以 mp4 之类的文件类型结尾。一个人说你不能一票否决。顺便说一句,我为 macOS 和 iOS 编写代码。因此,我将主题留给两个平台。谢谢。