我正在尝试实现一个从网址加载和播放视频的AVPlayer。它位于由按钮调用的函数中。这是我目前的代码:
import UIKit
import AVKit
import AVFoundation
class MovieDetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func playTrailer(_ sender: Any) {
guard let videoURL = URL(string: "http://caribbeancinemas.com/img/trailers/6134.mp4") else {
return
}
let player = AVPlayer(url: videoURL)
let controller = AVPlayerViewController()
controller.player = player
present(controller, animated: true) {
player.play()
}
}
}
当我在模拟器上运行应用程序时视频实际播放但有两个问题发生:
我在控制台中收到以下错误消息:
2018-03-31 16:49:53.389148-0400 MoviesApp [18928:3137301] [MediaRemote] [AVOutputContext]警告:AVF上下文不可用于sharedAudioPresentationContext 2018-03-31 16:49:53.497987-0400 MoviesApp [18928:3137301] [framework] CUICatalog:提供的资产名称无效:'(null)' 2018-03-31 16:49:53.498937-0400 MoviesApp [18928:3137301] [framework] CUICatalog:提供的资产名称无效:'(null)' 2018-03-31 16:49:53.857871-0400 MoviesApp [18928:3138058]任务< 223C4724-B3B5-4EBE-9AE0-5BA3C30B37C6>。< 2>完成错误 - 代码:-999 2018-03-31 16:49:55.365686-0400 MoviesApp [18928:3138196]任务< 95E91605-9B52-4212-97B3-47E73CE21B8B>。< 1>完成错误 - 代码:-999
视频播放器不会立即开始播放视频(缓冲),但它似乎要等到播放前下载整个视频。
我已经在这里寻找解决方案,但还没找到,所以任何帮助都表示赞赏。
提前致谢!
答案 0 :(得分:0)
@IBAction func playTrailer(_ sender: Any) {
guard let videoURL = URL(string: "http://caribbeancinemas.com/img/trailers/6134.mp4") else {
return
}
let videoAsset = AVURLAsset(url: videoURL! as URL)
let item = AVPlayerItem(asset: videoAsset)
let player = AVPlayer(playerItem: item)
let controller = AVPlayerViewController()
controller.player = player
present(controller, animated: true) {
player.play()
}
}