ios 使用AVPlayerViewController预览.flac文件时出现问题

osh3o9ms  于 2023-01-10  发布在  iOS
关注(0)|答案(1)|浏览(103)

我正在使用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()
}
vybvopom

vybvopom1#

尝试将资产加载为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)
})

相关问题