I am using AVAudioPlayer in my project to play different songs, about 3 or 4 minutes longs, but when I play one song and then play a different song they merge. How can I play one song, and then when I play a different song the first one would stop. I'm using a button for each song to play.
Just call [currentPlayer stop] before starting the new player - where currentPlayer is a pointer to the player that is playing now.
I suspect you're creating an AVAudioplayer, starting it, and then not keeping a pointer arround. You should probably have "currentPlayer" as an instance variable and property for your class. If you do that, then this will work:
Code:
//on button click
[currentPlayer stop];
self.currentPlayer = //create a new player here.
For this to work, You need to variable "theAudio1" to be available anywhere in your class- that means you need to declare it in the .h file as an "instance variable."
Code:
//put this between the interface brackets in your .h file
AVAudioPlayer *theAudio1;
Code:
//put this stuff inside your button method
NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"m4a"];
[theAudio1 stop];
[theAudio1 release]; // this prevents us from leaking memory
theAudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio1.delegate = self;
[theAudio1 play];