iPhone - Sound not playing with AudioServicesPlaySystemSound nor AVAudioPlayer - iphone

I have a button on a UIImagePicker overly , and I want it to make some sound when clicked.
To do so, I have in my project resources a sound in m4a and mp3 formats (for test purpose).
I try to play the sound by many ways, trying alternatively with m4a and mp3 formats, but nothing goes out of the iPhone (not the simulator).
The Frameworks are include in the project.
Here is my sample code :
#import <AudioToolBox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>
- (IBAction) click:(id)sender {
// Try
/*
SystemSoundID train;
AudioServicesCreateSystemSoundID(CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("TheSound"), CFSTR("mp3"), NULL), &train);
AudioServicesPlaySystemSound(train);
*/
// Try
/*
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: #"TheSound" withExtension: #"mp3"] error:&error];
[audioPlayer play];
[audioPlayer release];
*/
// Try (works)
//AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
// Try
/*
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"TheSound" ofType:#"mp3"] isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
*/
NSLog(#"Clicked");
}
Could you tell me what is going wrong ?

You can't use an MP3 with Audio Services. It must be a WAV file of a certain subtype. As for AVAudioPlayer, it supports MP3 files but when AVAudioPlayer is deallocated it stops playback, and since you do so immediately after sending -play (by sending -release to balance the +alloc), you never hear anything.
So either convert the file to WAV, or hang onto your AVAudioPlayer long enough for it to play. :)

Related

sound is not playing in real device in iphone

I am new to iphone programming.Can any body tell me that below code is for playing recorded sound but its not playing sound in device .can any body tell me what mistake is there in this code.
if(soundID)
{
AudioServicesDisposeSystemSoundID(soundID);
}
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:audiopath isDirectory:NO];
NSLog(#"%#",filePath);
//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
//Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);
In the above code the audio path is the voice recored path. Actually in database path is storing like this
/Users/User/Library/Application Support/iPhone Simulator/5.0/Applications/C974262E-7658-45EE-8E8C-290B780E7C3E/Documents/2012-12-18 13:50:56 +0000.caf
After this while I am retrieving path is show like this:
file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/C974262E-7658-45EE-8E8C-290B780E7C3E/Documents/2012-12-18%2013:50:56%20+0000.caf
What is the mistake in this code? Please tell me.
`NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"your audio file name" ofType:#"wav"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play];`
the names dont match the upper one has an additional SPACE which is encoded on ios
a) 2012-12-18 13:50:56 +0000.caf
b) 2012-12-18 13:50:56%20+0000.caf

Play audio files in iPhone/iPad application?

I want to play sound in my application for that I am using .mp3 file type. It is working fine in Simulator but when I am lunching it to my device it is not working and not producing any sound can any one help me why it is not working on device or which file format is better to play audio file in iPhone and iPad?
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: [NSString stringWithFormat:#"%#",SoundFileName] withExtension: #""];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
AudioServicesPlaySystemSound (soundFileObject);
Above is mine code to play sound
Thank you.
The system sound IDs won't play if the device is muted, so this is possibly the cause. (They're only really for the purpose of user interface effects such as alerts, etc. as per the official documentation.)
As such, I'd be tempted to use the AVAudioPlayer style approach. As a sample (no pun intended) implementation:
// *** In your interface... ***
#import <AVFoundation/AVFoundation.h>
...
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sample_name" ofType:#"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];
if(audioError != nil) {
NSLog(#"An audio error occurred: \"%#\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}
// *** In your dealloc... ***
[testAudioPlayer release];
You should also remember to set the appropriate audio category. (See the AVAudioSession setCategory:error: method.)
Finally, you'll need to add the AVFoundation library to your project. To do this, alt click on your project's target in the Groups & Files column in Xcode, and select "Get Info". Then select the General tab, click the + in the bottom "Linked Libraries" pane and select "AVFoundation.framework".
I honestly would suggest using AVAudioPlayer instead of this, but you could try using this alternate code instead. In your example it looks like you forgot to include the extension, which might be the problem also. The simulator is more lenient on naming, so if you forgot the extension it might play on the simulator and not a device.
SystemSoundID soundFileObject;
CFURLRef soundFileURLRef = CFBundleCopyResourceURL (CFBundleGetMainBundle (),CFSTR ("sound"),CFSTR ("mp3"),NULL );
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject );
AudioServicesPlaySystemSound (soundFileObject);

iPod music will stop after play short sound !

i have buttons on my application and have a short sound effect , when user taps on buttons , short sound plays but if someone listen to the music , the music will stop ! how can i handle this ? to play music and my effect ,i use AudioToolbox framework .
i read apple documentation doesn't work with AVFoundation.framework ! for some reasons i can't change my code ! is there any way for systemSoundID ?
-(void)soundType{
NSString *path = [NSString stringWithFormat:#"%#%#",[[NSBundle mainBundle] resourcePath], #"/type.wav"];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
}
Do you mean that when iPod music is playing and you run your app, the music stops? If so, you should set your audio session to ambient sound so that the iPod can continue playing. Try this:
AudioSessionInitialize(NULL, NULL, NULL, self);
UInt32 category = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
AudioSessionSetActive(YES);
You can try using NSSound
NSSound *snd = [[NSSound alloc] initWithContentsOfFile: fileName byReference: NO];
[snd play];

Using system Sound to play sounds

Here is the code:
-(void)stop
{
NSLog(#"Disposing Sounds");
AudioServicesDisposeSystemSoundID (soundID);
//AudioServicesRemoveSystemSoundCompletion (soundID);
}
static void completionCallback (SystemSoundID mySSID, void* myself) {
NSLog(#"completion Callback");
}
- (void) playall: (id) sender {
[self stop];
AudioServicesAddSystemSoundCompletion (soundID,NULL,NULL,
completionCallback,
(void*) self);
OSStatus err = kAudioServicesNoError;
NSString *aiffPath = [[NSBundle mainBundle] pathForResource:#"slide1" ofType:#"m4a"];
NSURL *aiffURL = [NSURL fileURLWithPath:aiffPath];
err = AudioServicesCreateSystemSoundID((CFURLRef) aiffURL, &soundID);
AudioServicesPlaySystemSound (soundID);
NSLog(#"Done Playing");
}
Output:
Disposing Sounds
Done Playing
In actual no sound gets play at all and completion call back isn't called as well. Any idea what could be wrong here?
I want to stop any previous sound before playing current.
AFAIK, the only supported files are .caf, .aif, or .wav:
To play your own sounds, add the sound
file to your application bundle; the
sound file must adhere to the
following requirements:
Must be .caf, .aif, or .wav files.
The audio data in the file must be in PCM or IMA/ADPCM (IMA4) format.
The file's audio duration must be less than 30 seconds.
Audio & Video Coding How-To's
What does err contain? From that you should be able to infer the problem.
You can use AudioToolBox framework for this:
CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(1100);

NSSound on the iphone

I've been looking for a while how to play sound on the iphone, and I think it's something along the lines of:
[[NSSound soundNamed:#"cat.mp3"] play];
But the NSSound is on the AppKit ... any suggestions ? I know there is a really simple answer to this, but today my searches are not rendering any result ...
the new AVFoundation class is the easiest way to play sound on the iPhone, though it is for firmware 2.2 only:
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[audioPlayer play];
you just need this import in the class that uses it:
#import <AVFoundation/AVAudioPlayer.h>
Audio Toolbox system sounds are great for short async playback.
// Initialize
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
// Init each sound
CFURLRef tapURL;
SystemSoundID tapSound;
tapURL = CFBundleCopyResourceURL(mainBundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(tapURL, &tapSound);
// Play the sound async
AudioServicesPlaySystemSound(tapSound);
I haven't gotten MP3s to work with AudioServicesPlaySystemSound. But it plays AIF files perfectly.