我的Firebase存储中有一些音频文件。我可以获取这些音频文件的下载URL。我正在使用这些URL播放音频文件。(我没有下载音频文件)
问题是我很快就获得了音频文件的URL,但是AVPlayer仍然需要4到10秒钟才能开始播放。
我添加了以下代码行以减少延迟:
weakSelf.player!.automaticallyWaitsToMinimizeStalling = false
但是播放音频文件仍然需要几秒钟
这是我的功能:
var audioPlayer: AVAudioPlayer?
func playAudioFile(audioUrl: String) {
let audioPath = audioReference.child(audioUrl)
audioPath.downloadURL { [weak self] (url, err) in
guard let weakSelf = self else {return}
if err != nil {
print("Error => \(err)")
}
if let url = url {
do {
let playerItem = AVPlayerItem(url: url)
weakSelf.player = try AVPlayer(playerItem:playerItem)
weakSelf.player!.volume = 1.0
weakSelf.player!.automaticallyWaitsToMinimizeStalling = false
weakSelf.player!.play()
NotificationCenter.default.addObserver(weakSelf, selector: #selector(weakSelf.didFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
self?.playerObserver = playerItem.observe(\.status, options: [.new, .old], changeHandler: { (playerItem, change) in
if playerItem.status == .readyToPlay {
SVProgressHUD.dismiss()
}
})
} catch let error as NSError {
weakSelf.player = nil
print(error.localizedDescription)
} catch {
print("AVAudioPlayer init failed")
}
}
}
}
我希望AVPlayer从URL获得音频文件后立即播放音频文件。
如何减少延迟?