2011-08-25 88 views
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]; 

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 

回答

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

現在,玩家扮演的第一首歌。 。下面給出的代表將在歌曲完成後開火。然後播放第二首歌曲。請注意BER你要釋放的第一個球員和初始化新AVAudioPlayer與第二首歌初始化..

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

這個邏輯應該work..I不能找到任何AVAudioPlayer功能改變initializing..So後的歌曲,我們需要創造新的對象.. :(