HLS 视频无法在模拟器和真实设备上播放

HLS video is not playing on simulator and real device

我正在开发一个 iOS 应用程序,它只播放实时流 HLS 视频。

我的问题是我已经使用 AVPlayer 和视图控制器来设置 playground,一切正常,视图控制器已启动,播放器也已启动,但流式传输未启动。流是一种 .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()
    }

我使用的是不安全的 HTTP URL,它不允许设备或模拟器播放它。我放了一个例外,允许不安全的协议,让 iOS 顺利流式传输 HLS。

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

在我的情况下,接受的答案似乎不正确我遇到了同样的问题并添加了特定于 ATS 的所有任意负载配置 dint 工作。

在我尝试了所有方法之后,修复是如果 url 从 "http" 开始,ATS 会很容易地停止 url,所以如果我们有不安全的 "http" url,将 url 中的 http 替换为 https 对我有用。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>some.url.to.com.countrycode</key>
        <dict>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

Swift:

let httpsurl = httpURLString.stringByReplacingOccurrencesOfString("http", withString: "https")

Obj-C:

NSString *httpsurl = [httpURLString stringByReplacingOccurrencesOfString:@“http” withString:@“https”];