3

Possible Duplicate:
merge Audio files on iPhone

I'm trying to combine 2 audio files into one file. For example: file1.mp3 - it says "I am". file2.mp3 - it says "George". I want to get a combined file file3.mp3 which will say "I am George".

How can I do this in objective-C?

Community
  • 1
  • 1
Rendel
  • 1,794
  • 3
  • 13
  • 16
  • i saw that documentation. there is merging and not combine. i want to be the file2 after file1 together in the file3. – Rendel Jul 17 '12 at 09:52
  • 1
    Just a quick note: I wouldn't be using `.mp3` here. It is a license-protected file format, and if your app gets over 1000 users, suddenly you owe royalties to Fraunhofer Gesellschaft. – Richard J. Ross III Jul 17 '12 at 11:42
  • Is it OK if it's only playback? (Using Apple's frameworks) – Nicolas Miari Jul 17 '12 at 12:26
  • You asked about creating a file, not just playback. There is no built-in code or API in iOS to build .mp3 files. You will need to find a huge library somewhere (and perhaps pay royalties). – hotpaw2 Jul 17 '12 at 17:00

3 Answers3

12

Look at AVFoundation framework ... There're many ways, but the simplest one for you can be ...

  • create AVAsset for both files (use AVURLAsset subclass),
  • alloc AVMutableComposition (composition),
  • add AVMutableCompositionTrack with type AVMediaTypeAudio to composition

[composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

  • get track(s) from the first AVAsset and add it AVMutableCompositionTrack,
  • get track(s) from the second AVAsset and append it to AVMutableCompositionTrack,
  • then create AVAssetExportSession with your composition and export it.

Simplified description, but you get a clue. Depends on how many tracks do you have, what kind of effects do you want to use, etc.

If you do want to see some real code, open AVMovieExporter example, copy this code and remove video stuff and leave audio there only.

zrzka
  • 20,249
  • 5
  • 47
  • 73
  • tnx for your answer. can you write some code? i have 2 files: file1,mp3 and file2.mp3. i just need to combine them.(first play file1 then file2) – Rendel Jul 17 '12 at 10:27
  • 13
    Sorry, I'm not going to write code for you. It's your task and I told you how to do it and where to find an example of already written code where you can wipe out video part and you got what do you want. – zrzka Jul 17 '12 at 10:39
  • i've done 2 steps. but i cant add AVMutableCompositionTrack with type AVMediaTypeAudio. it says an error undecleared AVMediaTypeAudio – Rendel Jul 17 '12 at 10:57
  • AVMediaTypeAudio is defined in AVMediaFormat.h. Did you include in your code and did you add AVFoundation framework to your project? – zrzka Jul 17 '12 at 11:04
  • i resolved that. but how i can append or export session? i created tracks already. – Rendel Jul 17 '12 at 11:25
  • Allocate AVAssetExportSession and pass your composition to initWithAsset:presetName: method, choose AVAssetExportPreset, set outputFileType of AVAssetExportSession, set timeRange of AVAssetExportSession, set outputURL (= your new file) and send exportAsynchronouslyWithCompletionHandler: message to AVAssetExportSession. – zrzka Jul 17 '12 at 11:29
  • how to video file add to other animation gif file create together making video objective c – Ramani Hitesh Dec 14 '18 at 13:13
1

You can merge 2 mp3 files as:-

  NSMutableData *part1 = [NSMutableData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"file1" ofType: @"mp3"]];

  NSMutableData *part2 = [NSMutableData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"file2" ofType: @"mp3"]];

 [part1 appendData: part2];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDir = [paths objectAtIndex:0];
    NSString *myMp3 = [documentsDir stringByAppendingPathComponent: @"final.mp3"];

    [part1 writeToFile:myMp3 atomically:YES];

This way you can straight away append mp3 file into one. I have used it in one of my apps. It gets a bit complicated for other formats where you have to update the header of the files respectively, but relatively simpler with mp3.

UPDATE:

Well you must also take care that the bit rates of the mp3 files you are trying to concatenate are same. If they differ they will merge but would play only partially or perhaps not at all. So make sure the bit rates sync with each other.

Apple_iOS0304
  • 1,092
  • 1
  • 9
  • 19
0

One alternative is to use AVAssetReader to convert all the mp3 files into uncompressed audio files, or even memory buffers if the audio is short enough. Uncompressed PCM data can then just be concatenated, mixed, or cross-faded, as needed. Then use AVAssetWriter to write the result into some compressed file format (such as aac) if needed.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153