AVAssetResourceLoaderDelegate和HLS不起作用?

时间:2016-06-22 08:29:20

标签: ios hls avasset avurlasset

对我来说,它不适用于模拟器和真实设备(iPhone 5S)上的“m3u8”网址。 与此同时,Apple演示工作良好,但我没有看到任何差异。 使用其他网址扩展名即可(例如,网址末尾的“m3u9”)

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController, AVAssetResourceLoaderDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // if I change m3u8 to different file extension, it's working good
        let url = NSURL(string: "cplp://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8") 

        let asset = AVURLAsset(URL: url!, options: nil)
        asset.resourceLoader.setDelegate(self, queue: dispatch_queue_create("AVARLDelegateDemo loader", nil))

        let item = AVPlayerItem(asset: asset)
        let player = AVPlayer(playerItem: item)
        player.play()
    }

    func resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {

        NSLog("This method is never called in case of m3u8 url")

        return true
    }
}

1 个答案:

答案 0 :(得分:0)

好的,我已经找到了如何修复它,但仍然不明白为什么它只发生在“m3u8”链接上。

需要保存强大的player或至少item参考。 谢谢,Apple,为文档。 :讽刺:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController, AVAssetResourceLoaderDelegate {
    var player: AVPlayer! // <-- the fix

    override func viewDidLoad() {
        super.viewDidLoad()

        // if I change m3u8 to different file extension, it's working good
        let url = NSURL(string: "cplp://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8") 

        let asset = AVURLAsset(URL: url!, options: nil)
        asset.resourceLoader.setDelegate(self, queue: dispatch_queue_create("AVARLDelegateDemo loader", nil))

        let item = AVPlayerItem(asset: asset)
        player = AVPlayer(playerItem: item) // <-- the fix
        player.play()
    }

    func resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {

        NSLog("This method is never called in case of m3u8 url")

        return true
    }
}