AVPlayerViewController play/pause issue in iOS

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 07:37:44

问题


I am working on the iOS application. This application base on the NEWS media.

I am playing the small video of highlighted News. I am facing a stranger problem with AVPlayerViewController.

When i play the video everything is fine. When i go to offline the buffer video play perfect & after the play buffer video, the video stops but the progress bar(Slider) continuously in play state while the video has been stopped.

For more clearance please watch this GIF image :

Please visit this link for batter understanding.

This is my code for player.

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.playVideo()
    }

    func playVideo(){
        let videoURL = NSURL(string: "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8")
        let player = AVPlayer(URL: videoURL!)
        let playerController = AVPlayerViewController()
        playerController.player = player
        self.addChildViewController(playerController)
        self.view.addSubview(playerController.view)
        playerController.view.frame = self.view.frame
        player.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

回答1:


I've tried your code and I couldn't even see picture from your gif. I've also tried your code with another video link (https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4) and problem wasn't reproduced.

I guess your problem is in the way you try to show your AVPlayerViewController.

Consider next snippet:

let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
    playerViewController.player!.play()
}

This should work. If you need some ui customization you shouldn't use AVPlayerViewController I guess.

UPDATE:

Try adding Reachaility support:

class MyViewController: UIViewController {
  var reachability: Reachability?

  override func viewDidLoad() {
    super.viewDidLoad()

    reachability = try? Reachability.reachabilityForInternetConnection()
    reachability?.whenUnreachable = { _ in
      player.pause()
    }
    try? reachability?.startNotifier()


来源:https://stackoverflow.com/questions/39411636/avplayerviewcontroller-play-pause-issue-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!