Local video won't play

Hey there,

I wanted to play a local video in my app using the Apple introduction (https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos ).

Unfortunately it is not possible to use a local file for this.

Here is my code:

Code Block
@IBAction func playVideo(_ sender: Any) {
        guard let path = Bundle.main.url(forResource: "Video", withExtension: "mp4") else {
            print("Video file not found")
            return
        }
        let player = AVPlayer(url: path)
        let controller = AVPlayerViewController()
        controller.player = player
        present(controller, animated: true) {
            player.play()
        }
    }


Where is the issue?
In console I always get the message "Video file not found".

I implemented the video file in Assets.xcassets.

Help is welcome!

Replies

Hi,

To access videos in the asset catalog I believe you need to use NSAssetData's data property to get the video out of the asset catalog.

May I ask why you are storing it in the asset catalog anyway? It seems that storing it in the bundle itself might work better for you.
Hi, try moving your video file out of your asset catalog into your regular file structure (the bundle) and make sure your target is selected in the right panel.
Thanks for the answers!

It was just not possible to drag the video file in the bundle... so I placed it in the Asset Catalog.

What exactly do you mean with "bundle"?
Is it the first level of my file "ProjectName" where are files such "ProjectName.xcodeproj" or the second level "/ProjectName/ProjectName" where are my Swift files?

Or something completely different?