目标 C:音频播放完毕时捕获事件
我是 Objective C 的新手,我正在开发一个应用程序,可以在 uibutton 的触摸事件上播放声音。 当我触摸按钮时,图像发生变化并且音频开始播放。当音频播放完毕后,我想放置 uibutton 的原始图像。我怎样才能做到这一点?我怎样才能捕捉到音频完成的事件? 我正在使用该代码:
- (SystemSoundID) createSoundID: (NSString*)name
{
NSString *path = [NSString stringWithFormat: @"%@/%@",
[[NSBundle mainBundle] resourcePath], name];
NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
return soundID;
}
在
viewDidLoad
我调用
freq = [self createSoundID: @"freq.wav"];
和在按钮触摸上调用的函数中我使用:
AudioServicesPlaySystemSound(freq);
我该怎么办?谢谢你!
i'm new in Objective C, i'm developing an app that play sound on touch event of an uibutton.
When i touch the button, it change is image and audio starts to playing. When audio is finish to play, i want to put the original image of uibutton. How can i do that? How can i catch the event of the audio that is finish?
i'm using that code:
- (SystemSoundID) createSoundID: (NSString*)name
{
NSString *path = [NSString stringWithFormat: @"%@/%@",
[[NSBundle mainBundle] resourcePath], name];
NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
return soundID;
}
In
viewDidLoad
i call
freq = [self createSoundID: @"freq.wav"];
and in function that is called on button's touch i use:
AudioServicesPlaySystemSound(freq);
How can i do? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定 AudioService API 是否可行,但您可以使用
AVAudioPlayer
代替,并分配一个实现AVAudioPlayerDelegate
协议的委托,并在audioPlayerDidFinishPlaying:successively:< 时执行某些操作:< /code> 被调用。
请记住添加 AVFoundation 框架。
示例:
在您的接口定义中:
...
...
在您的实现中:
...
...
...
...
Not sure if it's possible with the AudioService API but you can use
AVAudioPlayer
instead and assign a delegate that implements theAVAudioPlayerDelegate
protocol and does something whenaudioPlayerDidFinishPlaying:successfully:
is called.Remember to add the
AVFoundation
framework.Example:
In your interface definition:
...
...
In your implementation:
...
...
...
...
对于 AudioService API,您可以使用
AudioServicesAddSystemSoundCompletion
。It is possible with the AudioService API, you want to use
AudioServicesAddSystemSoundCompletion
.