Adeline Yu
Adeline Yu

Reputation: 13

AVPlayer on Swift app shows a black screen but plays audio when playing video

New to Swift! I am trying to have the app play a video from my Resources upon pressing a button. I have this method in my ViewController:

if let path = Bundle.main.path(forResource: "Cypress", ofType: "mov") {
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        
        // Create a new AVPlayerViewController and pass it a reference to the player.
            let controller = AVPlayerViewController()
            controller.player = player

            // Modally present the player and call the player's play() method when complete.
        self.present(controller, animated: true) {
                player.play()
            }
    }

However, when the video begins to play, it is a black screen but the audio is playing properly. Are there any quick fixes to this? I am using Xcode version 12.2. Any advice would be greatly appreciated thanks :)

Upvotes: 1

Views: 1766

Answers (1)

shim
shim

Reputation: 10126

The problem is with the video file itself / the encoding.

Deceptively, the Quicktime ".mov" format is not a video codec, but rather audio/video container format that can contain video and audio (and some other things) in any number of compression codecs (h.264, mpeg2, ProRes, MJPEG, AAC, mp3, etc.) … Your files don't work because they include video compressed with a codec which iOS does not support.

From @alexkent in this SO answer on another post.

I don't think a .mov is necessarily not going to work for you, but you'd have to make sure it was encoded in a way that is iOS-compatible. So as you mentioned in the comments, using .mp4 ensures the video is encoded in an iOS-compatible fashion.

Upvotes: 1

Related Questions