Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play a short sound in iOS

Tags:

ios

audio

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?

like image 515
Besi Avatar asked Apr 26 '12 07:04

Besi


People also ask

How do you make a lost iPhone beep?

Play a soundOpen the Find My app. Choose the Devices tab or the Items tab. Select your missing device or item, then choose Play Sound. If your device is offline, it won't play a sound until it connects to a network.

How do I change audio settings in IOS?

Go to Settings > Accessibility > Audio/Visual > Headphone Accommodations. Tap Custom Audio Setup. Follow the instructions on your screen. When finished, tap Use Custom Settings to apply the custom settings suggested based on your choices.

Can iPhone play two sounds?

Search for the song you want to hear and it will load it in the music player. Repeat the same process for the other music player too. Now when you hit the play button, the top music player will play the chosen song in the left earbud while the bottom one will play it in the right earbud.


2 Answers

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.

Basically:

@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 

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

like image 143
bbum Avatar answered Sep 17 '22 11:09

bbum


For short sound clips (less than 30 secs), there's a SystemSounds library which is really nice.

Pros: You don't need to manage volume settings separately. The sound is played in a separate thread and loading and playing of audio clip is v fast. In short, you treat this clip as another system sound.

Cons: You can't provide a separate audio control setting. It's tied to the settings of the system sounds. You can't play more than 30 seconds. You probably can't apply any sound filters to enhance audio effect.

There are certainly more pros and cons, but these are some I could think of, off the top of my head.

use this import: <AudioToolbox/AudioToolbox.h> Add the AudioToolbox Framework then call the below method like [self playSound], wherever you want to play the clip.

-(void) playSound {     NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"changeTrack" ofType:@"aif"];     SystemSoundID soundID;     AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);     AudioServicesPlaySystemSound (soundID);     [soundPath release]; } 
like image 22
Himanshu Singh Avatar answered Sep 20 '22 11:09

Himanshu Singh