0

I'm trying to get the volume of an AVAudioPlayer to oscillate over time.

Ex: Every 10 seconds, adjust volume to 0.5, then to 1.0, etc.

I have tried using an NSTimer but it only works the first time and doesn't loop.

    oscillateTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(oscillateRun) userInfo:nil repeats:YES];

oscillateRun function

    - (void)oscillateRun {

    BOOL oscillation;
    oscillation = NO;

        if(oscillation = YES) {
            oscillation = NO;
            audioPlayer.volume = 0.50;
        }
        else {
            oscillation = YES;
            audioPlayer.volume = 1;
        }

}

Unsure what to do, thanks in advance!

Mani
  • 17,549
  • 13
  • 79
  • 100
Steven Ritchie
  • 228
  • 1
  • 2
  • 12

1 Answers1

0

Here problem is that every time you are creating new bool variable and it takes every time NO that'why value there is no changes in volume of the AVPlayer.

create oscillation object globally ,

 BOOL oscillation;

call this method like

  [self oscillateRun]; 

 - (void)oscillateRun {

            if(oscillation == YES) {
                oscillation = NO;
                [audioPlayer setVolume : 0.50];
            }
            else {
                oscillation = YES;
                [audioPlayer setVolume : 1.0];
            }
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(oscillateRun) userInfo:nil repeats:YES];
    }

EDIT:-

for camparing the two variables use '==' change this one in if condition then it'l works fine.
Balu
  • 8,470
  • 2
  • 24
  • 41
  • Where am I putting [self oscillateRun] ? I have my NSTimer currently in the audioPlayer function, you recommend moving that to the oscillateRun function? – Steven Ritchie May 07 '13 at 15:04
  • @Steven Ritchie:you can place this one where you want to start the AVPlayer(playing audio). – Balu May 08 '13 at 05:28
  • This still doesn't work. It lowers the volume after the first interval, but does not raise it after another interval. I also use if(oscillation == YES) which was a typo in my initial code – Steven Ritchie May 08 '13 at 20:04
  • @Steven Ritchie:once check my answer and it'l works fine now. – Balu May 09 '13 at 04:47
  • I figured it out on my own, putting the timer in the function was creating multiple instances of the timer in parallel, breaking it. Thanks though! – Steven Ritchie May 17 '13 at 04:53