简体   繁体   中英

iOS AVPlayer not loading most HLS streams

I am trying to stream HLS video using an AVPlayer embedded in an AVPlayerViewController. To do this I am doing the following to setup an AVPlayerViewController, following the apple docs here: https://developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html

let url = NSURL.init(string: "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8") // example URL from apple dev works!

let asset = AVURLAsset.init(URL: url!)
let requestedKeys = ["tracks"]
asset.loadValuesAsynchronouslyForKeys(requestedKeys) {
    () -> Void in
    dispatch_async(dispatch_get_main_queue())
        {
            let error = NSErrorPointer()
            let status = asset.statusOfValueForKey("tracks", error: error)

            if (status == AVKeyValueStatus.Loaded) {
                let playerItem = AVPlayerItem.init(asset: asset)
                playerItem.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.init(rawValue: 0), context: nil)
                self.avPlayerViewController.player = AVPlayer.init(playerItem: playerItem)
            }
            else {
                // You should deal with the error appropriately.
                NSLog("The asset's tracks were not loaded:\n%@", (error.memory)!.localizedDescription);
            }
        }
    }

this all works fine. But when I change the url to

let url = NSURL.init(string: "http://cdn-fms.rbs.com.br/hls-vod/sample1_1500kbps.f4v.m3u8") // but this one doesn't

or any other HLS stream it doesn't work anymore. I get this image: dead AVPlayer

I can open the stream using safari and it works fine. I have also validated the stream using the Apple's HTTP Live Streaming Tools

I am a bit stuck now. Any ideas?

I was using HTTP URL which is not secured thus, it doesn't allow device or simulator to play it. I put an exception in info.plist to allow insecure protocols which let the iOS to stream the HLS smoothly.

 <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <true/>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
        <true/>
        <key>NSIncludesSubdomains</key>
        <true/>
    </dict>

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM