Why can't I extract data from media file using AVURLAsset in a Playground?

北战南征 提交于 2019-12-14 03:50:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!