1

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 :)

shim
  • 9,289
  • 12
  • 69
  • 108
Adeline Yu
  • 13
  • 3
  • 1
    Likely the problem is with the video file itself / the encoding. https://stackoverflow.com/questions/27753755/workaround-playing-a-video-mov-file-in-ios-app-also-related-to-adobe-flash-ani – shim Dec 10 '20 at 19:49
  • Yep! The video had to be mp4 not mov. Thanks! – Adeline Yu Dec 18 '20 at 18:42

1 Answers1

1

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.

shim
  • 9,289
  • 12
  • 69
  • 108