如何在AVAudioPlayer中循环播放多个音频文件?

Meh*_*nki 2 iphone loops avaudioplayer ios6

我想通过循环播放多个文件.
我在下面写了代码..
请帮帮我!! !!

soundList = [[NSArray alloc] initWithObjects:@"mySong1.mp3",@"mySong2.mp3",@"mySong3.mp3",@"mySong4.mp3",@"mySong5.mp3",@"mySong6.mp3",@"mySong7.mp3",@"mySong8.mp3",@"mySong9.mp3", nil];
for (i=0; i<=([soundList count] - 1); ) {                
    while(i<[soundList count]){
        NSLog(@"File is : %@",[soundList objectAtIndex:i]);
        mediaPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[soundList objectAtIndex:i] ofType:nil]] error:&error];
        [mediaPlayer setDelegate:self];
        self.lblCurrentSongName.text = [soundList objectAtIndex:i];
        [mediaPlayer prepareToPlay];
        i++;
    }
}
Run Code Online (Sandbox Code Playgroud)

请给我建议.!!

gue*_*nis 5

我打算给你一个指导方针,但要注意它不是经过测试的代码.您提供的代码不会出于多种原因,1.您不会在avaudioplayer 2上调用play.while循环执行速度太快而且歌曲会相互重叠,或者因为您没有将引用存储到以前的avaudioplayer它可能会造成麻烦(不知道确切)

NSInteger currentSoundsIndex; //Don't forget to set this in viewDidLoad or elsewhere

//In viewDidLoad add this line
{
...
currentSoundsIndex = 0;
...
}

-(void) playCurrentSong
{
NSError *error;
mediaPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[soundList objectAtIndex:currentSoundsIndex] ofType:nil]] error:&error];
if(error !=nil)
{
   NSLog(@"%@",error);
   //Also possibly increment sound index and move on to next song
}
else
{
self.lblCurrentSongName.text = [soundList objectAtIndex:currentSoundsIndex];
[mediaPlayer setDelegate:self];
[mediaPlayer prepareToPlay]; //This is not always needed, but good to include
[mediaPlayer play];
}

}

//This is the delegate method called after the sound finished playing, there are also other methods be sure to implement them for avoiding possible errors
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
//Increment index but don't go out of bounds
currentSoundsIndex = ++currentSoundsIndex % [soundList count];
[self playCurrentSong];
}
Run Code Online (Sandbox Code Playgroud)