merge Audio files on iPhone

后端 未结 4 1849
心在旅途
心在旅途 2020-11-30 08:06

I wanna merge a .caf file and a .mp3 file to a .mp3 file on iPhone,or I can convert them to .aac and then merge them.How can I do this ? (Just Like Kala OK,I wanna merge

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 08:53

    you can use the this SDAVAssetExportSession with the following code to export many files to AAC:

    -(void)mergeAudioFiles
    {
        NSFileManager * fm = [[NSFileManager alloc] init];
        NSError * error;
        NSArray * filesNames = **NSArray of File Names;
        NSString * filePath = @"Dest File Name";
        NSString * pathToSave =[NSString stringWithFormat:@"%@%@",filePath,@".m4a"];
    
        CMTime startTime = kCMTimeZero;
        AVMutableComposition *composition = [AVMutableComposition composition];
        AVMutableCompositionTrack *compositionAudioTrack =[AVMutableCompositionTrack alloc];
        compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    
        float audioEndTime=0;
        for (NSString *fileName in filesNames) {
            NSURL *audioUrl = [NSURL fileURLWithPath:fileName];
            AVURLAsset *audioasset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];
            CMTimeRange timeRange  = CMTimeRangeMake(kCMTimeZero, audioasset.duration);
            AVAssetTrack *audioAssetTrack= [[audioasset tracksWithMediaType:AVMediaTypeAudio] lastObject];
           [compositionAudioTrack insertTimeRange:timeRange ofTrack:audioAssetTrack atTime:startTime error:&error];
           startTime = CMTimeAdd(startTime, timeRange.duration);
           CMTime assetTime2 = [audioasset duration];
           Float64 duration2 = CMTimeGetSeconds(assetTime2);
           audioEndTime+=duration2;
        }
    
        NSURL *exportUrl = [NSURL fileURLWithPath:pathToSave];
    
        float audioStartTime=0;
        CMTime startTime1 = CMTimeMake((int)(floor(audioStartTime * 100)), 100);
        CMTime stopTime = CMTimeMake((int)(ceil(audioEndTime * 100)), 100);
        CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime1, stopTime);
    
    
        SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:composition];
        encoder.outputFileType = AVFileTypeAppleM4A;
        encoder.outputURL = exportUrl;
        encoder.audioSettings = @
        {
           AVFormatIDKey: @(kAudioFormatMPEG4AAC),
           AVNumberOfChannelsKey: @2,
           AVSampleRateKey: @44100,
           AVEncoderBitRateKey: @128000,
        };
        encoder.timeRange = exportTimeRange;
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        NSLog(@"Starting Audio Marge");
       [encoder exportAsynchronouslyWithCompletionHandler:^
       {
            if (encoder.status == AVAssetExportSessionStatusCompleted)
            {
                NSLog(@"Audio Marge succeeded");
                NSError * err = NULL;
                BOOL result = [fm moveItemAtPath:pathToSave toPath:filePath error:&err];
                if(!result) {
                    NSLog(@"Error: %@", err);
                }
                NSLog(@"Audio Copied");
            } else if (encoder.status == AVAssetExportSessionStatusCancelled) {
                NSLog(@"Audio export cancelled");
            } else {
               NSLog(@"Audio export failed with error: %@ (%ld)", encoder.error.localizedDescription, encoder.error.code);
            }
            dispatch_semaphore_signal(semaphore);
       }];
       NSLog(@"Audio Wait to Finish");
       dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
      //cleanup
       for (NSString *fileName in filesNames) {
           [fm removeItemAtPath:fileName error:&error];
       }
       NSLog(@"Audio Marge Finished");
    }
    

提交回复
热议问题