STRITZ
STRITZ

Reputation: 35

Adding silence to the end of a recorded audio file

Currently I'm allowing user to record their own voice for a duration no longer than 30 seconds. Once they are finished recording their audio, I grab the duration of their audio. I run this quick math (SixtySeconds-TheirAudioDuraion) = TimeNeededToFill. Basically I need to end up with a precise 1-minute track in the end. Some portion of which is actual Audio, the remainder is Silent Audio. Im currently using AVAudioPlayer to do all of my audio recording. Is there a programatic way to accomplish this vs. some brute-force hack where I start cramming silent audio track files together to create a single file?

Simple brilliance needed and would be appreciated.

My Best to all.

Upvotes: 0

Views: 2899

Answers (2)

memmons
memmons

Reputation: 40492

This can be done fairly easily using 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;

}];

Upvotes: 2

Peter M
Peter M

Reputation: 7493

I would have a 60 second recording of silence already "recorded", append that to the users recording, and then trim the total length to 60 seconds.

This SO question avaudiorecorder-avaudioplayer-append-recording-to-file has some references to appending sound files in Siddarth's answer.

This SO question trim-audio-with-ios has information about trimming sound files.

Upvotes: 0

Related Questions