在 iOS 中播放短声音 [英] Play a short sound in iOS

查看:21
本文介绍了在 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.

如果你alloc/init,它需要被释放(除非你使用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.

请参阅音频界面声音示例.

基本上:

@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
#import

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

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