如果第一个声音完成,我想播放第二个调用

时间:2013-10-22 04:19:01

标签: ios objective-c xcode

这是我的下一个和上一个按钮功能。在这里我想播放第一个声音,然后想要播放第二个等等。

出现问题:第一个声音被中断,第二个声音被调用播放。理想情况下,我希望第二个声音仅在第一个声音完成时启动。

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

  }
}

帮帮我。

1 个答案:

答案 0 :(得分: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
}