Besi Besi - 1 year ago 110
iOS Question

Play a short sound in iOS

I guess I could use

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?

Answer Source

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.

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

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

See the Audio UI Sounds example.


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

- (void) playMySoundLikeRightNowReally {

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

For completeness:
add AudioToolbox.framework
#import <AudioToolbox/AudioToolbox.h>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download