Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play system button click sound on iPad?

I need to play some system sound when users click button in my application which is running on iPad. How to implement it in iOS?

like image 912
Radislav Avatar asked May 24 '12 13:05

Radislav


People also ask

How do I turn off the button sound on my iPad?

Disable the keyboard sounds on iPhone or iPadOpen the Settings app. Tap Sounds & Haptics. Scroll down to Keyboard Clicks and turn it off.


2 Answers

If you want to play a short sound (shorter than 30 sec), you can do it easily like this:

Note: You'll have to add AudioToolbox framework and import it (#import <AudioToolbox/AudioToolbox.h>)

SystemSoundID mySSID;

NSString *path = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: path], &mySSID); 

AudioServicesPlaySystemSound(mySSID);

Also note that the file can be:

  • No longer than 30 seconds in duration
  • In linear PCM or IMA4 (IMA/ADPCM) format
  • Packaged in a .caf, .aif, or .wav file
like image 196
Alladinian Avatar answered Oct 06 '22 18:10

Alladinian


You should use AVAudioPlayer.

There's a great tutorial here on using AVAudioPlayer to play sounds. A very simple example of it's use:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

if (audioPlayer == nil)
    NSLog([error description]);
else
    [audioPlayer play];
like image 23
Liam George Betsworth Avatar answered Oct 06 '22 16:10

Liam George Betsworth