How to make an avaudioplayer to stop playing previous song while clicking on newsong?
I created a view with a button songs.
- When we click on button it goes to a table which contains list of songs.
- When we select a song it goes to another view and plays the song.
- When开发者_如何学Pythonever the song is playing we can go back to main view or to the table of songs the song will be playing in background its fine upto here.
But whenever I try to play another song from the table of songs the previous song is not stopped and it continues to play along with the selected song.
My aim is play the song when it is switched to a different view. I have done this one but what I want is whenever I select another song from the table of songs it must stop the previous song and play the selected song.
Please solve this problem.
You have to stop the avaudioplayer
and recreate the instance after releasing the existing one.
- (IBAction)buttonClicked
{
if([audioPlayer isPlaying])
{
[audioPlayer stop];
[audioPlayer release];
audioPlayer = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfUrl:songUrl];
[audioPlayer play];
}
// Do something like change view etc.
}
If u are selecting from a table view put this in the UITableView delegate
didSelectRowAtIndexPath
:
精彩评论