I am playing videos from urls in AVPlayer. It is working fine. But, Few videos keep loading but not player, I checked them in browser and they are showing Video does not exist.
So, How to detect that video does not exist state and how to show alert for end user understands.
override func viewDidLoad() {
super.viewDidLoad()
self.setupAVAudioSession()
}
func playVideo(){
if let str = videoUrl{
if let videoURL:URL = URL(string: str) {
player = AVPlayer(url: videoURL)
player?.rate = 1 //auto play
let playerFrame = CGRect(x: 0, y: 0, width: 200, height: 210)
let playerViewController = AVPlayerViewController()
playerViewController.delegate = self
playerViewController.player = player
playerViewController.view.frame = playerFrame
playerViewController.showsPlaybackControls = true
addChild(playerViewController)
videoPlayerView.addSubview(playerViewController.view)
}
}
}
func setupAVAudioSession(){
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
}
catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
}
func playerViewController(_ playerViewController: AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator){
}
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator){
playerViewController.dismiss(animated: true)
}
Any suggestions?
You can check existence of your video url by sending
HEAD
request before start playing it e.g.: