为什么我的音乐重叠?有没有办法一个一个地玩?
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *array = [NSMutableArray array];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Master of Puppets" ofType:@"mp3"] atIndex:0];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"] atIndex:1];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Start" ofType:@"caf"] atIndex:2];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"thx" ofType:@"m4v"] atIndex:3];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"aif"] atIndex:4];
for (int i=0; i < [array count]; i++)
{
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[array objectAtIndex:i]] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];
}
}
在.h:
@interface STAMP_AudioViewController : UIViewController <AVAudioPlayerDelegate>{
AVAudioPlayer* theAudio;
}
@end
答案 0 :(得分:3)
从AVAudioPlayerDelegate实施audioPlayerDidFinishPlaying委托并从那里开始第二首歌......
NSMutableArray *array ;
int index = 0;
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *array = [NSMutableArray array];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Master of Puppets" ofType:@"mp3"] atIndex:0];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"] atIndex:1];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"Start" ofType:@"caf"] atIndex:2];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"thx" ofType:@"m4v"] atIndex:3];
[array insertObject: [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"aif"] atIndex:4];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[array objectAtIndex:index]] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];
index++;
}
现在播放器播放第一首歌曲......下面给出的代表将在歌曲完成后播放..然后播放第二首歌曲。记住你必须释放第一个播放器并用第二首歌初始化新的AVAudioPlayer和init ..
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[theAudio release];
if(index > 4)//your array highest index
return;
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[array objectAtIndex:index]] error:NULL];
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];
index++;
}
这个逻辑应该工作..我无法在AVAudioPlayer中找到任何函数来在初始化后更改歌曲。所以我们需要创建新对象.. :(