gpt4 book ai didi

ios - 检查在 avplayercontroller 中播放的有效 url?

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:49 26 4
gpt4 key购买 nike

我有三个网址,必须检查这些网址是否有可播放的视频链接。如果 url 中不包含视频,我必须退出链接并从下一个 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()

}
}
}

最佳答案

只需检查视频的长度,如果它无效,长度将使用此代码返回为零:

let assets = AVAsset(URL: NSURL(fileURLWithPath: filePath))
let length=Float(assets.duration.value)/Float(assets.duration.timescale)
if length != 0.0 {
// play it
}

如果还有问题可以随时问我。

关于ios - 检查在 avplayercontroller 中播放的有效 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com