AVAssetResourceLoaderDelegate 和 HLS 不起作用?

AVAssetResourceLoaderDelegate and HLS does not work?

对我来说,它无法在模拟器和真实设备 (iPhone 5S) 上使用 "m3u8" url。 同时,Apple demo 运行良好,但我看不出有什么不同。 与其他 url 扩展没关系(例如 "m3u9" 在 url 的末尾)

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
    }
}

好的,我已经找到了修复方法,但仍然不明白为什么它只发生在 "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
    }
}