Neha Dangui
Neha Dangui

Reputation: 647

stop the AVAudioPlayer on viewWillDisappear

I am working on an app in Xcode which uses AVAudioPlayer.

@property (nonatomic, strong) AVAudioPlayer *player;

on tap of an image I call a function which plays the audio file:

-(void)playMusic{
        if(self.player.currentTime == 0.0){
            self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.mp3", [[NSBundle mainBundle] resourcePath]]] error:nil];
            self.player.numberOfLoops = 0;
            [self.player play];
        }
}

on viewWillDisappear I need to stop the player, so I call:

-(void)viewWillDisappear:(BOOL)animated{
    [self.player stop];
} 

But the above code does not work, and the audio does not stop playing.

Can anyone please tell me what the issue could be ?

Upvotes: 0

Views: 540

Answers (1)

codeIgnitor
codeIgnitor

Reputation: 765

Why don't you stop Playing song with same image tap as,

   if([player isPlaying] )
    {
        [player pause];
    }
    else
    [player play];

Upvotes: 1

Related Questions