我试图通过其网址播放视频;即视频流。
当URL在本地时,以下代码在捆绑中起作用:
class EditShowVideoViewController:AVPlayerViewController {
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification",
name:AVPlayerItemDidPlayToEndTimeNotification,
object: nil)
if let url = NSURL(string: gEditMediumTuple!.medium as! String) {
player = AVPlayer(URL: url)
player!.play()
} else {
showAlert(sender: self.parentViewController!, withTitle: "No Video", withMessage: "No video is found.", alertPurpose: .noVideo)
}
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.AllButUpsideDown
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func handleNotification() {
let controller = self.parentViewController as! EditContainerViewController
controller.rollback()
}
}
但它不适用于远程URL。所以我尝试了以下方法:
let playerItem = AVPlayerItem(URL:url)
player = AVPlayer(playerItem:playerItem)
player!.play()
我假设我必须在播放之前等待.mpg加载。
什么是首选方式?
答案 0 :(得分:0)
这对我有用
let playerVC = AVPlayerViewController()
let url = NSURL(string: "http://urltofile")!
let player = AVPlayer(URL: url)
playerVC.player = player
self.addChildViewController(playerVC)
self.view.addSubview(playerVC.view)
playerVC.view.frame = self.view.frame
player.play()
小心:如果您使用的是iOS 9,ATS可能会阻止您的http呼叫