gpt4 book ai didi

objective-c - 根据计时器提示 AVAudioPlayer 播放

转载 作者:行者123 更新时间:2023-11-29 13:48:37 24 4
gpt4 key购买 nike

我正在尝试设置通过 UISwitch 启用的秒表警报。我希望在定时器启动后 x 秒后触发警报。我有一个 ivar int 变量 (timerCount),它保存已经过去的秒数,我想根据那个 ivar 播放声音。当计时器开始并且我启用开关时,我希望播放器会在 10 秒时开火,但事实并非如此。我知道“if 语句”是罪魁祸首,因为当我删除它时,AVPlayer 将在启用开关时播放 wav 文件。我只需要另一组眼球来观察这个并弄清楚我错过了什么。这是代码片段:

- (IBAction)timerSound{

if (voiceSwitch.on){

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"horn"
ofType:@"wav"];

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];

player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];

if (timerCount == 10)

[self.player play];

}

else {
[self.player stop];
}
}

最佳答案

您正在寻找playAt: .

基于您的示例 –

- (IBAction)valueChanged:(id)sender {
UISwitch *aSwitch = (UISwitch*)sender;
if ( aSwitch.on ) {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"horn"
ofType:@"wav"];

NSURL *fileURL = [NSURL fileURLWithPath:soundFilePath];

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil];

[audioPlayer playAtTime:(audioPlayer.deviceCurrentTime + 3)];
} else {
[audioPlayer stop];
}
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[self.theSwitch setOn:NO animated:YES];

[player release];
}

关于objective-c - 根据计时器提示 AVAudioPlayer 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6099508/

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