如何重叠音频文件并在Xcode中组合iPhone?
发布时间:2020-12-14 17:53:50 所属栏目:百科 来源:网络整理
导读:所以我有两个音频文件,相同的格式,可能长度不同.我想组合这些文件(将音频从一个叠加到另一个,不要将它们连接在一起). 可以说我有两个文件: 音频文件A,长度30秒,大小220k 音频文件B,长度45秒,大小300k 我想要的是,结合音频文件: 音频文件C,大小300k(我认识
所以我有两个音频文件,相同的格式,可能长度不同.我想组合这些文件(将音频从一个叠加到另一个,不要将它们连接在一起).
可以说我有两个文件: >音频文件A,长度30秒,大小220k 我想要的是,结合音频文件: >音频文件C,大小300k(我认识到这可能更多) 感谢大家的帮助! 解决方法
这是我在我的应用程序中所做的.
- (void) setUpAndAddAudioAtPath:(NSURL*)assetURL toComposition:(AVMutableComposition *)composition { AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil]; AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; NSError *error = nil; BOOL ok = NO; CMTime startTime = CMTimeMakeWithSeconds(0,1); CMTime trackDuration = songAsset.duration; //CMTime longestTime = CMTimeMake(848896,44100); //(19.24 seconds) CMTimeRange tRange = CMTimeRangeMake(startTime,trackDuration); //Set Volume AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track]; [trackMix setVolume:0.8f atTime:startTime]; [self.audioMixParams addObject:trackMix]; //Insert audio into track ok = [track insertTimeRange:tRange ofTrack:sourceAudioTrack atTime:CMTimeMake(0,44100) error:&error]; } - (IBAction)saveRecording { AVMutableComposition *composition = [AVMutableComposition composition]; audioMixParams = [[NSMutableArray alloc] initWithObjects:nil]; //IMPLEMENT FOLLOWING CODE WHEN WANT TO MERGE ANOTHER AUDIO FILE //Add Audio Tracks to Composition NSString *URLPath1 = [[NSBundle mainBundle] pathForResource:@"mysound" ofType:@"mp3"]; NSString *URLPath2 = [[NSBundle mainBundle] pathForResource:@"mysound2" ofType:@"mp3"]; NSURL *assetURL1 = [NSURL fileURLWithPath:URLPath1]; [self setUpAndAddAudioAtPath:assetURL1 toComposition:composition]; NSURL *assetURL2 = [NSURL fileURLWithPath:URLPath2]; [self setUpAndAddAudioAtPath:assetURL2 toComposition:composition]; AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams]; //If you need to query what formats you can export to,here's a way to find out NSLog (@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:composition]); AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: composition presetName: AVAssetExportPresetAppleM4A]; exporter.audioMix = audioMix; exporter.outputFileType = @"com.apple.m4a-audio"; NSURL *exportURL = [NSURL fileURLWithPath:exportFile]; exporter.outputURL = exportURL; // do the export [exporter exportAsynchronouslyWithCompletionHandler:^{ int exportStatus = exporter.status; NSError *exportError = exporter.error; switch (exportStatus) { case AVAssetExportSessionStatusFailed: break; case AVAssetExportSessionStatusCompleted: NSLog (@"AVAssetExportSessionStatusCompleted"); break; case AVAssetExportSessionStatusUnknown: NSLog (@"AVAssetExportSessionStatusUnknown"); break; case AVAssetExportSessionStatusExporting: NSLog (@"AVAssetExportSessionStatusExporting"); break; case AVAssetExportSessionStatusCancelled: NSLog (@"AVAssetExportSessionStatusCancelled"); break; case AVAssetExportSessionStatusWaiting: NSLog (@"AVAssetExportSessionStatusWaiting"); break; default: NSLog (@"didn't get export status"); break; } }]; } 请注意我不久前做过这个,你可能需要一点点工作才能使它工作.但它确实在某一点上起作用.如果您遇到问题,请告诉我. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- cocos2d-x 3.x 中文乱码问题解决
- 正则表达式 – 评估针对perl数组输入的邮政编码
- 为什么大写的属性名称没有在Objective-C / UITouch中出错?
- 利用AJAX是想JSP页面的单个控件的后台校验
- 【SQLite】Android Studio SQLite Database小例
- flex DataGrid设置超链接
- XML(2)通过XmlDocument与XDocument方式写入XML
- C异步OutputDebugString
- stl – 如何在Objective-C std :: map中使用NSString作为键
- reactjs – 如何区分导致React组件渲染的原因:来自组件内部