0

我在标签中显示从 5 到 1 的倒数计时器。当我的倒数计时器显示时,我试图在特定标签的背景中播放音频,即如果显示 5,则应播放 5 的音频声音,如果 1 秒后显示 4,应该播放4的音频等等。我已经完成了代码,但问题是5、4、3、2、1的所有音频同时播放。这是我的代码。

-(void)viewWillAppear:(BOOL)animated
{
    countDown = 7;
    exitcountdown = 0;
    lblCountdown.hidden = YES;
    btnCancel.hidden = YES;
    lblCancel.hidden = YES;
    countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
    [countDownTimer fire];


}





-(void)updateTime:(NSTimer*)timerParam
{

    countDown--;
    if (countDown==5)
    {
        lblCountdown.hidden = NO;
        lblCountdown.text = [NSString stringWithFormat:@"%d",countDown];
        NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",countDown] ofType:@"mp3"]; 
                if ([player isPlaying])
        {
            [player stop];
        }
        player= [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
        player.delegate = self;
        [player stop];
        [player setCurrentTime:0.0];
        [player setVolume:100.0];
        [player play];
        lblCancel.hidden = NO;
        btnCancel.hidden = NO;
        lblWarning.hidden = YES;
        lblGps.hidden = YES;

        [countDownTimer fire];
    }
    else if (countDown==0)
    {
        [self sendRequest];
        countDownTimerexit = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(exitBackground:) userInfo:nil repeats:YES];
        [countDownTimerexit fire];
        NSString *phoneLinkString = [NSString stringWithFormat:@"tel://%@", self.emergencyphonenumber];
        NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
        [[UIApplication sharedApplication] openURL:phoneLinkURL];

        [self clearCountDownTimer];
        btnCancel.hidden = YES;
        lblCancel.hidden = YES;

    }
    lblCountdown.text = [NSString stringWithFormat:@"%d",countDown];
}

lblCountdown 是将 countDown 打印为 5、4、3、2、1 的标签。countDown 是一个 int 变量 countDownTimer 是 NSTimer 变量

4

1 回答 1

0

我认为您的代码中发生的事情是在标签上显示文本比播放声音要快。显示 5 并且您在 5 声到达结束计数器变为 4 之前请求播放声音,然后您停止播放器并请求下一个文件。

尝试在播放声音和显示下一个数字之间插入一些延迟。

于 2012-04-05T04:26:24.470 回答