【问题标题】:merging 2 audio files caf using AVFoundation in iOS 9在 iOS 9 中使用 AVFoundation 合并 2 个音频文件 caf
【发布时间】:2017-06-12 18:38:11
【问题描述】:

嗨,我只是在 iOS 9 中遇到了这个问题。我正在尝试合并 2 个 caf 文件,并且在 iOS 8 中一切正常,但是当我在 iOS 9 上测试应用程序时,它无法正常工作。只播放了一个文件当我播放最终合并文件时..任何形式的帮助将不胜感激A

AVMutableComposition *composition = [[AVMutableComposition alloc] init];

        AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionAudioTrack setPreferredVolume:1.0];
        NSURL *url = [NSURL fileURLWithPath:[url3 path]];
        AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
        AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];

        AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
        [compositionAudioTrack1 setPreferredVolume:1.0];
        NSURL *url1 = [NSURL fileURLWithPath:[url12 path]];
        AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil];
        AVAssetTrack *clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset1.duration) ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil];

        AVAssetExportSession *exportSession = [AVAssetExportSession
                                               exportSessionWithAsset:composition
                                               presetName:AVAssetExportPresetAppleM4A];

        //NSLog(@"Output file path - %@",soundOneNew);

        // configure export session  output with all our parameters
        exportSession.outputURL = [NSURL fileURLWithPath:soundOneNew]; // output path
        exportSession.outputFileType = AVFileTypeAppleM4A; // output file type
        // perform the export

        NSLog(@"AVAssetExportSessionStatusCompleted");

        [exportSession exportAsynchronouslyWithCompletionHandler:^{

            if (AVAssetExportSessionStatusCompleted == exportSession.status) {
                NSData *newAudiofound = [NSData dataWithContentsOfURL:exportSession.outputURL];
                [newAudiofound writeToFile:[finalURLAudio path] atomically:YES];
                [self playAudio:exportSession.outputURL];

            } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
                // a failure may happen because of an event out of your control
                // for example, an interruption like a phone call comming in
                // make sure and handle this case appropriately
                NSLog(@"AVAssetExportSessionStatusFailed");
            } else {
                NSLog(@"Export Session Status: %d", exportSession.status);
            }
        }];

【问题讨论】:

  • 你好@muhammad 你找到你的问题的解决方案了吗?因为我也面临这个问题。我正在制作一个用于修剪和合并音频文件的应用程序。但是当我在 ios 9 AVAssetExportSessionStatus 上运行应用程序时,总是失败,如果你解决了你的问题,请帮忙。
  • @Nitin 您从我的回答中找到解决方案了吗?
  • 是的,我找到了解决方案..请在下面找到我已经添加了它...如果可行,请投票..:D

标签: ios audio avfoundation


【解决方案1】:
-(void)mergesound{


    NSURL *audio_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"m4a"]];

    AVMutableComposition *composition = [[AVMutableComposition alloc] init];

    CMTime currentTime = kCMTimeZero;


    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionAudioTrack setPreferredVolume:1.0];

    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:audio_url options:nil];
    AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];


    AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:audio_url options:nil];
    AVAssetTrack *clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];


     currentTime = CMTimeAdd(currentTime, avAsset.duration);


    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset1.duration) ofTrack:clipAudioTrack1 atTime:currentTime error:nil];

    AVAssetExportSession *exportSession = [AVAssetExportSession
                                           exportSessionWithAsset:composition
                                           presetName:AVAssetExportPresetAppleM4A];

    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    NSString *outputFilePath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"FinalVideo.m4a"]];

    if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
        [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];

    exportSession.outputURL = [NSURL fileURLWithPath:outputFilePath]; // output path
    exportSession.outputFileType = AVFileTypeAppleM4A; // output file type
    // perform the export

    NSLog(@"AVAssetExportSessionStatusCompleted");

    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (AVAssetExportSessionStatusCompleted == exportSession.status)
        {
            NSData *newAudiofound = [NSData dataWithContentsOfURL:exportSession.outputURL];
            [newAudiofound writeToFile:outputFilePath atomically:YES];
          //  [self playAudio:exportSession.outputURL];

        } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
            // a failure may happen because of an event out of your control
            // for example, an interruption like a phone call comming in
            // make sure and handle this case appropriately
            NSLog(@"AVAssetExportSessionStatusFailed");
        } else {
            NSLog(@"Export Session Status: %d", exportSession.status);
        }
    }];
}

【讨论】:

    【解决方案2】:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path1;
    NSString *path2;
    
    path1 = [NSString stringWithFormat:@"%@/firstPart.caf",documentsDirectory];
    path2 = [NSString stringWithFormat:@"%@/newRecording.caf",documentsDirectory];
    
    AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:path1] options:nil];
    AVAsset *avAsset2 = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:path2] options:nil];
    
    AVMutableComposition *composition = [[AVMutableComposition alloc] init];
    if (version < 9.0) {
        [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    }
    AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    
    AVAssetTrack *assetTrack1;
    AVAssetTrack *assetTrack2;
    
    if ([avAsset1 tracksWithMediaType:AVMediaTypeAudio].count > 0) {
        assetTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        assetTrack2 = [[avAsset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    }
    
    CMTime insertionPoint = kCMTimeZero;
    
    [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset1.duration) ofTrack:assetTrack1 atTime:insertionPoint error:nil];
    insertionPoint = CMTimeAdd(insertionPoint, avAsset1.duration);
    [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset2.duration) ofTrack:assetTrack2 atTime:insertionPoint error:nil];
    
    NSURL *outPutUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/mergedFilePart1.caf",documentsDirectory]];
    
    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
    
    exportSession.outputURL = outPutUrl;
    
    exportSession.outputFileType = AVFileTypeAppleM4A;
    
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        if (AVAssetExportSessionStatusCompleted == exportSession.status) {
    
            NSLog(@"AVAssetExportSessionStatusCompleted");
            [self mergeAudioPart2];
    
        }
        else if (AVAssetExportSessionStatusFailed == exportSession.status)
        {
    
            NSLog(@"%ld",(long)exportSession.status);
            NSLog(@"AVAssetExportSessionStatusFailed");
        }
        else
        {
            NSLog(@"Export Session Status: %ld", (long)exportSession.status);
        }
    }];
    

    用这个替换你的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      相关资源
      最近更新 更多