0
votes

I'm using AVAssetExportSession exportAsynchronouslyWithCompletionHandler to export an audio, but error.

(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)

1

1 Answers

0
votes

To solve this problem, I replace AVAssetExportSession with AVAssetReader And AVAssertWriter to export.