检查有效 url 以在 avplayercontroller 中播放?
check valid url to play in avplayercontroller?
我有三个 url,必须检查这些 url 是否有视频 link 可以播放。如果 url 中不包含视频,我必须退出 link 并播放下一个 url 中的视频。以下代码将有助于跟踪播放状态。
let playerAV = AVPlayerViewController()
var player = AVPlayer()
playerAV.player = player
playerAV.view.frame = CGRectMake(0, 0, self.videoView.frame.width, self.videoView.frame.height)
self.addChildViewController(playerAV)
self.videoView.addSubview(playerAV.view)
playerAV.didMoveToParentViewController(self)
playerAV.player?.play()
addObserverOfMoviePlayer()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.notificationObserver(_:)), name:AVPlayerItemDidPlayToEndTimeNotification , object: player.currentItem)
_ = UIDevice.beginGeneratingDeviceOrientationNotifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.deviceOrientationDidChange(_:)) , name:
UIDeviceOrientationDidChangeNotification, object: nil)
playerAV.player!.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
playerAV.player!.currentItem!.addObserver(self, forKeyPath: "playbackBufferEmpty", options: NSKeyValueObservingOptions.New, context: nil)
playerAV.player!.currentItem!.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: NSKeyValueObservingOptions.New, context: nil)
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "rate" {
if let rate = change?[NSKeyValueChangeNewKey] as? Float {
if playerAV.player!.currentItem!.status == AVPlayerItemStatus.ReadyToPlay{
if rate != 0 && playerAV.player!.error == nil {
print("normal playback")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ChannelDetailViewController.somAction), userInfo: nil, repeats: true)
}
else{
timer?.invalidate()
print("movie player stopped")
}
}else if playerAV.player!.currentItem?.status == AVPlayerItemStatus.Unknown{
timer?.invalidate()
print("not ready to play")
}
}
}
if keyPath == "playbackBufferEmpty"{
if playerAV.player?.currentItem?.playbackBufferEmpty == true{
timer?.invalidate()
print("movie player stopped due to no buffer")
}
}
if keyPath == "playbackLikelyToKeepUp" {
if playerAV.player?.currentItem?.playbackLikelyToKeepUp == true{
print("movie player playing after enough data in buffer")
playerAV.player?.play()
}
}
}
只需检查视频的长度,如果它无效,长度将 return 为零,使用此代码:
let assets = AVAsset(URL: NSURL(fileURLWithPath: filePath))
let length=Float(assets.duration.value)/Float(assets.duration.timescale)
if length != 0.0 {
// play it
}
如果还有问题可以随时问我。
我有三个 url,必须检查这些 url 是否有视频 link 可以播放。如果 url 中不包含视频,我必须退出 link 并播放下一个 url 中的视频。以下代码将有助于跟踪播放状态。
let playerAV = AVPlayerViewController()
var player = AVPlayer()
playerAV.player = player
playerAV.view.frame = CGRectMake(0, 0, self.videoView.frame.width, self.videoView.frame.height)
self.addChildViewController(playerAV)
self.videoView.addSubview(playerAV.view)
playerAV.didMoveToParentViewController(self)
playerAV.player?.play()
addObserverOfMoviePlayer()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.notificationObserver(_:)), name:AVPlayerItemDidPlayToEndTimeNotification , object: player.currentItem)
_ = UIDevice.beginGeneratingDeviceOrientationNotifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.deviceOrientationDidChange(_:)) , name:
UIDeviceOrientationDidChangeNotification, object: nil)
playerAV.player!.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
playerAV.player!.currentItem!.addObserver(self, forKeyPath: "playbackBufferEmpty", options: NSKeyValueObservingOptions.New, context: nil)
playerAV.player!.currentItem!.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: NSKeyValueObservingOptions.New, context: nil)
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "rate" {
if let rate = change?[NSKeyValueChangeNewKey] as? Float {
if playerAV.player!.currentItem!.status == AVPlayerItemStatus.ReadyToPlay{
if rate != 0 && playerAV.player!.error == nil {
print("normal playback")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ChannelDetailViewController.somAction), userInfo: nil, repeats: true)
}
else{
timer?.invalidate()
print("movie player stopped")
}
}else if playerAV.player!.currentItem?.status == AVPlayerItemStatus.Unknown{
timer?.invalidate()
print("not ready to play")
}
}
}
if keyPath == "playbackBufferEmpty"{
if playerAV.player?.currentItem?.playbackBufferEmpty == true{
timer?.invalidate()
print("movie player stopped due to no buffer")
}
}
if keyPath == "playbackLikelyToKeepUp" {
if playerAV.player?.currentItem?.playbackLikelyToKeepUp == true{
print("movie player playing after enough data in buffer")
playerAV.player?.play()
}
}
}
只需检查视频的长度,如果它无效,长度将 return 为零,使用此代码:
let assets = AVAsset(URL: NSURL(fileURLWithPath: filePath))
let length=Float(assets.duration.value)/Float(assets.duration.timescale)
if length != 0.0 {
// play it
}
如果还有问题可以随时问我。