使用exportAsynchronouslyWithCompletionHandler
我尝试从 iPod 库导出音频文件。我想用这个 iPod-Library 文件创建一个新文件。在《AV Foundation 编程指南》的“读取和写入资产”一章中,有一个小示例。但是这段代码对我不起作用。发生 4 个错误
Export Status 4 Error Domain=AVFoundationErrorDomain
Code=-11800 "The operation could not be completed"
UserInfo=0x1edfa0 {NSLocalizedFailureReason=An unknown error occurred (-12124),
NSUnderlyingError=0x1ebdc0 "The operation couldn’t be completed. (OSStatus error -12124.)",
NSLocalizedDescription=The operation could not be completed}
我的代码
AVURLAsset *objAvUrlAsset = [[AVURLAsset alloc] initWithURL:[tempArray objectAtIndex:0] options:nil];
NSLog(@"%s objAvUrlAsset %@", __FUNCTION__, objAvUrlAsset);
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:objAvUrlAsset];
NSLog(@"%s compatiblePresets %@", __FUNCTION__, compatiblePresets);
if ([compatiblePresets containsObject:AVAssetExportPresetAppleM4A]) {
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
initWithAsset:objAvUrlAsset presetName:AVAssetExportPresetAppleM4A];
NSString * exportPath = [NSString stringWithFormat:@"testTrack1.M4A"];
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.shouldOptimizeForNetworkUse = YES;
NSLog(@"%s export Session %@",__FUNCTION__, exportSession);
[exportSession exportAsynchronouslyWithCompletionHandler: ^(void) {
// export completed
NSLog(@"Export Status %d %@", exportSession.status, exportSession.error);
}];
[exportSession release];
}
nslog 消息
-[MyLocalMusicLibrary getAllTracks] objAvUrlAsset AVURLAsset: 0x1dd5a0, URL = ipod-library://item/item.mp3?id=-212958261728896866
-[MyLocalMusicLibrary getAllTracks] compatiblePresets (
AVAssetExportPresetAppleM4A,
AVAssetExportPreset960x540,
AVAssetExportPresetLowQuality,
AVAssetExportPresetMediumQuality,
AVAssetExportPreset640x480,
AVAssetExportPresetHighestQuality,
AVAssetExportPreset1280x720
)
-[MyLocalMusicLibrary getAllTracks] export Session AVAssetExportSession: 0x1e85c0,
asset = AVURLAsset: 0x1dd5a0, URL = ipod-library://item/item.mp3?id=-212958261728896866,
presetName = AVAssetExportPresetAppleM4A, outputFileType = com.apple.m4a-audio
-[MyLocalMusicLibrary getAllTracks] Export Status 4 Error Domain=AVFoundationErrorDomain
Code=-11800 "The operation could not be completed"
UserInfo=0x1edfa0 {NSLocalizedFailureReason=An unknown error occurred (-12124),
NSUnderlyingError=0x1ebdc0 "The operation couldn’t be completed. (OSStatus error -12124.)",
NSLocalizedDescription=The operation could not be completed}
如何修复这四个错误。我是否需要导出 AVAssetReader 和 AVAssetWriter?
I try to export an audio file from the iPod-Library. I want to create with this iPod-Library file an new file. In the AV Foundation Programming Guide in chapter Reading and Writing Assets there is a little example. However this code doesn't work for me. 4 errors happens
Export Status 4 Error Domain=AVFoundationErrorDomain
Code=-11800 "The operation could not be completed"
UserInfo=0x1edfa0 {NSLocalizedFailureReason=An unknown error occurred (-12124),
NSUnderlyingError=0x1ebdc0 "The operation couldn’t be completed. (OSStatus error -12124.)",
NSLocalizedDescription=The operation could not be completed}
My code
AVURLAsset *objAvUrlAsset = [[AVURLAsset alloc] initWithURL:[tempArray objectAtIndex:0] options:nil];
NSLog(@"%s objAvUrlAsset %@", __FUNCTION__, objAvUrlAsset);
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:objAvUrlAsset];
NSLog(@"%s compatiblePresets %@", __FUNCTION__, compatiblePresets);
if ([compatiblePresets containsObject:AVAssetExportPresetAppleM4A]) {
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
initWithAsset:objAvUrlAsset presetName:AVAssetExportPresetAppleM4A];
NSString * exportPath = [NSString stringWithFormat:@"testTrack1.M4A"];
NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.shouldOptimizeForNetworkUse = YES;
NSLog(@"%s export Session %@",__FUNCTION__, exportSession);
[exportSession exportAsynchronouslyWithCompletionHandler: ^(void) {
// export completed
NSLog(@"Export Status %d %@", exportSession.status, exportSession.error);
}];
[exportSession release];
}
the nslog message
-[MyLocalMusicLibrary getAllTracks] objAvUrlAsset AVURLAsset: 0x1dd5a0, URL = ipod-library://item/item.mp3?id=-212958261728896866
-[MyLocalMusicLibrary getAllTracks] compatiblePresets (
AVAssetExportPresetAppleM4A,
AVAssetExportPreset960x540,
AVAssetExportPresetLowQuality,
AVAssetExportPresetMediumQuality,
AVAssetExportPreset640x480,
AVAssetExportPresetHighestQuality,
AVAssetExportPreset1280x720
)
-[MyLocalMusicLibrary getAllTracks] export Session AVAssetExportSession: 0x1e85c0,
asset = AVURLAsset: 0x1dd5a0, URL = ipod-library://item/item.mp3?id=-212958261728896866,
presetName = AVAssetExportPresetAppleM4A, outputFileType = com.apple.m4a-audio
-[MyLocalMusicLibrary getAllTracks] Export Status 4 Error Domain=AVFoundationErrorDomain
Code=-11800 "The operation could not be completed"
UserInfo=0x1edfa0 {NSLocalizedFailureReason=An unknown error occurred (-12124),
NSUnderlyingError=0x1ebdc0 "The operation couldn’t be completed. (OSStatus error -12124.)",
NSLocalizedDescription=The operation could not be completed}
How can i fix this four errors. Do i need for this export the AVAssetReader and AVAssetWriter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从
AVAssetExportSession
得到了相同的结果,完全无用的NSError
试图告诉您的是您导出到的路径不完全存在。您需要确保使用NSFileManager
创建了要放置文件的目录的路径。I got the same result from
AVAssetExportSession
, what the completely uselessNSError
is trying to tell you is that the path you're exporting to doesn't fully exist. You'll want to make sure that you create the path to the directory you're placing the file in usingNSFileManager
.对我来说问题是我试图从 UIImagePickerController 获取文件后直接从 tmp 目录转换文件
我要做的就是首先将文件保存到库中,然后迭代 ALAssetLibrary 文件以查找保存的最新视频。然后我将该视频转换为 .mp4 并将其存储在应用程序的本地文档目录中以供我随意使用。
有点可笑的是,这是必要的,但它确实有效。
The problem for me was that I was trying to convert the file directly from the tmp directory after getting it from the UIImagePickerController
What I had to do was first save the file to the Library, then iterate over the ALAssetLibrary files to find the latest video saved. Then I converted that video to .mp4 and stored it int he application's local documents directory for use as I please.
Kind of ridiculous that this is necessary but it works.