0

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.

memmons
  • 40,222
  • 21
  • 149
  • 183
STRITZ
  • 35
  • 1
  • 8
  • The correct approach is to use an `AVMutableCompositionTrack` to append a file. See my answer below. – memmons May 03 '13 at 13:44

2 Answers2

2

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;

}];
memmons
  • 40,222
  • 21
  • 149
  • 183
  • 1
    The doc says : "Note that you cannot add empty time ranges to the end of a composition track." . Are you sure this is working ? – Xys May 16 '17 at 09:49
0

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.

Community
  • 1
  • 1
Peter M
  • 7,309
  • 3
  • 50
  • 91
  • This is an approach I didn't think of... clever! Thanks Peter! – STRITZ Apr 20 '12 at 20:19
  • One last thing for all who end up viewing this Q&A. I've actually implemented another solution that actually works exactly how I need it to. Peter's answer is actually sufficient, works and is a solid approach. I actually solved my own issue by playing dual tracks, one with the audio and another underlying track of 60 seconds of silence. I then call "audioDidFinishPlaying" on my silent audio track to get the rest of my job done. Hope this helps any readers! – STRITZ Apr 20 '12 at 23:23