Video Player: Error if video could not load?

Hey there,

I created a video player like this one: https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/creating_a_basic_video_player_ios_and_tvos

But if the video file can not load (because of wrong url or missing internet connection) the player is just showing this loading-circle.
So there is no information whether the video file can be loaded or not.

Is there a way to check if a video file from the internet is available or not?

Thanks!

Accepted Reply

Could you show the code where you read and try to launch the video ?

You could use
AVAsset(url: url).isPlayable
to test if video is playable; and react accordingly.

You may have a look here:
https://stackoverflow.com/questions/48866004/ios-detecting-if-an-url-stream-is-working-or-not-with-avplayer

Replies

Could you show the code where you read and try to launch the video ?

You could use
AVAsset(url: url).isPlayable
to test if video is playable; and react accordingly.

You may have a look here:
https://stackoverflow.com/questions/48866004/ios-detecting-if-an-url-stream-is-working-or-not-with-avplayer
Thanks for the helpful answer!
Code was this:
Code Block Swift
func getVideoPlayerOnline(url: String) -> AVPlayer? {
    guard let url = URL(string: url) else {
        print("url not found")
            return nil
        }
    //test if video is playable
    if AVAsset(url: url).isPlayable {
        let player = AVPlayer(url: url)
        return player
    } else {
        return nil
    }
}


Now if I insert .isPlayable it works!


By the way, dear Claude:
Do you know how to enable email notifications in this forum?
Cause I didn't get them on this and on other threats.