AVAudioPlayer audio files not syncing when mixing - iPhone

I am trying to play 6 separate .mp3 sound files using instances of AVAudioPlayer. Sounds play at the same time, but they seem to lose synchronization or at slightly different speeds. Does anyone know why this could be?

This is how I initialize the sound:

 NSURL *musicurl = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource: @"SoundLoop" ofType: @"mp3"]];
music = [[AVAudioPlayer alloc] initWithContentsOfURL: musicurl error: nil];
[musicurl release];
music.numberOfLoops = -1; // Loop indefinately
music.currentTime = 0; // start at beginning
music.volume = 1.0;
music.meteringEnabled = YES;

and here is my AudioSession code:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];     
[[AVAudioSession sharedInstance] setPreferredHardwareSampleRate:44100 error:nil];
[[AVAudioSession sharedInstance] setPreferredIOBufferDuration:30 error:nil];

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
Float32 hardvol = 1.0;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, sizeof(hardvol), &hardvol);
UInt32 doSetProperty = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive:YES error: nil]; 

Could this have anything to do with the speed of sound transmission? or what am I using .mp3?

Thank.

+3
source share
2 answers

I found a solution to this problem using the playAtTime method: class AVAudioPlayer:

   NSTimeInterval shortStartDelay = 0.5;            // seconds
    NSTimeInterval now = player.deviceCurrentTime;

    [player playAtTime:now + shortStartDelay];  //these players are instances of AVAudioPlayer
    [player2 playAtTime:now + shortStartDelay];
    [player3 playAtTime:now + shortStartDelay];

Using this option will allow sounds to play asynchronously and synchronously.

+5
source

MultiMedia. " " . http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/Introduction/Introduction.html

, :

, . , MP3, , MP3 . AAC ALAC . iPod AAC MP3 , ; AAC, ALAC MP3- .

iPod , PCM () IMA4 () .

, , , , , Apple " ". , , , .

iOS 3.0, , , , , 1-1. , PCM () IMA4 () .

, , . 6 , , . ( ) , Apple (PCM IMA4), . , .

+1

All Articles