gpt4 book ai didi

ios - AVPlayer 太长并且延迟到 readyToPlay 和快速播放服务器音乐

转载 作者:IT王子 更新时间:2023-10-29 05:13:23 25 4
gpt4 key购买 nike

我尝试用 swift 语言缓冲并立即播放 remote url 音频。

但问题是readyToPlay案例和播放时间过长。

例如,一个声音 url 大约需要 12 到 15 秒才能运行。

这是我的代码:

var asset: AVAsset!
var player: AVPlayer!
var playerItem: AVPlayerItem!

private var playerItemContext = 0

let requiredAssetKeys = [ "playable","hasProtectedContent"]

let url = URL(string: "http://sound_link.mp3")!
asset = AVAsset(url: url)

playerItem = AVPlayerItem(asset: asset,
automaticallyLoadedAssetKeys: requiredAssetKeys)
playerItem.addObserver(self,
forKeyPath: #keyPath(AVPlayerItem.status),
options: [.old, .new],
context: &playerItemContext)

player = AVPlayer(playerItem: playerItem)

根据这个(ExploringAVFoundation) documentation've done that

我使用 observeValue func 来处理玩家准备好玩的游戏:

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

guard context == &playerItemContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}

if keyPath == #keyPath(AVPlayerItem.status) {
let status: AVPlayerItemStatus
if let statusNumber = change?[.newKey] as? NSNumber {
status = AVPlayerItemStatus(rawValue: statusNumber.intValue)!
} else {
status = .unknown
}
// Switch over status value
switch status {
case .readyToPlay:
print("readyToPlay \(status.rawValue)")
player.play() // here play remote sound
case .failed:
print("readyToPlay \(status.rawValue)")
case .unknown:
print("failed \(status.rawValue)")
}

}
}

这是返回的日志:

2017-02-08 13:44:00.626036 [15144:3747850] [aqme] 255: AQDefaultDevice (1): skipping input stream 0 0 0x0
readyToPlay 1
2017-02-08 13:44:02.631182 [15144:3747850] [aqme] 255: AQDefaultDevice (173): skipping input stream 0 0 0x0

在上面的日志中,用 4 秒出现 readyToPlay 1 然后用 10 秒播放声音

服务器速度很好,我尝试在 Android 中播放服务器声音,缓冲和播放声音的最长时间约为 3 秒(在 Android 应用程序中),但在IOS 完全缓冲和播放声音,总共需要大约 15 秒!

感谢您的关注

最佳答案

尝试使用这个:

player.automaticallyWaitsToMinimizeStalling = false

关于ios - AVPlayer 太长并且延迟到 readyToPlay 和快速播放服务器音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42110510/

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