目标 C:音频播放完毕时捕获事件

发布于 2024-12-06 16:05:23 字数 778 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(2

扶醉桌前 2024-12-13 16:05:23

不确定 AudioService API 是否可行,但您可以使用 AVAudioPlayer 代替,并分配一个实现 AVAudioPlayerDelegate 协议的委托,并在 audioPlayerDidFinishPlaying:successively:< 时执行某些操作:< /code> 被调用。

请记住添加 AVFoundation 框架。

示例:

在您的接口定义中:

#import <AVFoundation/AVFoundation.h>

...

@inteface SomeViewController : UIViewController <AVAudioPlayerDelegate>

...

@propety(nonatomic, retain) AVAudioPlayer *player;

在您的实现中:

@synthesize player;

...

// in some viewDidLoad or init method
self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:NULL] autorelease];
self.player.delegate = self;

...

- (void)pressButton:(id)sender {
  // set play image
  [self.player stop];
  self.player.currentTime = 0;
  [self.player play];
}

...

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
   // set finish image
}

...

# in some dealloc or viewDidUnload method
self.player = nil;

Not sure if it's possible with the AudioService API but you can use AVAudioPlayer instead and assign a delegate that implements the AVAudioPlayerDelegate protocol and does something when audioPlayerDidFinishPlaying:successfully: is called.

Remember to add the AVFoundation framework.

Example:

In your interface definition:

#import <AVFoundation/AVFoundation.h>

...

@inteface SomeViewController : UIViewController <AVAudioPlayerDelegate>

...

@propety(nonatomic, retain) AVAudioPlayer *player;

In your implementation:

@synthesize player;

...

// in some viewDidLoad or init method
self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:NULL] autorelease];
self.player.delegate = self;

...

- (void)pressButton:(id)sender {
  // set play image
  [self.player stop];
  self.player.currentTime = 0;
  [self.player play];
}

...

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
   // set finish image
}

...

# in some dealloc or viewDidUnload method
self.player = nil;
不必你懂 2024-12-13 16:05:23

对于 AudioService API,您可以使用 AudioServicesAddSystemSoundCompletion

It is possible with the AudioService API, you want to use AudioServicesAddSystemSoundCompletion.

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