Sound working in emulator, not in real iPhone - iphone

Have few short sound effect samples, which play just fine in emulator, but not at all in real iPhone 3GS. Here's the code, about as-is from Apple SysSound sample:
CFBundleRef mb = CFBundleGetMainBundle ();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL
(mb, CFSTR("mySound"), CFSTR ("caf"), NULL);
SystemSoundID sid;
AudioServicesCreateSystemSoundID(soundFileURLRef, &sid);
AudioServicesPlaySystemSound(sid);
When using iPhone, I can hear keyclicks and music from iTunes (not trying to use at same time as playing my sound) - but cannot hear my sound at all. Vibra works ok, so even Framework should be set up correctly.
Tried even the SoundEffect.h/m sample code, no change. Used same sound files, but shouldn't CAF be ok, especially when it plays in emulator?
What can I try next?

Try converting to a different format such as wav or mp3, then play again. If you want to use caf, Make sure you are formatting the caf correctly in Terminal.app:
afconvert -f caff -d ima4 mysound.wav

Just as a sidenote - I was having the exact same problem and spent probably close to an hour on converting files to the correct format, etc.. Yet the problem was the "mute" switch on the iPad. So even though the volume was up, and I could hear other sounds on the iPad, because the mute switch was turned on, it wasn't playing system sounds.
To add to the confusion, this app uses text-to-speech and the volume coming from the transcription was perfectly fine, it was only the sounds coming from AudioServicesPlaySystemSound() that I couldn't hear.

kind of a long shot, but remember that the phone's file system is case sensitive, while the mac's usually isn't. Double check your file name

Found an easier solution: use AIF sound files:
Click iTunes > Preferences
Click on "General" tab
Click "Import Settings" button
In "Import Using" dropdown, choose "AIFF Encoder"
Save your changes
Select your sound files and choose "Create AIFF version"
Here's code I'm using, together with SoundEffect.h and SoundEffect.m from Apple sample BubbleLevel:
NSBundle *mainBundle = [NSBundle mainBundle];
buzzerSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:#"buzzerSound" ofType:#"aif"]];
[buzzerSound play];
Now same code - and sound effects - work in both emulator and hardware. Btw don't forget to switch back your original iTunes settings!

Related

UILocalNotification custom sound is not playing in iOS7

I'm using UILocalNotification in an application.
In the application there are two sounds which are played conditionally- I have applied proper conditions for them.
But when I install the application and run it on an iOS 7 device, then it fires the local notification but a sound is not playing in the application.
Code is given below to set the notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = [pickerView date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
if (alarm_number == 1) {
localNotification.alertBody = [NSString stringWithFormat:#"First Alarm"];
}
else
{
localNotification.alertBody = [NSString stringWithFormat:#"Second Alarm"];
}
localNotification.alertAction =#"Ok";
NSString *message = [[NSString alloc]initWithString:#""];
if(alarm_number == 1)
{
localNotification.soundName=#"Alarm_1.mp3";
message = #"First Alarm Scheduled";
}
else
{
localNotification.soundName=#"Alarm_2.mp3";
message = #"Second Alarm Scheduled";
}
localNotification.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSString *alarmString;
if (alarm_number==1) {
alarmString = [NSString stringWithFormat:#"First Alarm"];
}
else
{
alarmString = [NSString stringWithFormat:#"Second Alarm"];
}
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:alarmString forKey:#"AlarmFor"];
localNotification.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
What I have checked for is the Sound setting in the Settings/Notification Centre app for my app.
Please go through 1st to 3rd image to see what I have checked.
(1) Notification Center
(2) Application
(3) Sound is off here
So, to enable this I have checked Inter App Audio at Capabilities in Targets of the application and it was Off as shown in the image below.
Capabilities in Inter-app audio
Then I have changed it to On and it looks like shown in the image below.
Yet, it still does not play any sound in iOS 7 devices.
Does anybody have any idea about why is it not working? It would be a great help.
Thanks.
Did you try to turn off "Do not disturb" mode off?
I have the same problem, then I found out "Do not Disturb" is on.
After turn off that, it all works the right way.
try using .caf file instead of .mp3
afconvert -f caff -d LEI16#44100 -c 1 wolf.wav wolf_out.caf
My guess is your sound isn't formatted appropriately to play by the OS. Make sure your sound is formatted in IMA4 format. Also, make sure your file is in your application bundle, and you should be good to go.
For more reference and similar question, see this SO question.
Choose custom sound for Local Notifications
Here is a link for Apple's Local and Push Notification Guide. It explains the sounds in detail and the default sounds that can be used.
I ran into the same issue, I had to do the following steps:
Convert the file from mp3 or wav to caf.
As stated in other answers you can convert wav, aif and mp3 files in terminal using the command:
afconvert -f caff -d LEI16#44100 -c 1 in.mp3 out.caf
You can also export with Quicktime 7 as .aif with the following
settings Linear PCM Rate 44.100 Linear PCM sample size 16 these aif
files seem to be supported.
Make sure you files are less than 30 seconds, i.e 29 seconds or less.
Add you files to the root of your bundle, you can drag drop into Xcode or right click "add files to (projectname)"
Note you must select the following options:
a)Destination Copy items if needed
b)Add to targets (your build name(s))
4)Reference the file by its name including the extension e.g
notif.soundName = #"sound.caf";
Make sure you delete the app from the device / simulator between tests or you will not see the results of your changes.
If your still having problems, check the sound file file is in the root of the project.
Check the Sound file "Target Membership" is selected in the show file inspector.
Please try first whether setting->notificationcenter->sounds is not off.Then check whether in setting->sounds->slider is not on zero.That means if u have sounds on zero that will not allow sound to come up. Hope that helps.
Check that the "Sound Effects" is not mute or low. The volume may be high but if the Sound Effects volume is mute then the local notification may not sound. U can increase it when you are in the Home screen of iOS and then press the volume up button.
If mp3 file is edited with some tool, it is possible that something was missed by edit tool.
I had similar problem while trying to play mp3 file as local notification sound.
Sound was shorter than 30 seconds, it was bundled, everything appears regular on the surface.
After few hours i have realised that problem was caused by online mp3 cutter tool.
I have find better editor, and file became playable on notification.
Make sure the sound is added to the main bundle!
I didn't get any idea why it did work.
But, at last I have solved my problem by making a new project in Xcode 5.0 and ported old project to this new project. So it's working and I can get sound too.

Trying to play an audio file

I'm trying to play an audio file in a cocos2d application. Here is the line which tries to play the sound:
[[SimpleAudioEngine sharedEngine] playEffect:#"pig_squeal.wav"];
If I put a log near this line, the log appears, and I can play the sound with iTunes. But when the sound should be played, there is a message displayed:
AudioStreamBasicDescription: 2 ch, 44100 Hz, 'lpcm' (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved
What's the problem?
This was discussed in comments but I've amalgamated all the possibilities of why it may not work here.:
That's not an error message, just some information.
Is the sound definitely in your library and a part of the target?
You haven't changed the volume of the SimpleAudioEngine or the volume of your device isn't all the way down?
Click the sound in your library. Press Option+Command+1 to bring up the file editor. Scroll down to "Target Membership" and ensure the sound is checked for your target.
Try a different sound effect too? Try and narrow the problem down to "is it SimpleAudioEngine".
Also try and playBackgroundMusic for a sound (This was the solution in this case)
And try an mp3
There isn't a problem. It's a status message written to the log when you initialise the current OpenAL context using alcMakeContextCurrent. As far as I'm aware, you can't get rid of the message.
There is no problems with your code for the Playing of the Sound. Please check the Sound file has been added to the project also do check the format of the sound. It should play the Sound whenever you call the Play Effect. Please also try to PRELOAD the sound effect in the init method.
that message means the song was read correctly and should be playing. possible reasons you don't hear it?
volume turned down.
device malfunctioning
audio has silence in it
audio file is large and will take a long time to load.
sound has been redirect to come out of the headphones or the ear piece (even if not attached)
But the sound is loading and most likely playing.
If you are getting a crash while running in device from xcode pls try this(worked for me). Dissconnect the device from Xcode and run the app in the device. I dont know why it worked like that. But when I did this there was no crash.

How to play iPhone tap sound?

I have a button in my iPhone app that I'd like to have play the default "keyboard tap" sound when it's tapped. I've been able to play my own custom sounds easily enough, but is there any way to play a default system sound like this in my app?
usingsystemsounds
You must use the System Sound for this.The below might be usefule.Refer Multimedia Programming guide to know more.
CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL
(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
Also you can use the inbuilt system sounds by
AudioServicesPlaySystemSound(1100);
The click sound you want is, I believe, in the sample app called SysSound.
http://developer.apple.com/iphone/library/samplecode/SysSound/
I found this, which sounds like what you are looking for.
Nothing public. There's probably an answer if you're willing to dive into private APIs, but this is strongly discouraged (as you will likely break future compatibility, and may get rejected from the app store).
There's an example on the iPhone Dev Center web site for playing short-duration sounds (5 seconds or less), You can use that code to play any sound you want. Find a click sound doing a google search and use that. That's all you need.
http://developer.apple.com/iphone/library/codinghowtos/AudioAndVideo/index.html#PLAY_SHORT_SOUNDS_AND_ALERTS_2
AudioServicesCreateSystemSoundID (fileURL, soundID);

iPhone - some sound files not playing

I have a list of short wav files. Two of these files do not play on either the simulator or the device itself. All are wav files 1 second long.
This is how I play the files
SystemSoundID soundID;
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"wav"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
AudioServicesCreateSystemSoundID((CFURLRef)fileUrl, &soundID);
AudioServicesPlaySystemSound(soundID);
Can someone please tell me what the problem could be. Thanks.
P.S. When I view the files in Finder, the files not playing show the default player as QuickTime and the others show the default player as iTunes if that helps in any way.
The solution as Alan suggested is to use AIFF files. To convert wav to AIFF, open the sound file in iTunes. In iTunes, select Preferences->General->Import Settings->Import using AIFF Encoder. Select the sound file and in Advanced menu option select the option "Create AIFF version"
The iPhone only supports the following codecs:
AMR (Adaptive Multi-Rate, a speech codec)
iLBC (internet Low Bitrate Codec, also a speech codec)
IMA/ADPCM (also known as IMA-4)
Linear PCM
µLaw and aLaw
Your WAV files may contain audio that is in a different format, so even though the iPhone supports WAV files, it might not support your specific WAV file.
The best option is normally to use the command line program "afconvert" to convert everything to CAF files (which is Apple's default format). e.g.:
afconvert sound1.wav sound1.caf
the full set of conversion options can be listed by running:
afconvert -h
This conversion will be lossless from most formats. The CAF file format is normally the fastest and lowest overhead to use on the iPhone.
See this question
Trying to play sound through iPhone Simulator
I've had a lot more success with .aiff files.
It would be good to know what exactly AudioServicesCreateSystemSoundID is checking when a file is passed to it.

Multiple audio sounds in iPhone app?

I've gotten to play a single sound in the iPhone app I've started, but I now desire to play multiple sounds based on a button. To create a separate set of .m and .h files for each audio sounds, and then including them all, doesn't seem the most efficient way to tackle this in terms of coding...then again, I'm just starting out with Cocoa and only just completed my first app ever.
Any help is appreciated. The key here is multiple sounds, each triggered by its own button or image. Thanks.
If the files are MP3 or AAC format, then you can only play one at a time. This is a limitation of core audio on the iPhone.
In terms of playing multiple sounds at once, that's easy, just create a new player instance for every one that you want to play (and remember to release them when you're done)
NSString *path = [[NSBundle mainBundle] pathForResource:#"dream" ofType:#"m4a"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
To convert an MP3 into something like IMA4 (which you can play more than one at once) you would run the following (in terminal, in leopard):
/usr/bin/afconvert -f caff -d ima4 sound.mp3 sound.caf
The above code is firmware 2.2 only, but you can do the same with the AudioQueue files if you want to support older firmwares (it's just a lot more complex, so I haven't listed the code here).
If you have access to the iPhone developer network, then there are a bunch of code samples that show you how to play audio.
All you need to do is make one class that has a function called
-(void)Play:(NSString*)sSoundFile {
// play sound file here
}
I don't have any direct experience with iPhone development, but in theory could you create a single large sound file with all your sounds in it? Each sound could be separated by just enough silence to be individually accessed by a time index. Of course, the downside is that you'd have to load the entire file into memory, unless you could figure out a way to load chunks in a random-access fashion...
#rustyshelf - does this work in the simulator? I'm using that code, and using #import but neither audioPlayerDidFinishPlaying nor audioPlayerDecodeErrorDidOccur ever get called. Very annoying to try to debug.
http://www.icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/
in this example in ios5 you have to put
CFURLRef soundurl=(__bridge CFURLRef)[NSURL fileURLWithPath:objstring];
in place of
CFURLRef soundurl=(CFURLRef)[NSURL fileURLWithPath:objstring];
Hardik Mamtora