我试图缓冲并立即播放远程url音频与
swift语言.
但问题是长时间准备好播放的情况和播放.
例如,声音网址需要大约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功能:
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秒!
感谢您的关注
解决方法
尝试使用URL实例化您的avPlayerItem,然后在全局线程中获取其他资源,我认为问题发生是因为avPlayer正在尝试在主UI线程中获取资源
当我尝试从avAssets阅读字幕选项时,我遇到了同样的问题
在我的播放器中加载电影花了5-6秒
当我尝试从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 } }) })