使用exportAsynchronouslyWithCompletionHandler

时间:2011-03-29 09:47:07

标签: iphone avfoundation ipod m4a

我尝试从iPod-Library导出音频文件。我想用这个iPod-Library文件创建一个新文件。在“AV基础编程指南”的“阅读和写作资产”一章中,有一个小例子。但是这段代码对我不起作用。发生了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。

2 个答案:

答案 0 :(得分:8)

我从AVAssetExportSession获得了相同的结果,完全没用的NSError试图告诉你的是你导出的路径并不完全存在。您需要确保使用NSFileManager创建放置文件的目录的路径。

答案 1 :(得分:0)

对我来说问题是我在从UIImagePickerController获取文件后试图直接从tmp目录转换文件

我要做的是首先将文件保存到库中,然后遍历ALAssetLibrary文件以查找保存的最新视频。然后我将该视频转换为.mp4并将其存储在应用程序的本地文档目录中,以供我随意使用。

有点荒谬,这是必要的,但它确实有效。