问题
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