Search code examples
iosobjective-cavaudioplayer

AVAudioPlayer more then 2 sounds


So i tried by all guides that provided here to play at same time 2 sounds by AVAudioplayer + AVA , AVA + System Sound and it didn't worked. Second sound never sounds.

So i decided to stop main audio before i play 2. audio (the bell audio) and it didn't worked.

I need to play 2 sounds at the same time or when 1. audio will be paused.(I have delegate in my h file)

Here is code , this method called by a NSTimer every 0.5 seconds:

if(!(bellint == -0.5)){
    bellint = bellint+0.5;
}
if(!(self.loaderAudioView.fakeDuration == secondsTime)){
    secondsTime = secondsTime +0.5;
    self.loaderAudioView.fakeTimer = secondsTime ;
    NSLog(@"fakedur %f , secondsOfTimer : %f" ,self.loaderAudioView.fakeTimer,secondsTime);
}
else{
    self.loaderAudioView.fakeTimer = secondsTime = 0 ;
    [self.timer invalidate];
    self.playOrPauseButton.on = NO;
}
if((int)secondsTime % self.bellsInt == 0.0){
    [self.loaderAudioView pausePlayer];
     bellint = bellint+0.5;

    NSURL *path = [[NSBundle mainBundle] URLForResource:@"bell_start" withExtension:@"mp3"];
    // I've putted here AVAudioplayer initialization cause you can see. It's in another method realized.Here I just call [objAudio play];

    AVAudioPlayer* objAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];

    objAudio.delegate = self;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [objAudio prepareToPlay];
    [objAudio play];

    NSLog(@"BANG");
}
if(bellint == 5.0){
    [self.loaderAudioView playPlayer];

    bellint = -0.5;
}

Solution

  • So after i placed my objAudio to h file and initialized

    self.objAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:path error:nil];
    

    Everything became as should be.


    I don't know why it didn't worked like that.
    Is it bug or there a certain reason.
    If anyone knows why or have a theory I welcome.