How to play the system by pressing a button on the iPad?

I need to play some system sound when users press a button in my application that runs on an iPad. How to implement it in iOS?

+5
source share
3 answers

If you want to play a short sound (less than 30 seconds), you can do it easily:

Note . You will need to add an AudioToolbox structure 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 may be:

  • Duration no more than 30 seconds
  • In linear PCM or IMA4 format (IMA / ADPCM)
  • Packed in .caf, .aif or .wav file
+11
source

You must use AVAudioPlayer.

, AVAudioPlayer . :

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];
+3
NSString *path = [[NSBundle mainBundle] pathForResource:@"gorilla 2" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

theAudio.delegate=self;
[theAudio play];
0
source

All Articles