-(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++; } }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...loopNSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"au"];NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path]; sound = [[AVAudioPlayer alloc] initWithContentsOfURL : fileURL error:nil];sound.delegate = self; } 解决方案 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...loopNSString *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++; } } 这篇关于如何在AVAudioplayer中播放多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 08:12