1

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

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

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 Code=-11800“操作无法完成” UserInfo={NSLocalizedDescription=操作无法完成,NSUnderlyingError=0x7b5b6810 {错误域=NSOSStatusErrorDomain Code=-12782“(null)”},NSLocalizedFailureReason=An发生未知错误(-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)
    }
}
4

1 回答 1

0

您应该确保您的 URL 提供有效的 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

于 2017-03-08T08:02:20.840 回答