我正在尝试播放本地视频文件并不断收到以下日志:
[框架] CUICatalog:提供的 Assets 名称无效:'(null)'
我的视频文件在项目目录中,也在主包资源中。我已经尝试了多个版本的语法来播放视频。这是我现在在测试项目中的代码:
@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()
})
}
}
当我使用 AVplayer 和 PlayerLayer 时,我没有收到“空”消息。它只在我使用 AVPlayerViewController 时发生。
我是编程新手,非常感谢任何帮助,因为我无法在线找到有效的解决方案。
最佳答案
我在播放从 AWS S3 下载的文件时遇到了同样的问题。
我没有解释错误消息背后的含义。
但就我而言,这是因为该文件实际上无法播放。
1。确保路径正确。
我没有使用Bundle
。
我将文件放在 Documents 目录下,并使用 FileManager
设置路径:
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)")
那会打印出这样的东西:
file:///var/mobile/Containers/Data/Application/D8AF92BD-D736-4B86-9FF4-C60ABA6C741B/Documents/recording.mp4
2。确保文件确实可以播放。
为此,我想检查文件存储在设备上时的实际情况。
事实证明该文件已“损坏”(例如复制/下载中断),因此它确实无法播放。
- 将 iOS 设备连接到您的 Mac。
- 从 Xcode 转到窗口> 设备和模拟器
- 从左侧的已连接列表中查找您的设备。
- 从已安装的应用列表中查找您的应用。
- 点击列表底部的齿轮。
- 选择下载容器..
- 下载 *.xcappdata 到本地目录
- 在 Finder 中打开目录
- 右键单击 *.xcappdata,然后选择显示包内容
- 它应该显示如下图所示
- 检查文件的属性并尝试播放
3。确保 AVPlayer
确实支持该文件格式
要查看支持的 AVPlayer 格式,请使用 print(AVURLAsset.audiovisualTypes())
。
引用:
https://developer.apple.com/documentation/avfoundation/avurlasset/1386800-audiovisualtypes
一旦路径、文件和格式都OK,我就可以使用AVPlayer
和AVPlayerViewController
正常播放了。
let player = AVPlayer(url: localFileUrl)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
关于 swift 4 : CUICatalog: Invalid asset name supplied: '(null)' when using AVPlayerViewController Only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49182840/