如何连接iOS中2或3的音频文件? [英] How to concatenate 2 or 3 audio files in iOS?

查看:120
本文介绍了如何连接iOS中2或3的音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C的新人和有经验的iPhone开发的只有5个月。

I am newcomer in Objective-C and have experience only 5 months in iPhone development.

我需要什么:结果
我需要连接2个或多个音频文件合并成一个,和出口的结果AIFF,MP3,CAF或M4A格式。

What I need:
I need to concatenate 2 or more audio files into one, and export result as aiff, mp3, caf or m4a format.

例如:结果
含你需要一个音频文件,第二个下载,第三文件。结果
每个音频部分依赖于从用户操作。

For example:
First audio file containing "You need", second "download" and third "document".
Every audio part depends on actions from user.

我花了2天没有运气。那个地方是我最后的边疆。

I spent 2 days without luck. That place is my last frontier.

我会非常AP preciate了一块code的。

I will very appreciate for a piece of code.

推荐答案

code以下可用于合并的音频文件。

Code below can be used to merge audio files.

输入文件:文件的ID在阵列audioIds提供。
    例如。 audio1.mp3,audio2.mp3 ... audioN.mp3是在文档文件夹中
输出文件:combined.m4a

Input files: Ids of the files to be supplied in array audioIds. Eg. audio1.mp3, audio2.mp3 … audioN.mp3 to be available in documents folder Output file: combined.m4a

     - (BOOL) combineVoices {

       NSError *error = nil;
       BOOL ok = NO;


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


       CMTime nextClipStartTime = kCMTimeZero;
        //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.
        AVMutableComposition *composition = [[AVMutableComposition alloc] init];

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

     for (int i = 0; i< [self.audioIds count]; i++) {
        int key = [[self.audioIds objectAtIndex:i] intValue];
        NSString *audioFileName = [NSString stringWithFormat:@"audio%d", key];

        //Build the filename with path
        NSString *soundOne = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp3", audioFileName]];
        //NSLog(@"voice file - %@",soundOne);

        NSURL *url = [NSURL fileURLWithPath:soundOne];    
        AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
        NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
        if ([tracks count] == 0) 
            return NO;
        CMTimeRange timeRangeInAsset = CMTimeRangeMake(kCMTimeZero, [avAsset duration]);
        AVAssetTrack *clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        ok = [compositionAudioTrack insertTimeRange:timeRangeInAsset  ofTrack:clipAudioTrack atTime:nextClipStartTime error:&error];
        if (!ok) {
            NSLog(@"Current Video Track Error: %@",error);
        }
        nextClipStartTime = CMTimeAdd(nextClipStartTime, timeRangeInAsset.duration);
    }

    // create the export session
    // no need for a retain here, the session will be retained by the
    // completion handler since it is referenced there
    AVAssetExportSession *exportSession = [AVAssetExportSession
                                           exportSessionWithAsset:composition
                                           presetName:AVAssetExportPresetAppleM4A];
    if (nil == exportSession) return NO;

    NSString *soundOneNew = [documentsDirectory stringByAppendingPathComponent:@"combined.m4a"];
    //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
    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (AVAssetExportSessionStatusCompleted == exportSession.status) {
            NSLog(@"AVAssetExportSessionStatusCompleted");
        } 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);
        }
    }];

    return YES;
}

这篇关于如何连接iOS中2或3的音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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