发挥iOS的一个短声 [英] Play a short sound in iOS

查看:149
本文介绍了发挥iOS的一个短声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可以使用 AVAudioPlayer 播放声音,但是,我需要的是只打一短声,我不需要任何循环或细细粒度控制音量等。

I guess I could use AVAudioPlayer to play a sound, however, what I need is to just play a short sound and I don't need any loops or fine-grained control over the volume etc.

有没有一种简单的方法来做到这一点?

Is there an easy way to do this?

推荐答案

其他答案的每一个人......奇怪的是,(除非ARC为的有一个的答案启用)泄漏内存原来答案标记为正确的有没有明显的理由 retainCount 的电话。

Every single one of the other answers leaks memory (unless ARC is enabled for one of the answers)... oddly, the answer originally marked as correct has a call to retainCount for no apparent reason.

如果您分配/初始化的东西,它需要被释放(除非您使用ARC)。

If you alloc/init something, it needs to be released (unless you are using ARC).

如果你调用 AudioServicesCreateSystemSoundID()你必须处理所产生的声音。

If you call AudioServicesCreateSystemSoundID() you have to dispose of the resulting sound.

查看<一个href=\"http://developer.apple.com/library/ios/#sample$c$c/SysSound/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008018\">Audio UI听起来例子。

基本上是:

@interface MyClass:UI*ViewController // fixed
{
     SystemSoundID mySound;
}
@implementation MyClass
- (void) viewDidLoad {
    [super viewDidLoad];
    AudioServicesCreateSystemSoundID(.... URL ...., &mySound);
}

- (void) playMySoundLikeRightNowReally {
    AudioServicesPlaySystemSound(mySound);
}

- (void) dealloc {
   AudioServicesDisposeSystemSoundID(mySound);
   [super dealloc]; // only in manual retain/release, delete for ARC
}
@end

有关完整性:结果
添加AudioToolbox.framework结果
#进口&LT; AudioToolbox / AudioToolbox.h&GT;

这篇关于发挥iOS的一个短声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆