m3u8文件无法在iOS

时间:2016-11-03 09:03:53

标签: ios swift m3u8 avplayerviewcontroller

您好我想使用swift播放m3u8文件。我的m3u8文件结构是这样的。

#EXTM3U

#EXT-X-VERSION:3

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

但这不是在玩。但是如果我在here中使用apple sample m3u8文件进行播放。但是以上m3u8文件也在Android应用程序和windows vlc播放器中播放。这是什么原因?请帮我。 感谢

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

添加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)
      }

   }
}

3 个答案:

答案 0 :(得分:0)

尝试使用此框架。它为我工作。

pod'Keith'

对于示例项目,请下载并检查 https://github.com/Movile/Keith

答案 1 :(得分:0)

尝试使用其他播放器,例如节点播放器。据我说,AVplayer不是播放m3u8格式或用于实时流式传输的好选择。即使播放视频,播放时也会有延迟。

答案 2 :(得分:0)

我使用AVPlayerItem解决了类似的问题,以存储对AVAset的引用。

@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()
}