gpt4 book ai didi

iphone - 我如何创建一个循环来检查每次迭代时是否有新的 IBAction 输入?

转载 作者:行者123 更新时间:2023-11-28 23:09:51 25 4
gpt4 key购买 nike

我想制作一个循环来播放声音文件(非常短的哔哔声)并等待时间等于延迟然后重复,这个循环一直运行直到我按下暂停按钮并且可以通过 slider 按钮更改等待时间延迟当循环运行时。

- (void)playSound 
{

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Metronome-Sound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}

- (IBAction)playSelected:(UIButton *)sender
{
if (stateOfMetronome == 0){
stateOfMetronome = 1;
while (stateOfMetronome == 1){
[self playSound];
[NSThread sleepForTimeInterval:delay];
}
}
}


- (IBAction)pauseSeleted:(UIButton *)sender
{
if (stateOfMetronome == 1){
stateOfMetronome = 0;
[audioPlayer pause];
}
}

最佳答案

我认为实现这一点最轻松的方法是使用 NSTimer设置为重复。当您需要停止/暂停它时,只需使计时器无效即可。当你想启动它时,只需重新初始化它。使用此方法,您可以轻松地控制何时延迟触发以及轻松自定义间隔。

此外,请记住,您可能会在休眠线程时阻止所有 UI 交互。这可能意味着您无法在线程休眠时播放/暂停,因为在休眠时没有运行循环处理。查看详情 NSThread .

关于iphone - 我如何创建一个循环来检查每次迭代时是否有新的 IBAction 输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8583238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com