- (BOol)combinefiles{ AVMutableComposition *composition = [[AVMutableComposition alloc] init]; AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_InvalID]; [compositionAudioTrack setPreferredVolume:0.8]; Nsstring *soundOne =[[NSBundle mainBundle]pathForResource:@"test1" ofType:@"m4a"]; NSURL *url = [NSURL fileURLWithPath:soundOne]; 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:0.8]; Nsstring *soundOne1 =[[NSBundle mainBundle]pathForResource:@"test2" ofType:@"m4a"]; NSURL *url1 = [NSURL fileURLWithPath:soundOne1]; AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil]; AVAssetTrack *clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero,avAsset.duration) ofTrack:clipAudioTrack1 atTime:kCMTimeZero error:nil]; AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetname:AVAssetExportPresetAppleM4A]; if (nil == exportSession) return NO; exportSession.outputURL = [NSURL fileURLWithPath:[@"test3.m4a" documentDirectory]]; // output path exportSession.outputfileType = AVfileTypeAppleM4A; // output file type // perform the export [exportSession exportAsynchronouslyWithCompletionHandler:^{ if (AVAssetExportSessionStatusCompleted == exportSession.status) { NSLog(@"AVAssetExportSessionStatusCompleted"); } else if (AVAssetExportSessionStatusFailed == exportSession.status) { NSLog(@"AVAssetExportSessionStatusFailed"); } else { NSLog(@"Export Session Status: %ld",(long)exportSession.status); } }]; return YES;}
从上面的代码我们可以将两个音频文件合并为一个,其中两个文件同时播放.但我希望第三个文件一个接一个地播放前两个文件.
请帮我解决这个问题.
解决方法 做了很多研究后我找到了答案..它有效..- (voID)mergeTwoAudiofiles{ AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test1" ofType:@"m4a"]] options:nil]; AVAsset *avAsset2 = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test2" ofType:@"m4a"]] options:nil]; AVMutableComposition *composition = [[AVMutableComposition alloc] init]; [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_InvalID]; AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_InvalID]; AVAssetTrack *assetTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; AVAssetTrack *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]; AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetname:AVAssetExportPresetAppleM4A]; exportSession.outputURL = [NSURL fileURLWithPath:[@"test3.m4a" documentDirectory]]; exportSession.outputfileType = AVfileTypeAppleM4A; [exportSession exportAsynchronouslyWithCompletionHandler:^{ if (AVAssetExportSessionStatusCompleted == exportSession.status) { NSLog(@"AVAssetExportSessionStatusCompleted"); } else if (AVAssetExportSessionStatusFailed == exportSession.status) { NSLog(@"AVAssetExportSessionStatusFailed"); } else { NSLog(@"Export Session Status: %ld",(long)exportSession.status); } }];}总结
以上是内存溢出为你收集整理的concat两个音频文件一个接一个ios全部内容,希望文章能够帮你解决concat两个音频文件一个接一个ios所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)