Skip to content

Instantly share code, notes, and snippets.

@stevenhuey
Created June 30, 2012 13:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenhuey/3023781 to your computer and use it in GitHub Desktop.
Save stevenhuey/3023781 to your computer and use it in GitHub Desktop.
Creating our AVMutableComposition and AVPlayer
// Setup
_composition = [AVMutableComposition composition];
_audioMixValues = [[NSMutableDictionary alloc] initWithCapacity:0];
_audioMixTrackIDs = [[NSMutableDictionary alloc] initWithCapacity:0];
// Insert the audio tracks into our composition
NSArray* tracks = [NSArray arrayWithObjects:@"track1", @"track2", @"track3", @"track4", nil];
NSString* audioFileType = @"wav";
for (NSString* trackName in tracks)
{
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:trackName ofType:audioFileType]]
options:nil];
AVMutableCompositionTrack* audioTrack = [_composition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
NSError* error;
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]
atTime:kCMTimeZero
error:&error];
if (error)
{
NSLog(@"%@", [error localizedDescription]);
}
// Store the track IDs as track name -> track ID
[_audioMixTrackIDs setValue:[NSNumber numberWithInteger:audioTrack.trackID]
forKey:trackName];
// Set the volume to 1.0 (max) for the track
[self setVolume:1.0f forTrack:trackName];
}
// Create a player for our composition of audio tracks. We observe the status so
// we know when the player is ready to play
AVPlayerItem* playerItem = [[AVPlayerItem alloc] initWithAsset:[_composition copy]];
[playerItem addObserver:self
forKeyPath:@"status"
options:0
context:NULL];
_player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment