Chelsea Wang
Chelsea Wang

Reputation: 629

play a set of sounds one by one using AVAudioPlayer

I want to use AVAudioPlayer in my app. The logic is: firstly play a set of sounds at the same time and then play another set of sounds.But i found that the app only play all these sets together and cannot play one by one set.Being a newer,i found it is diffidult to me, anyone have ideas? Thank u for ur help and ideas.

Upvotes: 0

Views: 677

Answers (1)

DShah
DShah

Reputation: 9866

Use Delegate method to recognized finishing of the first song then in that method start new song.

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
   //play next song from List/Array of songs.
}

for playing song AVAudioPlayer code goes as below.

NSString* filename = [soundsList objectAtIndex:YOURINDEXNUMBER];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"mp3"];  

AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded

[newAudio release]; // release the audio safely

theAudio.delegate = self; 
[theAudio prepareToPlay];
[theAudio setNumberOfLoops:0];
[theAudio play];

Upvotes: 1

Related Questions