scenario
Save and play multiple MP3 audio files as one
The solution
- First generate the entire audio path as an array:
NSMutableArray * fileUrlArr = @[].mutableCopy; [mp3NameArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSString *mp3Name = [NSString stringWithFormat:@"%@",obj]; // MP3 path NSString *audioFileURL = [[NSBundle mainBundle] pathForResource:mp3Name ofType:@"mp3"]; [fileUrlArr addObject:audioFileURL]; }];Copy the code
- Through the following methods merging audio, stored in a random file, because if the existing or file directory write fails, there will be a error code AVAssetExportSessionStatusFailed 】 【
/ / / merge audio - (void) mergeAVAssetWithSourceURLs: (NSArray *) sourceURLsArr completed: (void (^) (nsstrings * outputFileUrlStr)) AVMutableComposition *composition = [AVMutableComposition composition]; __block CMTime beginTime = kCMTimeZero; __block CMTime beginTime = kCMTimeZero; [sourceURLsArr enumerateObjectsUsingBlock:^(id _Nonnull audioFileURL, NSUInteger idx, AVURLAsset *audioAsset1 = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:audioFileURL]]; / / audio track AVMutableCompositionTrack * audioTrack1 = [composition addMutableTrackWithMediaType: AVMediaTypeAudio preferredTrackID:0]; / / get audio material orbital AVAssetTrack * audioAssetTrack1 = [[audioAsset1 tracksWithMediaType: AVMediaTypeAudio] firstObject]; / / audio merger - insert audio track file [audioTrack1 insertTimeRange: CMTimeRangeMake (kCMTimeZero, audioAsset1.duration) ofTrack:audioAssetTrack1 atTime:beginTime error:nil]; BeginTime = CMTimeAdd(beginTime, audioasset1.duration);}]; / / export merged audio file / / audio files currently only find support: m4a type AVAssetExportSession * session = [[AVAssetExportSession alloc] initWithAsset: composition presetName:AVAssetExportPresetAppleM4A]; NSDateFormatter *formater = [[NSDateFormatter alloc] init]; [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss-SSS"]; NSString * timeFromDateStr = [formater stringFromDate:[NSDate date]]; NSString *outPutFilePath = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/sound-%@.mp4", timeFromDateStr]; / / audio file output session. OutputURL = [NSURL fileURLWithPath: outPutFilePath]; session.outputFileType = AVFileTypeAppleM4A; / / with the aforementioned ` present ` session. The corresponding shouldOptimizeForNetworkUse = YES; / / to optimize network [session exportAsynchronouslyWithCompletionHandler: ^ {if (session. Status = = AVAssetExportSessionStatusCompleted) NSLog(@" merge successful ----%@", outPutFilePath); if (completed) {completed(outPutFilePath); if (completed) {completed(outPutFilePath); }} else {/ / other, concrete is described here ` AVAssetExportSessionStatus `. NSLog (@ "merger fail - % ld", (long) session. Status); if (completed) { completed(nil); } } }]; }Copy the code
- Output combined audio
/ / merge audio files are generated new audio [self mergeAVAssetWithSourceURLs: musicArr completed: ^ (nsstrings * outputFileUrlStr) {if (! OutputFileUrlStr) {NSLog(@" Sound generation failed! "); return; } self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:outputFileUrlStr] error:nil]; [self.audioPlayer play]; }];Copy the code
Reference: www.cxyzjd.com/article/ism… www.jianshu.com/p/3e357e312… www.cocoachina.com/articles/17…