作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们的应用程序允许用户录制视频并将其保存到他们的相机胶卷中。但是,即使保存的视频可以从相机胶卷中正常播放,我们似乎也无法在 AVPlayer 中播放视频。
由于某种原因,有效的视频文件报告有 0 个轨道。
代码:
func playPreview(videoUrl: NSURL) {
let asset = AVAsset.assetWithURL(videoUrl) as? AVAsset
println("\(asset!.tracks.count) tracks found for \(videoUrl)")
let playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRectMake(0, 0, 320, 320)
view.layer.addSublayer(playerLayer)
player.seekToTime(kCMTimeZero)
player.play()
}
我们正在测试的 videoUrl 是:
file:///var/mobile/Containers/Data/Application/F532DF34-371C-437E-A121-04C532349720/Documents/editedVideo-7325.mp4
该文件存在,大小为 419498 字节,如下面的代码和输出所示。
既然文件显然有内容,为什么轨道报告为 0?除了检查轨道数量之外,我们还能做些什么来排除故障?
func listFiles() {
let allPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsPath = allPaths[0] as! NSString
if let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(docsPath as String, error: nil) as? [String] {
println("Found \(files.count) file(s) in \(docsPath):")
for filename in files {
let filePath = docsPath.stringByAppendingFormat("/\(filename)") as String
var attr = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: nil)
if let _attr:NSDictionary = attr {
let fileSize = _attr.fileSize()
println("\(filename) @ \(fileSize) bytes")
}
}
}
}
Found 11 file(s) in /var/mobile/Containers/Data/Application/5809B294-8A54-4506-A79A-F19E7C877A4A/Documents:
editedVideo-2252.mp4 @ 732567 bytes
editedVideo-3582.mp4 @ 728485 bytes
editedVideo-5175.mp4 @ 413897 bytes
editedVideo-5366.mp4 @ 607992 bytes
editedVideo-5459.mp4 @ 350652 bytes
editedVideo-622.mp4 @ 763026 bytes
**editedVideo-7325.mp4 @ 419498 bytes**
editedVideo-7428.mp4 @ 858742 bytes
editedVideo-7948.mp4 @ 923791 bytes
editedVideo-8612.mp4 @ 429463 bytes
editedVideo-9363.mp4 @ 511307 bytes
最佳答案
videoUrl
参数未指向正确的文件。
在本地引用视频时使用此方法:
let filename = "yourvideo.mp4"
let allDirs = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDir = allDirs[0] as! NSString
let filePath = documentsDir.stringByAppendingPathComponent(filename)
let fileUrl = NSURL(fileURLWithPath: filePath)
Documents
目录路径(例如/var/mobile/Containers/Data/Application/8BA180AB-2DD9-4FD8-9312-2BF006914561/Documents/)显然发生了变化。
您不能每次都期望相同的路径,因此不能像我们一样使用完全形成的文件 URL 字符串,即使是出于测试目的。
更改的内容是 Documents
目录的父目录(字母数字字符串)。
关于ios - 即使视频存在并且从相机胶卷中正常播放,AVAsset 也有 0 个轨道 : troubleshooting steps?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31127783/
我是一名优秀的程序员,十分优秀!