1

I have some songs in a plist and want to play them one after the other using AVAudioplayer. But when the first song ends it stops. How do I start the player with next song? I have tried a loop which counts to next number in the plist but it does not work. The player stops after first round. This should be simple but how? This is parts of my code. Is it is possible to use a loop as I do?

    NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist"  
ofType:@"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];


 for (int n = 1; n<=5;n++) {
NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

 sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

sound.delegate = self;
 }
Lars -
  • 501
  • 1
  • 10
  • 27
  • place audioPlayerDidFinishPlaying method and use your logic of incrementing song in that method. – skyline Mar 28 '13 at 09:18
  • Possible duplicate of http://stackoverflow.com/questions/8102201/how-to-play-multiple-audio-files-in-a-row-with-avaudioplayer – Arpit Kulsreshtha Mar 28 '13 at 09:25
  • I tried to put n++ in audioPlayerDidFinishPlaying but next time running it, it starts from beginning. Now I have the code I wrote in viewDidLoad, is that right? How do I know the Audioplayer has stopped? I have a play button and suppose it has stopped when the song ends? Guess something is missing here – Lars - Mar 28 '13 at 09:44

1 Answers1

1

try like this it'l helps you,

-(void)viewDidLoad{

 NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist"  
ofType:@"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];
[self AddSongToAvplayer];
}

-(void)AddSongToAvplayer{

NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

 sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

sound.delegate = self;
n++;


 if(n<=[sound count])
    [NSTimer scheduledTimerWithTimeInterval:sound.duration target:self selector:@selector(AddSongToAvplayer) userInfo:nil repeats:NO];
}

in the above code sound.duration gives the timeInterval of that particular song and based on that after completion of that song you can call that method by using the timer .if the last song coming then stop that NSTimer.

EDIT:

-(IBAction)AddSongToAvplayer{
        if(n<=[sound count]){

    NSString *filename = [soundsList objectAtIndex:n]; //n is number in plist, counting upwards in a for...loop


    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];

     sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];

    sound.delegate = self;
    n++;

    }

    }
Balu
  • 8,470
  • 2
  • 24
  • 41
  • I tried to put in this but it seems I am missing some here. Does not get any sound at all. Must check if it ever uses that piece of code. – Lars - Mar 28 '13 at 09:47
  • in which method you are getting the array of sounds. – Balu Mar 28 '13 at 09:48
  • I have the array of sounds in viewDidLoad, picking them up from the plist. But putting the array in the together in the new location as you suggested does not help – Lars - Mar 28 '13 at 09:58
  • Thank you, it does work now. Had some trouble with the number n. It was solved with property and @synthesize – Lars - Mar 28 '13 at 10:15
  • Yes it works well, thanks again. But one more on my wish list is to get them play one after the other automatically without any further user input. Just the first start with a tap at a button. Now it stops after the first and one have to tap the button again to get next song in the list. I did some experimenting here but ended up at playing all songs simultaneously. – Lars - Mar 28 '13 at 10:54
  • No difference, it plays only one at the time. Guess it depends how I start the player. Now I have a IBAction connected to a button and there it says [sound play]. How shall I start the player then? – Lars - Mar 28 '13 at 11:14
  • add [sound prepareToPlay]; and [sound play];. – Balu Mar 28 '13 at 11:16
  • No, it does one loop or as many I want but only one sound at the time. To get next sound in the list I have to press the button once again. – Lars - Mar 28 '13 at 11:21