![](/img/trans.png)
[英]Video playback issues only on iOS 13 with AVPlayerViewController and AVPlayer when using HLS video
[英]Issues when previewing .flac files with AVPlayerViewController
我正在使用AVPlayerViewController
在我的应用程序中预览所有格式的音频。
一切正常,但在我添加了对.flac
文件的支持后,它出现了错误。
播放器有时会正确加载视频,有时则不会。 其他格式永远不会发生这种情况,例如.mp3
。
这是正确加载视频和未正确加载视频时的样子(第一张图片是一切正常的时候):
所有音频格式的逻辑都是一样的:
private lazy var _player = AVPlayer()
private lazy var _playerViewController = AVPlayerViewController()
func playAudio(correctUrl: URL) {
var item = AVPlayerItem(url: correctUrl)
self._player.replaceCurrentItem(with: item)
self._playerViewController.player = self._player
self.registerForAVPlayerNotifications()
self._playerViewController.player?.play()
}
尝试将资产加载为AVURLAsset
并检查可能的加载错误。
func loadAsset(_ streamUrl: URL,completionAfterLoad: ((AVPlayerItem) -> Void)? = nil) {
let asset = AVURLAsset(url: streamUrl, options: nil)
let assetKeys = [
"playable",
"tracks",
"duration"
]
// Create a new AVPlayerItem with the asset and an
// array of asset keys to be automatically loaded
asset.loadValuesAsynchronously(forKeys: assetKeys, completionHandler: {
DispatchQueue.main.async {
var error: NSError? = nil
var status = asset.statusOfValue(forKey: "playable", error: &error)
switch status {
case .loaded:
print("loaded\n\(String(describing: error?.localizedDescription))")
case .failed:
print("failed\n\(String(describing: error?.localizedDescription))")
case .cancelled:
print("cancelled\n\(String(describing: error?.localizedDescription))")
default:
print("default\n\(String(describing: error?.localizedDescription))")
}
status = asset.statusOfValue(forKey: "tracks", error: &error)
switch status {
case .loaded:
print("loaded\n\(asset.tracks)")
case .failed:
print("failed\n\(String(describing: error?.localizedDescription))")
case .cancelled:
print("cancelled\n\(String(describing: error?.localizedDescription))")
default:
print("default\n\(String(describing: error?.localizedDescription))")
}
// If loading of duration fails, stream plylist is in wrong format (e.g. missing version number etc.)
status = asset.statusOfValue(forKey: "duration", error: &error)
switch status {
case .loaded:
print("loaded\n\(asset.tracks)")
case .failed:
print("failed\n\(String(describing: error?.localizedDescription))")
case .cancelled:
print("cancelled\n\(String(describing: error?.localizedDescription))")
default:
print("default\n\(String(describing: error?.localizedDescription))")
}
print("tracks\n\(asset.tracks(withMediaType: .audio))")
let playerItem = AVPlayerItem(asset: asset, automaticallyLoadedAssetKeys: assetKeys)
completionAfterLoad?(playerItem) // perform any action to happen after playlist is completed loading
}
})
}
// Usage:
let avPLayer = AVPlayer()
let url = URL(string: "")!
loadAsset(url,completionAfterLoad: { playerItem in
avPLayer.replaceCurrentItem(with: playerItem)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.