Play sounds in iPhone SDK?

Does anyone have a snippet that uses an AudioToolBox structure that can be used to play short sound? I would appreciate it if you share it with me and the rest of the community. Wherever I looked, it doesn't seem too clear with their code.

Thanks!

+7
iphone xcode audio
source share
3 answers

Here is a simple example of using AVAudioPlayer:

-(void)PlayClick { NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"click" ofType:@"caf"]]; AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil]; [click play]; [click release]; } 

This assumes that the main package has a file named "click.caf". Since I play a lot in this sound, I actually continue to play it later, rather than releasing it.

+11
source share

I wrote a simple Objective-C wrapper around AudioServicesPlaySystemSound and friends:

 #import <AudioToolbox/AudioToolbox.h> /* Trivial wrapper around system sound as provided by Audio Services. Don't forget to add the Audio Toolbox framework. */ @interface Sound : NSObject { SystemSoundID handle; } // Path is relative to the resources dir. - (id) initWithPath: (NSString*) path; - (void) play; @end @implementation Sound - (id) initWithPath: (NSString*) path { [super init]; NSString *resourceDir = [[NSBundle mainBundle] resourcePath]; NSString *fullPath = [resourceDir stringByAppendingPathComponent:path]; NSURL *url = [NSURL fileURLWithPath:fullPath]; OSStatus errcode = AudioServicesCreateSystemSoundID((CFURLRef) url, &handle); NSAssert1(errcode == 0, @"Failed to load sound: %@", path); return self; } - (void) dealloc { AudioServicesDisposeSystemSoundID(handle); [super dealloc]; } - (void) play { AudioServicesPlaySystemSound(handle); } @end 

Lives here . For other sound parameters see this question .

+7
source share

Source : AudioServices - Wikia for iPhone Developers

AudioService sounds are independent of the device volume controller. Even if the phone is in silent mode, sound signals will play. These sounds can only be muted by tapping SettingsSoundsRings and alerts .

Custom system sounds can be played with the following code:

 CFBundleRef mainbundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL); AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject); 

Call vibration in iPhone

 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

Reproduction of the built-in sounds of the system.

 AudioServicesPlaySystemSound(1100); 
+2
source share

All Articles