如何将AVAsset转换为NSData或将其保存到文件管理器 [英] How to convert AVAsset to NSData or save it to file manager

查看:805
本文介绍了如何将AVAsset转换为NSData或将其保存到文件管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AVAsset 来录制音频文件,然后在第一次将它存储在 NSFileManager 之后之后将其转换为nsdata i Call API来存储它。

I am trying to do that using AVAsset to record Audio file and then after first i store it on NSFileManager and after that by convert it to nsdata i Call API to store it.

我成功创建 AVAsset 录制使用 SCPlayer 的第三方类进行文件播放。

I am successful to Create AVAsset recording file and play it using third party Class that is SCPlayer.

现在的问题是我不知道如何使用 AVAsset 文件将其保存在文件管理器中,然后在调用API之后将其转换为 NSData

Now problem is that i don't know how to Use AVAsset file for save it in file manager and then after call API to sent it by converting it to NSData.

有没有办法将 AVAsset 转换为 NSData ? ??

Is any way to convert AVAsset to NSData???

请帮助...

推荐答案

你可以做以下内容:


  1. 使用 AVAssetExportSession 导出 AVAsset 文件
    路径网址的对象。

  2. 将其转换为 NSData 使用它的 dataWithContentsOfURL
    方法。

  1. Use AVAssetExportSession to export your AVAsset object to a file path URL.
  2. Convert it to NSData using its dataWithContentsOfURL method.


NSURL *fileURL = nil;
__block NSData *assetData = nil;

// asset is you AVAsset object
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];

exportSession.outputURL = fileURL;
// e.g .mov type 
exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    assetData = [NSData dataWithContentsOfURL:fileURL];
    NSLog(@"AVAsset saved to NSData.");
}];


  • 执行任何操作后,不要忘记清理输出文件它;)

  • Don't forget to clean up the output file after doing whatever you need to do with it ;)

    这篇关于如何将AVAsset转换为NSData或将其保存到文件管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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