我想我可以使用AVAudioPlayer
播放声音,但是,我需要的只是播放短声音,并且不需要任何循环或对音量的细粒度控制等。
是否有捷径可寻?
最佳答案
其他答案中的每一个都会泄漏内存(除非为其中一个答案启用了ARC)...奇怪的是,最初标记为正确的答案在没有明显原因的情况下调用了retainCount
。
如果使用alloc/init
,则需要将其释放(除非您使用的是ARC)。
如果调用AudioServicesCreateSystemSoundID()
,则必须处置产生的声音。
请参见Audio UI Sounds示例。
基本上:
@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 <AudioToolbox/AudioToolbox.h>
关于ios - 在iOS中播放简短的声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10329291/