0
votes

i have problem with HLS live stream. Sometimes when i created AVPlayer via code :

    if let mediaUrl = URL(string: obj.message.replacingOccurrences(of: ".mpd", with: ".m3u8")) {
  // load movie via url
  self.player = AVPlayer(url: mediaUrl)
  let layer = AVPlayerLayer(player: self.player)
  self.streamView.layer.addSublayer(layer)
  // set stream view where will be shown stream
  layer.frame = self.streamView.layer.bounds
  self.player.play()
}

I have blank screen with no stream and no error, but when i lock device and unlock then stream starts.

This case is once every 10/20 times when i'm trying to play stream.

Any suggestions?

4

4 Answers

1
votes

I had the same issue , going through the logs revealed that my streaming url was not secure. So it was the ATS (App transport Security) issue. Resolved the issue by adding ATS specific KVP's in info.plist

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
0
votes

Verify your mediaUrl is populated. Try streaming Apple's sample live stream: https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8

This runs fine for me (I didn't implement your streamView though)

private var player: AVPlayer!

override func viewDidLoad() {
    super.viewDidLoad()
    if let mediaUrl = URL(string: "https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8") {
        // load movie via url
        self.player = AVPlayer(url: mediaUrl)
        let layer = AVPlayerLayer(player: self.player)
        self.view.layer.addSublayer(layer)
        // set stream view where will be shown stream
        layer.frame = self.view.layer.bounds
        self.player.play()
    }
}
0
votes

The reason could be wrong metadata in the video. Take a look at this thread which I answered: AVPlayer HLS live stream IOS

The transcoded video needs to have profile baseline in order to be played in AVPlayer. Take look at the ffmpeg transcoding command for details:

https://gist.github.com/chung-nguyen/d88e73e3cc8788878f5ffb8c232b4729

-1
votes

Try creating new object of AVPlayer, don't use sharedInstance of AVPlayer and then check.

Means always create new object of AVPlayer, don't use self.player.

And then check.