HLS&& iOS 10中的.m3u8

时间:2017-03-08 07:38:52

标签: ios swift avfoundation m3u8 avkit

我正在尝试在我的应用中播放.m3u8视频流。我正在使用Swift为iOS 9和10编程。

这是我到目前为止所得到的:

import UIKit
import AVKit
import AVFoundation
import DynamicBlurView

class VideoPlayerViewController: AVPlayerViewController {

    var urlString: String!
    let blurView = DynamicBlurView()

    override func viewDidLoad() {
        super.viewDidLoad()

        streamVideo()
    }

    func streamVideo() {
        blurView.frame = view.frame
        UIView.showProgressView(on: view, blurView: blurView)

        let url = URL(string: urlString!)
        let item = AVPlayerItem(url: url!)
        player = AVPlayer(playerItem: item)

        UIView.hideProgressView(on: view, blurView: blurView)
        player?.play()
    }
}

这应该很简单,但我在整个地方都会遇到错误和崩溃。

这是我得到的错误:

  

错误域= AVFoundationErrorDomain代码= -11800“操作可以   没有完成“UserInfo = {NSLocalizedDescription =该操作可以   未完成,NSUnderlyingError = 0x7b5b6810 {错误   Domain = NSOSStatusErrorDomain Code = -12782“(null)”},   NSLocalizedFailureReason =发生未知错误(-12782)}

请帮助我!

编辑:

没有运气就试过了这个建议。该应用程序仍然崩溃。如果该信息可以帮助任何人,那么崩溃将我带到AppDelegate。

import UIKit
import AVKit
import AVFoundation
import DynamicBlurView

class VideoPlayerViewController: AVPlayerViewController {

    var urlString: String!
    let blurView = DynamicBlurView()

    override func viewDidLoad() {
        super.viewDidLoad()

        streamVideo()
    }

    func streamVideo() {
        blurView.frame = view.frame
        UIView.showProgressView(on: view, blurView: blurView)

        let sampleURL = "https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8"

        guard let url = URL(string: sampleURL) else { return }
        let playerItem = AVPlayerItem(url: url)
        player = AVPlayer(playerItem: playerItem)
        player?.play()

        UIView.hideProgressView(on: view, blurView: blurView)
    }
}

1 个答案:

答案 0 :(得分:0)

您应确保您的网址提供有效的HLS来源。

import UIKit
import AVKit
import AVFoundation

class myAVPlayerViewController: AVPlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // custom HLS
        // http://localhost:3000/assets/videos/iframe_index.m3u8
        // apple HLS example
        // https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8

        guard let url = URL(string: "https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8") else { return }
        let playerItem = AVPlayerItem(url: url)
        player = AVPlayer(playerItem: playerItem)
        player?.play()
    }
}

另外,请记住在 Info.plist 中的 NSAppTransportSecurity 字典下将 NSAllowsArbitraryLoads 键设置为 YES