2011-08-25 7 views
0

왜 내 음악이 겹 칩니 까? 하나씩 재생할 수있는 방법이 있습니까? .H에서소리가 겹침

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

}

:

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

지금 플레이어가 먼저 노래를한다. . 그리고 주어진 대리인 아래 노래가 완료되면 시작됩니다 .. 그럼 두 번째 노래를 연주 .. 기억

- (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은 우리가 필요로하는 initializing..So 후 노래를 변경할 수 AVAudioPlayer의 모든 기능을 찾을 수 없습니다해야 .. 새로운 AVAudioPlayer 첫번째 선수를 해제하고 초기화해야하고 두 번째 노래 초기화하기 BER 새 개체를 만들려면 .. :(