AVAssetExportSession每次都会失败(错误-12780) [英] AVAssetExportSession fails every time (error -12780)

查看:871
本文介绍了AVAssetExportSession每次都会失败(错误-12780)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并一些音频文件(通过MPMediaPickerController选择),但是导出始终失败,错误代码为-12780.

I'm trying to merge some audio files (picked via MPMediaPickerController), but the export always fails with error code -12780.

当我尝试使用AVPlayer对象播放我的作品时,它会正确播放. 只是导出失败.

When I try to play my composition with an AVPlayer object, it plays correctly. Just the export fails.

我在做什么错了?

这是我的代码:

AVAssetExportSession *exportSession;
AVPlayer *player;

- (void)mergeAudiofiles {
    // self.mediaItems is an NSArray of MPMediaItems
    if (self.mediaItems.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"No Tracks selected."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }

    // Configure audio session
    NSError *sessionError;
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryAudioProcessing error:nil];
    [session setActive:YES error:&sessionError];
    if (sessionError) NSLog(@"Session-Error: %@", sessionError.localizedDescription);

    // Create composition
    AVMutableComposition *composition = [AVMutableComposition composition];
    AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    CMTime position = kCMTimeZero;

    for (MPMediaItem *item in self.mediaItems) {
        NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
        AVAsset *asset  = [AVAsset assetWithURL:assetURL];

        CMTimeRange duration = CMTimeRangeMake(kCMTimeZero, asset.duration);
        //        duration = CMTimeRangeMake(kCMTimeZero, CMTimeMake(5, 1));    // For player test

        NSError *error;
        [track insertTimeRange:duration ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] lastObject] atTime:position error:&error];
        if (error) NSLog(@"ERROR! :(");

        position = CMTimeAdd(position, duration.duration);
    }

    // Path to output file
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSURL *exportUrl = [NSURL URLWithString:[documentsDirectory stringByAppendingPathComponent:@"export.m4a"]];

    NSLog(@"Export URL = %@", exportUrl.description);

    //    Playing works!
    //    """"""""""""""
    //    AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithAsset:composition];
    //    player = [[AVPlayer alloc] initWithPlayerItem:pitem];
    //    [player play];
    //
    //    return;

    // Export
    exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
    exportSession.outputURL = exportUrl;
    exportSession.outputFileType = AVFileTypeAppleM4A;

    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch (exportSession.status) {
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Export failed -> Reason: %@, User Info: %@",
                      exportSession.error.localizedDescription,
                      exportSession.error.userInfo.description);
                break;

            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export cancelled");
                break;

            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Export finished");
                break;

            default:
                break;
        }
    }];
}

推荐答案

需要将导出URL更改为:

Need to change your export URL to:

    NSURL *exportUrl = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"export.m4a"]];

在引用文件路径时必须使用fileURLWithPath.我也对此感到迷恋,直到我读到它: AVAssetExportSession输出文件

You must use fileURLWithPath when referring to file paths. I got hung up by this too until I read this: AVAssetExportSession outputfile

这篇关于AVAssetExportSession每次都会失败(错误-12780)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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