Connor Woodford
Connor Woodford

Reputation: 41

Hls stream url wont play in AVPlayer

I am trying to play an hls live stream from a url that looks like this: "http://ip.address:port/my%20video.m3u8". Whenever the url does not involve any spaces the video plays fine otherwise it does not play. There is no error logged from the player but the player itself is just a black screen. I enabled "Allow Arbitrary Loads" and still no dice. When I try loading the urls with spaces in safari the video will play. Here is my code to load and play the video:

DispatchQueue.main.async {
    var player = AVPlayer(url: url!)
    var playerLayer: AVPlayerLayer!

    playerLayer = AVPlayerLayer(player: player)

    playerLayer.videoGravity = .resize
    self.videoView.layer.addSublayer(playerLayer)
    playerLayer.frame = self.videoView.bounds
    player.play()
}

What I also find odd is when I try sending the stream to a AVPlayerViewController the player pops up but does not play. Here is how I am sending it to the view controller:

DispatchQueue.main.async {
    let player = AVPlayer(url: urlTwo)
    let playerController = AVPlayerViewController()
    playerController.player = player
    present(playerController, animated: true) {
    player.play()
    }
}

Upvotes: 0

Views: 1555

Answers (1)

Rahul Verma
Rahul Verma

Reputation: 684

Please check the m3u8 file may be the content in m3u8 have some directory issue. .ts segment and .key file was mentioned in m3u8 file. And when you pass m3u8 to avplayer it fetch the key and .ts file automatically and decrypt the .ts with the mentioned key. Just cross check you m3u8 for key and .ts file.

Also, .key and .ts files need to be in same folder along with m3u8 otherwise it will not fetch and player keep looking for the key and ts.

#EXTM3U
#EXT-X-TARGETDURATION:12
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-VERSION:4
#EXT-X-KEY:METHOD=AES-128,URI="title.key"
#EXTINF:10.0100,
#EXT-X-BYTERANGE:943584@0
title.ts

Something like this.

Note: Sometimes m3u8 and key file have additional encryption which you need to decrypt on run time. I suggest open it in text editor and see if you can see something like given structure above. If it has something unreadable then you need to decrypt it first on run time. If you are using local server and loading file from cache then save the m3u8 file after decryption with Encoding.utf8 and .key file with Encoding.macOSRoman. Otherwise it will not gonna work. Cheers.

Upvotes: 1

Related Questions