Gizmodo
Gizmodo

Reputation: 3222

MPMusicPlayerController Volume Fade

I have:

 MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];

When using the app, I need to bring down the music to sound an alarm and when it goes away, I want to bring it up to previous volume. musicVolume here is a double which stores the volume level before fading:

musicVolume = musicPlayer.volume;
if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying)
    [self fadeMusicOut];

This fade the music out well.

To fade it back in:

    - (void) fadeMusicIn
    {
            [musicPlayer play];
            musicPlayer.volume += 0.05;
            if (musicPlayer.volume < musicVolume)
                [self performSelector: @selector(fadeMusicIn) 
                           withObject: nil 
                           afterDelay: 0.1 ];
            else 
            {

            }
    }

This fades music back in well, however, it always bring up the volume LESS THAN what was set before.

Howcan I acheive this to get back to original volume? Thanks in advance.

Upvotes: 1

Views: 1384

Answers (1)

MCKapur
MCKapur

Reputation: 9157

You are saying if (musicPlayer.volume < musicVolume) Which is basically stopping the performSelector: withObject: afterDelay: method from being called, or any method called in that if statement, once the musicPlayer.volume is equal or bigger to the musicVolume. But you want it to be equal. So try:

- (void) fadeMusicIn
{
    ...
        if (musicPlayer.volume == musicVolume)
            [self performSelector: @selector(fadeMusicIn) 
                       withObject: nil 
                       afterDelay: 0.1 ];
    ...
}

This way the if statement stops if musicPlayer.volume is smaller OR equal, so once its equal, it stops

Update, try == instead

Upvotes: 3

Related Questions