问题
In my Playground:
import Cocoa
import Foundation
import AVFoundation
var path2 = "/Users/me/Movies/bukesiyi.mp4"
var video2 = NSURL(fileURLWithPath: path2, isDirectory: false)
// it shows: file:///Users/me/Movies/bukesiyi.mp4
video2.checkResourceIsReachableAndReturnError(nil)
// it's true
var asset = AVURLAsset(URL: video2)
// it shows: <AVURLAsset: 0x7fc0a9f13500, URL = file:///Users/me/Movies/bukesiyi.mp4>
asset.tracksWithMediaType(AVMediaTypeVideo)
// it's []
asset.readable
// it's false
asset.playable
// it's false
asset.commonMetadata
// it's []
I don't know what is wrong, maybe the path? The documentation is not clear.
回答1:
It doesn't work because Playgrounds are sandboxed.
Although you can see your file URL with .checkResourceIsReachableAndReturnError
, you can't actually access the filesystem from a Playground.
The solution is to embed the file in the Playground itself.
Open the File Navigator (with menu or CMD+1) then drop your MP4 file in the navigator's Resources folder.
When done, you can access the file with NSBundle
- or even just drag and drop the file from the navigator to the Playground:
let video2 = #
and drop the file where I've put the #, it will paste an icon which is actually the file URL.
Now this URL will work with your code.
来源:https://stackoverflow.com/questions/34616197/why-cant-i-extract-data-from-media-file-using-avurlasset-in-a-playground