为什么不能在Playground中使用AVURLAsset从媒体文件中提取数据? [英] Why can't I extract data from media file using AVURLAsset in a Playground?

查看:172
本文介绍了为什么不能在Playground中使用AVURLAsset从媒体文件中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的操场上:

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.

推荐答案

它不起作用,因为Playgrounds是

It doesn't work because Playgrounds are sandboxed.

尽管您可以通过.checkResourceIsReachableAndReturnError看到文件URL,但实际上无法从Playground访问文件系统.

Although you can see your file URL with .checkResourceIsReachableAndReturnError, you can't actually access the filesystem from a Playground.

解决方案是将文件嵌入Playground本身.

打开文件导航器(带有菜单或CMD + 1),然后将MP4文件拖放到导航器的 Resources 文件夹中.

Open the File Navigator (with menu or CMD+1) then drop your MP4 file in the navigator's Resources folder.

完成后,您可以使用NSBundle访问文件-甚至只是将文件从导航器拖放到游乐场:

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 = #

将文件拖放到放置#的位置,它将粘贴一个实际上是文件URL的图标.

and drop the file where I've put the #, it will paste an icon which is actually the file URL.

现在此URL将与您的代码一起使用.

Now this URL will work with your code.

这篇关于为什么不能在Playground中使用AVURLAsset从媒体文件中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆