gpt4 book ai didi

ios - AVPlayer 和 AVPlayerItem 状态什么时候改变?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:55 55 4
gpt4 key购买 nike

我正在使用 AVFoundation 在 flutter 中开发一个音乐播放器插件,我想在 iOS 和 Android 上创建一个统一的界面。

我想捕获 AVPlayer 可能抛出的错误,例如,给定的 AVPlayerItem 无效(包含 URL不受支持的文件,例如 png 文件)。

我希望 AVPlayer.replace() 抛出异常或改变 AVPlayerItemAVPlayer 的状态或至少一些相关通知在 NSNotificationCeter 上。我试着听/观察所有这些,但没有一个报告任何类型的错误。事实上,AVPlayer 报告了“准备播放”状态。

    mediaPlayer = AVPlayer();
playerItem = AVPlayerItem(url: URL(fileURLWithPath: Bundle.main.path(forResource: "image", ofType: "png")!))

playerItem?.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), options: [.old, .new], context: &playerItemContext)
mediaPlayer?.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.old, .new], context: &mediaPlayerContext)

mediaPlayer?.replaceCurrentItem(with: playerItem); // this changes player status to - "ready-to-play"
mediaPlayer?.play() // this does not report anything

我是一名 android 开发人员,无法理解 AVPlayer 的工作原理。我怎样才能做到这一点?

注意:我不能使用 AVAudioPlayer,因为文档说在从网络或可能有延迟的源播放音乐时不应使用它。

self.mediaPlayer?.replaceCurrentItem(with: nil)
self.mediaPlayer?.play()

即使这段代码也不会产生错误

最佳答案

private func addObserverPlayerItem()
{
if let playerItem = self.player?.currentItem{
playerItem.addObserver(self, forKeyPath: "playbackBufferEmpty", options: .new, context: nil)
playerItem.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: .new, context: nil)
playerItem.addObserver(self, forKeyPath: "playbackBufferFull", options: .new, context: nil)
playerItem.addObserver(self, forKeyPath: "loadedTimeRanges", options: [.new], context: nil)
self.player?.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem.status), options: [.new, .initial], context: nil)
self.player?.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.new, .initial], context: nil)
}
}

override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if object is AVPlayerItem {
switch keyPath {

case "loadedTimeRanges":

let duration = self.currentItem?.totalBuffer() ?? 0
let totalduration = currentItem?.asset.duration
self.videoControll?.slider.bufferEndValue = totalduration?.seconds ?? 0
self.videoControll?.slider.bufferStartValue = (duration) / (totalduration?.seconds ?? 1)
print((duration) / (totalduration?.seconds ?? 1))

case "playbackBufferEmpty":
// Show loader
self.activityIndicator?.startAnimating()

case "playbackLikelyToKeepUp":
// Hide loader
self.activityIndicator?.stopAnimating()

case "playbackBufferFull":
// Hide loader
self.activityIndicator?.stopAnimating()
case #keyPath(AVPlayer.currentItem.status):

let newStatus: AVPlayerItem.Status
if let newStatusAsNumber = change?[NSKeyValueChangeKey.newKey] as? NSNumber {
newStatus = AVPlayerItem.Status(rawValue: newStatusAsNumber.intValue)!
} else {
newStatus = .unknown
}
if newStatus == .failed {
NSLog("SA Detected Error: \(String(describing: self.player?.currentItem?.error?.localizedDescription)), error: \(String(describing: self.player?.currentItem?.error))")
}
case #keyPath(AVPlayer.status):
print()
case .none:
self.activityIndicator?.stopAnimating()
case .some(_):
self.activityIndicator?.stopAnimating()
}
}
}

关于ios - AVPlayer 和 AVPlayerItem 状态什么时候改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56196363/

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