在logging的audio文件的末尾添加静音

目前我允许用户录制自己的声音,持续时间不超过30秒。 一旦他们完成录制他们的audio,我抓住他们的audio持续时间。 我运行这个快速math(SixtySeconds-TheirAudioDuraion)= TimeNeededToFill。 基本上我最终需要一个精确的1分钟的赛道。 其中一部分是实际的audio,其余部分是无声audio。 我目前使用AVAudioPlayer做我所有的录音。 有没有一个程序化的方式来实现这一点与一些暴力破解,我开始无声的音轨文件一起创build一个单一的文件?

简单的辉煌需要,将不胜感激。

我最好的。

我会有一个60秒钟的沉默logging已经“录制”,附加到用户录制,然后修剪总长度为60秒。

这样的问题avaudiorecorder-avaudioplayer-append-recording-to-file有一些参考在Siddarth的答案中追加声音文件。

这个问题trim-audio-with-ios有关于修剪声音文件的信息。

这可以使用AVMutableComposionTrack insertEmptyTimerange相当容易地完成。

 // Create a new audio track we can append to AVMutableComposition* composition = [AVMutableComposition composition]; AVMutableCompositionTrack* appendedAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; // Grab the audio file as an asset AVURLAsset* originalAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:originalAudioPath] options:nil]; NSError* error = nil; // Grab the audio track and insert silence into it // In this example, we'll insert silence at the end equal to the original length AVAssetTrack *originalTrack = [originalAsset tracksWithMediaType:AVMediaTypeAudio]; CMTimeRange timeRange = CMTimeRangeMake(originalAsset.duration, originalAsset.duration); [appendedAudioTrack insertEmptyTimeRange:timeRange]; if (error) { // do something return; } // Create a new audio file using the appendedAudioTrack AVAssetExportSession* exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetAppleM4A]; if (!exportSession) { // do something return; } NSString* appendedAudioPath= @""; // make sure to fill this value in exportSession.outputURL = [NSURL fileURLWithPath:appendedAudioPath]; exportSession.outputFileType = AVFileTypeAppleM4A; [exportSession exportAsynchronouslyWithCompletionHandler:^{ // exported successfully? switch (exportSession.status) { case AVAssetExportSessionStatusFailed: break; case AVAssetExportSessionStatusCompleted: // you should now have the appended audio file break; case AVAssetExportSessionStatusWaiting: break; default: break; } NSError* error = nil; }];