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?
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.
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.
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.
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>
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]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With