gpt4 book ai didi

ios - AVPlayer 失败后不会播放新的 AVPlayerItem

转载 作者:行者123 更新时间:2023-11-28 23:55:30 76 4
gpt4 key购买 nike

我正在开发一个播放音频文件或流媒体的应用程序,遇到了一个我无法解决的问题。基本上我的 AVPlayer 在当前项目发生故障后不再播放新项目。下面是一个快速应用程序,您可以复制/粘贴以进行复制。

import UIKit
import AVFoundation

class ViewController: UIViewController {
@objc var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
self.player = AVPlayer.init(playerItem: AVPlayerItem.init(url: URL.init(string: "file:///Users/bobsacamano/Library/Developer/CoreSimulator/Devices/AD4CC461-847E-441B-98BA-D5A62EE210AE/data/Containers/Data/Application/F3AEA610-5358-4EAD-93B3-F30E9491D052/Library/07E2837C-5FF2-4B30-99A7-CCE683C19C29.mp3")!))
self.player.play()
self.addObserver(self, forKeyPath: #keyPath(player.currentItem.status), options: .new, context: nil)
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(player.currentItem.status) {
switch player.currentItem!.status {
case .readyToPlay:
print("Ready to play")
case .failed:
print("Failure \(String(describing: player.currentItem!.error?.localizedDescription))")
self.player.replaceCurrentItem(with: AVPlayerItem.init(url: URL.init(string: "http://traffic.libsyn.com/atpfm/atp281.mp3")!))
self.player.play()
case .unknown:
print("Unknown")
}
}
}
}

初始的 AVPlayerItem 不存在,所以 KVO 会返回一个失败。如果我尝试用有效的 URL 替换该项目,它仍然无法播放。我是否遗漏了一些明显的东西?

最佳答案

https://developer.apple.com/documentation/avfoundation/avplayer/1388096-status文档说“当这个属性的值为AVPlayer.Status.failed时,你不能再使用播放器进行播放,你需要创建一个新的实例来替换它。”

关于ios - AVPlayer 失败后不会播放新的 AVPlayerItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291692/

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