2013-10-22 80 views
0

这是我的下一个和上一个按钮功能。在这之后我想播放第一个声音,然后想要播放第二个声音等等。我想播放,如果第一个声音完成调用播放第二个

问题发生:第一个声音被中断,第二个声音被调用来播放。理想情况下,我希望第二个声音只有在第一个声音完成时才会启动。

if (counter <= [textStr count]) { 
    counter++; 
    NSLog(@"%i", counter); 

    [stgImage1 setImage:[UIImage imageNamed:[imageStr objectAtIndex:counter]]]; 
    stgText1.text= [NSString stringWithFormat:@"%@", [textStr objectAtIndex:counter]]; 

    if (counter == 0) { 
    stgImage1.frame = CGRectMake(300, 120, 263, 263); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"eggDance" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 1) { 
    stgImage1.frame = CGRectMake(330, 150, 147, 209); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"clownDance" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 2) { 
    stgImage1.frame = CGRectMake(380, 120, 104, 237); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"drumperDrum" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 3) { 
    stgImage1.frame = CGRectMake(370, 120, 154, 253); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"ladyPlay" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 4) { 
    stgImage1.frame = CGRectMake(330, 60, 241, 298); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"boyPlayBall" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 5) { 
    stgImage1.frame = CGRectMake(350, 90, 144, 262); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"boyPlayBalloons" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 6) { 
    stgImage1.frame = CGRectMake(340, 160, 138, 198); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"boyRideCycle" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 7) { 
    stgImage1.frame = CGRectMake(320, 40, 201, 317); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"girlBlowBubbles" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 8) { 
    stgImage1.frame = CGRectMake(330, 70, 202, 268); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"dollDance" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 
    if(counter == 9) { 
    stgImage1.frame = CGRectMake(320, 100, 241, 245); 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"chickenDance" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID(CFBridgingRetain([NSURL fileURLWithPath:soundPath]), &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    } 

    if (counter >= 10) { 
    counter = 0; 
    [self initArray]; 

    } 
} 

帮帮我。

回答

0

尝试使用AVAudioPlayer

- (void) startPlaying{ 
counter = 0; 
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"eggDance" ofType:@"wav"]; 
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
player.delegate = self; 

[player play]; 
} 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 
{ 
    //start playing next sound 
} 
相关问题