3

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()
    }
Mahgolsadat Fathi
  • 3,107
  • 4
  • 16
  • 34

2 Answers2

0

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

0

The accepted answer don't seems to be correct in my case I was having the same issue and added the ATS specific all arbitrary loads configurations dint work.

After everything I tried, the fix was that the ATS stops a url readily if the url is starting from "http", so if we have the non-secure "http" url, replacing the http to https in the url worked for me.

<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”];
SaadurRehman
  • 622
  • 8
  • 20