7

I'm trying to play a local video file and keep getting the following log:

[framework] CUICatalog: Invalid asset name supplied: '(null)'

My video file is in the project directory and also in the main bundle resources. I've tried multiple versions of the syntax to play the video. Here's the code I have for now in a test project:

@IBAction func buttonAction(_ sender: Any) {

    if let path = Bundle.main.path(forResource: "slipMovement", ofType: 
    "mp4") {
        let video = AVPlayer(url: URL(fileURLWithPath: path))
        let videoPlayer = AVPlayerViewController()
        videoPlayer.player = video

        present(videoPlayer, animated: true, completion:  {
            video.play()
        })
    }
}

When I use a AVplayer and PlayerLayer, I don't get the 'null' messages. It only happens when I use AVPlayerViewController.

I'm fairly new to programming, any help would be very much appreciated as I just can't find a working solution online.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

I experienced this same problem when playing downloaded files from AWS S3.

I don't have an explanation for the meaning behind the error message.
But in my case it was because the file was not actually playable.


1. Make sure the path is correct.

I did not use Bundle.
I put the file under the Documents directory and I used FileManager to set the path:

let documentsUrl: URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let localFileName: String = "recording.mp4" 
let localFileUrl = documentsUrl.appendingPathComponent(localFileName)

print("Playing \(localFileUrl.absoluteString)")

That would print something like this:

file:///var/mobile/Containers/Data/Application/D8AF92BD-D736-4B86-9FF4-C60ABA6C741B/Documents/recording.mp4

2. Make sure the file is actually playable.

For this, I wanted to check what the file is actually like when it's stored on the device.
It turned out that file was "corrupted" (ex. copy/download interrupted), so it really wasn't playable.

  • Connect the iOS device to your Mac.
  • From Xcode, go to Window > Devices and Simulators
  • Look for your device from the Connected list on the left.
  • Look for your app from the Installed Apps list.
  • Click the gears on the bottom of the list.
  • Select Download Container..

download container

  • Download the *.xcappdata to a local directory
  • Open the directory in Finder
  • Right-click on the *.xcappdata, then select Show Package Contents
  • It should show something like the image below
  • Check the file's properties and try to play it

appdata

3. Make sure the file format is actually supported by AVPlayer

To see the supported AVPlayer formats, use print(AVURLAsset.audiovisualTypes()).

Reference:
https://developer.apple.com/documentation/avfoundation/avurlasset/1386800-audiovisualtypes


Once the path, the file, and the format were all OK, and I was able to play it normally using AVPlayer and AVPlayerViewController.

let player = AVPlayer(url: localFileUrl)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
    playerViewController.player!.play()
}
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135