OStack程序员社区-中国程序员成长平台

标题: ios - AVAssetResourceLoaderDelegate 和 HLS 不起作用? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:47
标题: ios - AVAssetResourceLoaderDelegate 和 HLS 不起作用?

对我来说,它不适用于模拟器和真实设备(iPhone 5S)上的“m3u8”网址。 同时,Apple 演示运行良好,但我看不出有什么不同。 使用其他 url 扩展名也可以(例如,url 末尾的“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
    }
}



Best Answer-推荐答案


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

关于ios - AVAssetResourceLoaderDelegate 和 HLS 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37962564/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4