Search code examples
iosswiftxcodeavplayerhttp-live-streaming

HLS video is not playing on simulator and real device


I am working on an iOS app that simply plays the live stream HLS video.

My issue is that I have used AVPlayer and view controller to set up the playground, All things are working fine the view controller is started, the player is also started but the streaming doesn't start. The stream is a type of .m3u8 that works extremely fine in safari and chrome. The iOS doesn't show me up the video either on simulator or real-device.

I have also searched for other SO solutions but neither of them worked from me.

 /* 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()
    }

Solution

  • I was using HTTP URL which is not secured thus, it doesn't allow device or simulator to play it. I put an exception 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>
    

    enter image description here