我尝试缓冲并立即用快速语言播放remote url
音频。
但问题是长时间的readyToPlay
案件和游戏。
例如,声音网址需要大约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
尝试用URL实例化你的avPlayerItem
,然后在全局线程中获取其他资产,我认为问题发生是因为avPlayer
试图在主UI线程中获取资产我遇到了同样的问题当我试图从avAssets
读取字幕选项时花了5在我的播放器中加载电影需要多6秒
这就是我解决问题的方法(在Swift 2中),我希望它有所帮助:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let avMediaSelectionGroup : AVMediaSelectionGroup? = self.container.player.currentItem?.asset.mediaSelectionGroupForMediaCharacteristic(AVMediaCharacteristicLegible)
dispatch_async(dispatch_get_main_queue(), {
if (avMediaSelectionGroup != nil && (avMediaSelectionGroup!.options.count != 0) && avMediaSelectionGroup?.options[0].valueForKey("title") != nil)
{
// show hide subtitle button
}
})
})