I want to play multiple songs with single object of AVAudioplayer, I put songs in table row , when user tap on row player view is open but when user go back in other row in table player play both songs simanteniosly . what I Can do to fix this?
2 Answers
I assume that you have created the object in the h file, and have property declared and synthesized it. Also you might be playing the file from didSelectRowAtIndexPath: method. In that method you might have the following section of code.
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
Before [appSoundPlayer prepareToPlay]; add [appsoundPlayer stop]; just to ensure that the audioplayer is in stop state before starting the playing session.
If you are following a different method, just post the relevant part of the code here,
-
You are assuming right but in didSelectRowAtIndexPath: method. i passes the name of song and this name is assign as url to player in its next view. – Abhijeet Barge Apr 07 '10 at 11:04
-
ok.. so you might have a play button there right??? or start playing right from the loading of view?? – Nithin Apr 07 '10 at 11:15
-
i m playing audio at tap event of button. – Abhijeet Barge Apr 07 '10 at 11:19
-
is it possible to do all with one common object of AVAudioPlayer ? if yes then how? – Abhijeet Barge Apr 07 '10 at 11:22
-
have you added the code for stopping the audioplayer? If you have, and still experiencing difficulty, post the code for that button event. Also in my code, appSoundPlayer is the synthesized instance of AVAudioPlayer. – Nithin Apr 07 '10 at 11:22
-
this is i did in didSelectRowAtIndexpath - PlayerViewController *playerView = [[PlayerViewController alloc] initWithNibName:@"PlayerViewController" bundle:nil]; NSUInteger row = [indexPath row]; playerView.FileName = [self.list objectAtIndex:row]; [self.navigationController pushViewController:self.playerView animated:YES]; – Abhijeet Barge Apr 07 '10 at 11:27
-
I've posted my entire Action method @ http://stackoverflow.com/questions/2025461/avaudioplayer-not-releasing-memory/2025538#2025538 – Nithin Apr 07 '10 at 11:29
-
and i assign it in playerview to player as like - NSString *path = [[NSBundle mainBundle] pathForResource:self.FileName ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; – Abhijeet Barge Apr 07 '10 at 11:29
-
The method you followed is not memory efficient. need to create an instance of it in playerviewcontroller.h(AVAudioPlayer *player & @property(nonatomic,retain)AVAudioPlayer *player) , synthesize it in PlayerViewController.m (@synthesize player). You can release it in dealloc method. Here player is similar to appSoundPLayer in my example – Nithin Apr 07 '10 at 11:34
-
I need functionality like singleton class of player. – Abhijeet Barge Apr 07 '10 at 11:37
-
have you followed the code in the link that i posted earlier?? that was working fine for me, – Nithin Apr 07 '10 at 11:41
-
thanks dude for giving me ur time, but this code is not working for me. – Abhijeet Barge Apr 07 '10 at 11:44
An AVAudioPlayer
is meant to play a single audio clip. If you want to play a different clip, stop the current player, make a new one with the new audio data, and play it.
If two songs are overlapping then you're probably accidentally making two AVAudioPlayer
instances and failing to stop the first one.

- 8,092
- 2
- 49
- 55