How to play system button click sound on iPad? - objective-c

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?

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

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];

NSString *path = [[NSBundle mainBundle] pathForResource:#"gorilla 2" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];

Related

AVAudioPlayer or PlaySystemSoundID?

I have an iOS app which is going to provide a sound pad for the user - i.e. when they tap a button a short sound clip is played.
At the moment, the app is designed around using SystemSoundID.
Would it now be advised to use AVAudioPlayer now over this? If so, what would be the ideal code to use? Would it be the standard:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"yoursoundfile"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];

AVAudioPlayer doesn't work with (NSString *)filename

I want to implement a player that plays sounds and/or music. I have these two methods:
-(void)playSound:(int)soundIndex
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[SoundFxFilenameArray objectAtIndex:soundIndex] ofType:#"mp3"]];
if(FxPlayer)
{
[FxPlayer release];
FxPlayer = nil;
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
FxPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[FxPlayer play];
}
-(void)playMusic:(NSString *)filename
{
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:filename ofType:#"mp3"]];
if(MusicPlayer)
{
[MusicPlayer release];
MusicPlayer = nil;
}
MusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[MusicPlayer play];
}
Now, [[SoundPlayer sharedManager] playSound:FXIndex_Default] works just fine. When I try to play music though, [[SoundPlayer sharedManager] playMusic:#"music_file_name_here" just doesn't do anything. Note that both the sound file and the music file are of the same format (mp3).
Also, the "sharedInstance" is because I implemented the singleton pattern for this audio player.
Any pointers or ideas on what is so wrong in my code so that I can play sounds but not music?
Use fileURLWithPath instead of URLWithString in playMusic:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filename ofType:#"mp3"]];
URLWithString expects a complete URL such as "file://path/to/file.mp3" as argument, fileURLWithPath expects just a file path.

Objective C error when playing multiple sounds

Hi i am trying to play many sounds using AVAudioPlayer and foundation.
The code is this
- (IBAction)pushButton19 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"The Terminator" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
- (IBAction)pushButton20 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"The Wizard of Oz" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
on the line theAudio.delegate = self;
it says
class * does not implement the
protocol
as well as
Semantic Issue Assigning to ID
from
incompatible type MainView *
what do i need to do to fix it and can u show me the correct code as im a noob at this?
Your controller must conforms to the AVAudioPlayerDelegate protocol and implements its necessary methods. This is a sample code for playing sound.

MP3 file not playing in Iphone/Ipad

I am palying MP3 files in my application using code
NSString *filePath = [[NSBundle mainBundle] pathForAuxiliaryExecutable:#"congratulation.MP3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[audioPlayer prepareToPlay];
audioPlayer.currentTime = 0;
[audioPlayer play];
I am playing 15 MP3 files
All playing well, but one file is not playing it is playing very well i itunes but not in my app
Any solution for this strange problem.
Amit Battan
I have to replace the MP3 files

Cocoa Touch - Loading AVAudioPlayer

I have this code to play a sound but the first time you play it it takes a good 5 seconds to load...
How can I speed this up?
-(IBAction)playSound{ //play the cricket noise
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
You can try by putting the following section of your code in viewDidLoad so that the audioPlayer instance is ready to play before it is asked to actually play the audio:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"Cricket_Sound" ofType:#"mp3"];
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
audioPlayer.numberOfLoops = 0;