本文介绍了如何在AVAudioPlayer中循环播放多个音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过循环播放多个文件。 我在下面编写代码.. 请帮助我。!! ..
I want to play multiple files by using loop. I written code below.. Please help me.!!..
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++; } }请给我建议。!! ..
Please give me suggestion .!!..
推荐答案我打算给你一个指导方针,但要注意它不是经过测试的代码。您提供的代码不会出于多种原因,1。您不会在avaudioplayer 2上调用play。while循环执行速度太快而且歌曲会相互重叠,或者因为您没有将引用存储到以前的avaudioplayer它可能会造成麻烦(不确切知道)
I'm going to give you a guideline doing it, but beware it is not tested code. The code you gave will not work for a number of reasons, 1. you don't call play on avaudioplayer 2. while loop will execute too fast and songs will overlap with each other or since you don't store the reference to previous avaudioplayer it might create trouble (don't know exactly)
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]; }更多推荐
如何在AVAudioPlayer中循环播放多个音频文件?
发布评论