0

Hello I want to play m3u8 file using swift. My m3u8 file structure is like this.

#EXTM3U

#EXT-X-VERSION:3

#EXT-X-STREAM- INF:BANDWIDTH=814508,CODECS="avc1.66.51,mp4a.40.34",RESOLUTION=720x576 chunklist_w247403833.m3u8

But this is not playing. But if I used apple sample m3u8 file in here its playing. but above m3u8 file is playing in android app and windows vlc player too. What would be the reason for this? Please help me. Thanks

code

 let videoURL = NSURL(string: "http://74.208.128.124:1935/live/myStream/playlist.m3u8")//http://devstreaming.apple.com/videos/wwdc/2016/102w0bsn0ge83qfv7za/102/hls_vod_mvp.m3u8
    let player = AVPlayer(url: videoURL! as URL)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.bounds
    self.view.layer.addSublayer(playerLayer)
    player.play()
}

Adding KVO

 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
   if keyPath=="status"
   {
      if(player.status==AVPlayerStatus.readyToPlay)
      {
        print(player.status)
      }
    else if(player.status==AVPlayerStatus.failed)
      {
        print("ERROR-----------", player.status)
      }

   }
}
Irrd
  • 325
  • 1
  • 6
  • 18
  • What's your code? Did you get a `AVPlayerStatus` and the corresponding error? – Larme Nov 03 '16 at 09:09
  • 1
    Try to add KVO listen to the status, like there http://stackoverflow.com/questions/5401437/knowing-when-avplayer-object-is-ready-to-play I guess that looking for "KVO + Swift" + the question may help you. You may be able to get the reason of the fail. – Larme Nov 03 '16 at 09:13
  • Thanks. I added the KVO and it never print my Error log, its going into if(player.status==AVPlayerStatus.readyToPlay) and it just print AVPlayerStatus in the log – Irrd Nov 03 '16 at 09:36
  • In case of `AVPlayerStatus.failed`, try to read `player.error`? Else, look there http://stackoverflow.com/questions/24004313/how-handle-streaming-error-with-avplayer it may help. – Larme Nov 03 '16 at 10:30

3 Answers3

0

Try to use this framework. It worked for me.

pod 'Keith'

For a sample project download this and check https://github.com/Movile/Keith

Yevhen
  • 1,048
  • 1
  • 14
  • 24
0

Try to use some other player like node player. According to me, AVplayer is not a good option to play m3u8 format or for live streaming. Even if the video plays, there will be lag while playing it.

iOS Developer
  • 464
  • 6
  • 24
0

I fixed a similar issue using AVPlayerItem to store the reference to the AVAsset.

@IBOutlet weak var videoContainer: UIView!
var player: AVPlayer?
var playerLayer: AVPlayerLayer?
var urlM3u8 = URL(string:"http://devstreaming.apple.com/videos/wwdc/2016/102w0bsn0ge83qfv7za/102/hls_vod_mvp.m3u8")

 func videoPlayer(url: URL){
    let item = AVPlayerItem(url: url)
    player = AVPlayer(playerItem: item)
    playerLayer = AVPlayerLayer(player: player)
    playerLayer?.videoGravity = .resizeAspect
    playerLayer?.frame = CGRect(x: 0, y: 0, width: self.videoContainer.frame.width, height: self.videoContainer.frame.height)
    videoContainer.layer.addSublayer(playerLayer!)
    player?.play()
}
STerrier
  • 3,755
  • 1
  • 16
  • 41