OGeek|极客世界-中国程序员成长平台

标题: ios - HLS 视频无法在模拟器和真实设备上播放 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:44
标题: ios - HLS 视频无法在模拟器和真实设备上播放

我正在开发一个 iOS 应用,它只播放 HLS 直播视频。

我的问题是我已经使用 AVPlayer 和 View Controller 来设置 Playground ,一切正常, View Controller 已启动,播放器也已启动,但流未启动。该流是一种 .m3u8 类型,在 safari 和 chrome 中工作得非常好。 iOS 不会在模拟器或真实设备上显示视频。

我还搜索了其他 SO 解决方案,但它们都没有对我有用。

 /* Button to play live news streaming */
@IBAction func liveNews(_ sender: Any)
{
    guard let NewsUrl = URL(string: "http://cdn39.live247stream.com/A1TVuk/tv/playlist.m3u8")
        else {
            return }

    /* Create an AV PLAYER and passed the HLS URL to it */
    let player = AVPlayer(url: NewsUrl)
    player.allowsExternalPlayback = true

    /* Setup a player view controller to handle the stream */
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player

    /* Using method of play() to load and play the stream  */
    present(playerViewController, animated: true){
    playerViewController.player?.play()
    }



Best Answer-推荐答案


我使用的是不 protected HTTP URL,因此它不允许设备或模拟器播放它。我放了一个异常(exception),允许不安全的协议(protocol)让 iOS 流畅地流式传输 HLS。

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

enter image description here

关于ios - HLS 视频无法在模拟器和真实设备上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53004636/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4