检测音频文件是否正在播放 - iphone

发布于 2024-09-27 23:21:31 字数 479 浏览 0 评论 0原文

我正在使用调用方法的 UIButton 播放音频剪辑(请参阅下面的代码)。我试图弄清楚如何编写一个语句来检测音频是否正在运行,如果音频文件正在播放,则禁用/隐藏 UIButton。

此时,如果您继续触摸播放按钮,则会播放音频剪辑的多个实例。

-(void) audioMethod {


NSString *path = [[NSBundle mainBundle] pathForResource:@"affs" ofType:@"mp3"];

theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

theAudio.delegate=self;

theAudio.numberOfLoops = -1;
[theAudio prepareToPlay];

[theAudio play];

}

感谢您的帮助

I'm playing an audio clip using a UIButton that calls a method (see code below). I'm trying to figure out how to write a statement to detect if the audio is running, and if the audio file is playing then disable/hide the UIButton.

At the moment if you keep touching the play button multiple instances of the audio clip play.

-(void) audioMethod {


NSString *path = [[NSBundle mainBundle] pathForResource:@"affs" ofType:@"mp3"];

theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

theAudio.delegate=self;

theAudio.numberOfLoops = -1;
[theAudio prepareToPlay];

[theAudio play];

}

thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2024-10-04 23:21:31

您只需将调用类声明为 AVAudioPlayerDelegate (正如我看到您所做的那样)。

然后编写一个方法:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player 
                       successfully:(BOOL)flag {

    myButton.enabled = YES;

}

并在开始播放声音时禁用该按钮:

...
theAudio.numberOfLoops = -1;
[theAudio prepareToPlay];

myButton.enabled = NO;

[theAudio play];

You simply declare the calling class as AVAudioPlayerDelegate (as I see you have done).

Then you write a method:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player 
                       successfully:(BOOL)flag {

    myButton.enabled = YES;

}

and disable the button when you start playing the sound:

...
theAudio.numberOfLoops = -1;
[theAudio prepareToPlay];

myButton.enabled = NO;

[theAudio play];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文