[英]Assigning AVAsset from NSData
Can I initialise AVAsset
from NSData
.. I know i can initialise AVData
from url using the function like我可以从
NSData
初始化AVAsset
.. 我知道我可以使用像这样的函数从 url 初始化AVData
[[AVURLAsset alloc] initWithURL:url options:nil];
Can any one suggest me a similar way of assigning AVAsset
directly from NSData
?任何人都可以建议我直接从
NSData
分配AVAsset
的类似方法吗?
As far as I know, no.据我所知,没有。
AVAsset
represents items in the (internal) media library of a device, so initializing it using your custom data wouldn't make much sense. AVAsset
表示设备(内部)媒体库中的项目,因此使用自定义数据对其进行初始化没有多大意义。 Perhaps it's worth mentioning that this class doesn't have a public initializer for NSData
.也许值得一提的是,这个类没有
NSData
的公共初始值设定项。
I suspect that you should most probably need to redesign your code logic if you need this.我怀疑如果你需要的话,你很可能需要重新设计你的代码逻辑。
The accepted answer is incorrect.接受的答案是不正确的。
It is possible to create an AVAsset
from an NSData
via an AVAssetResourceLoaderDelegate
implementation.它可以创建一个
AVAsset
从NSData
通过AVAssetResourceLoaderDelegate
实现。
Specifically implement:具体实现:
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool
It must:它必须:
loadingRequest.contentInformationRequest.contentType = AVFileType.mycontenttype.rawValue
loadingRequest.contentInformationRequest.contentType = AVFileType.mycontenttype.rawValue
I'll post some code later, but you can google "AVAssetResourceLoaderDelegate github" for examples我稍后会发布一些代码,但您可以谷歌“AVAssetResourceLoaderDelegate github”以获取示例
This is an alternate solution the way I solved it.这是我解决它的方式的替代解决方案。 Indirectly I initialised by writing/saving NSData to local directory first and then initialise AVAsset from that filepath.
我通过首先将 NSData 写入/保存到本地目录来间接初始化,然后从该文件路径初始化 AVAsset。
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:locationUrl]];
You can try writing your nsdata to filepath first then initialising from that path to AVAsset.您可以尝试先将 nsdata 写入文件路径,然后从该路径初始化到 AVAsset。
[videoData writeToFile: locationUrl atomically:YES];
Just make sure that file location URL is correct and also test confirm if data is being saved to that file path if anything error happens when loading back.只需确保文件位置 URL 正确,并测试确认数据是否正在保存到该文件路径,如果在加载时发生任何错误。 Lastly don't forget to clean up your files if you no longer need them.
最后,如果您不再需要文件,请不要忘记清理它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.