ios – AVPlayer太久了,随时准备播放并迅速播放服务器音乐

前端之家收集整理的这篇文章主要介绍了ios – AVPlayer太久了,随时准备播放并迅速播放服务器音乐前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图缓冲并立即播放远程url音频与 swift语言. @H_403_2@但问题是长时间准备好播放的情况和播放.

@H_403_2@例如,声音网址需要大约12到15秒才能运行.

@H_403_2@这是我的代码

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)
@H_403_2@根据这个(ExploringAVFoundation) documentation’ve done that

@H_403_2@并且为了处理该播放器准备玩家玩我使用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)")
        }

    }
}
@H_403_2@这是返回的日志:

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
@H_403_2@在上面的日志中,需要4秒钟才能显示readyToPlay 1,然后10秒钟播放声音

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

@H_403_2@感谢您的关注

解决方法

尝试使用URL实例化您的avPlayerItem,然后在全局线程中获取其他资源,我认为问题发生是因为avPlayer正在尝试在主UI线程中获取资源
当我尝试从avAssets阅读字幕选项时,我遇到了同样的问题
在我的播放器中加载电影花了5-6秒 @H_403_2@这是我如何解决我的问题(在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
                }

            })

        })
原文链接:https://www.f2er.com/iOS/335832.html

猜你在找的iOS相关文章