2013-03-28 55 views
0

我在plist中播放了一些歌曲,并想用AVAudioplayer一个接一个地播放它们。 但是,当第一首歌曲结束时,它停止。如何用下一首歌曲开始播放? 我已经尝试了一个循环来计算plist中的下一个数字,但它不起作用。玩家在第一轮后停止。这应该很简单,但如何? 这是我的代码的一部分。像我一样可以使用循环吗?如何播放AVAudioplayer中的多个文件

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; 
} 
+0

地方audioPlayerDidFinishPlaying方法和使用该方法增加歌曲的逻辑。 – skyline 2013-03-28 09:18:18

+0

可能的重复http://stackoverflow.com/questions/8102201/how-to-play-multiple-audio-files-in-a-row-with-avaudioplayer – 2013-03-28 09:25:24

+0

我试图把n ++放在audioPlayerDidFinishPlaying中,但下次运行它时它从头开始。现在我有了我在viewDidLoad中编写的代码,是吗?我如何知道Audioplayer已停止?我有一个播放按钮,并假设歌曲结束时它已停止播放?猜猜这里有什么东西丢失 – 2013-03-28 09:44:56

回答

1

尝试这样it'l帮助你,

-(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]; 
} 
在上面的代码 sound.duration

给出特定歌曲的一个时间间隔,并基于该那首歌结束后,您可以通过调用该方法使用定时器。如果最后一首歌曲停止播放NSTimer

编辑:

-(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++; 

    } 

    } 
+0

我试图把这个,但它似乎我缺少一些在这里。根本没有任何声音。必须检查它是否曾经使用过这段代码。 – 2013-03-28 09:47:30

+0

用哪种方法获得声音数组。 – Balu 2013-03-28 09:48:51

+0

一旦检查我的更新答案。 – Balu 2013-03-28 09:55:48