简体   繁体   English

AVAssetExportSession exportAsynchronouslyWithCompletionHandle:导出音频错误

[英]AVAssetExportSession exportAsynchronouslyWithCompletionHandle : export audio error

I'm using AVAssetExportSession exportAsynchronouslyWithCompletionHandler to export an audio, but error. 我正在使用AVAssetExportSession exportAsynchronouslyWithCompletionHandler导出音频,但是出错。

(In many audios, just one audio will occur this problem.) (在许多音频中,只有一个音频会发生此问题。)

+ (void)exportAudioWithAsset:(AVAsset *)asset exportPath:(nonnull NSString *)exportPath completion:(nonnull void (^)(BOOL, NSError * _Nullable, NSURL * _Nullable))completion {
    if (!asset || !exportPath) {
        if (completion) {
            completion(NO, nil, nil);
        }
        return;
    }
    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
    session.outputURL = [NSURL fileURLWithPath:exportPath];
    session.outputFileType = AVFileTypeAppleM4A;
    session.shouldOptimizeForNetworkUse = YES;
    [session exportAsynchronouslyWithCompletionHandler:^{
        if (session.status == AVAssetExportSessionStatusCompleted) {
            NSData *data = [NSData dataWithContentsOfFile:exportPath];
            if ([data length] > 0) {
                if (completion) {
                    completion(YES, nil, [NSURL fileURLWithPath:exportPath]);
                }
            } else {
                if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
                    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
                }
                if (completion) {
                    completion(NO, session.error, nil);
                }
            }
        } else {
            if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {
                [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
            }
            if (completion) {
                completion(NO, session.error, nil);
            }
        }
    }];
}

Session's error : Error Domain=AVFoundationErrorDomain Code=-11800 "这项操作无法完成" UserInfo={NSLocalizedFailureReason=发生未知错误(-12769), NSLocalizedDescription=这项操作无法完成, NSUnderlyingError=0x283a97630 {Error Domain=NSOSStatusErrorDomain Code=-12769 "(null)"}} (这项操作无法完成 means couldn't complete) 会话的错误:错误域= AVFoundationErrorDomain代码= -11800“该操作无法完成” UserInfo = {NSLocalizedFailureReason =发生未知错误(-12769),NSLocalizedDescription =此操作无法完成,NSUnderlyingError = 0x283a97630 {Error Domain = NSOSStatusErrorDomain Code =- 12769“(null)”}}(此操作无法完成意味着无法完成)

为了解决此问题,我将AVAssetExportSession替换为AVAssetReader和AVAssertWriter进行导出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM