UILocalNotification custom sound is not playing in iOS7 - iphone

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.

Related

Custom sounds for local notification in iphone is not working

Im integrating local notification into my app.
It is working fine.
But client want to use a different sound for local notification.
I drag and drop the file into the supporting files of xcode
Can anyone please help me how to do this.
code
AlocalNotifas.soundName = #"cling.wav";
localNotifas.soundName = [[NSBundle mainBundle] pathForResource:#"cling" ofType:#".wav"];
// [localNotifas setSoundName:[[NSBundle mainBundle] pathForResource:#"cling" ofType:#"wav"]];
// [localNotifas setSoundName:UILocalNotificationDefaultSoundName];
Can you experts please show me where im going wrong.
Remember that your notification sounds may not be longer than 30 seconds - if the file supplied is longer, nothing will sound.
Also, check the sound in some external player first, and best convert it to .caf format.
To convert a file to .caf, open up terminal, go to where you have your sound stored and type in:
afconvert -f caff -d LEI16#44100 -c 1 yourfile.wav yourfile.caf
Then just set it like:
yourNotification.soundName = #"yoursound_name.caf";
Use your local notification object and set its soundName property to sound file
[localNotif setSoundName:#"abc.mp3"];
Be sure to copy the sound file in the root of the project.
you can also use the name of the sound directly, by example...
localNotif.soundName = #"marimba.mp3";

Choose custom sound for local notifications

How do you change the sound that plays for local notifications? I use the code below to play the default sound:
notif.soundName = UILocalNotificationDefaultSoundName;
So, I tried this below, and it didn't work. What should I do? Thanks for your help!
notif.soundName = #"sound.caf";
You can convert from wav and mp3 using:
afconvert -f caff -d LEI16#44100 -c 1 in.wav out.caf
That should work. Make sure the sound is actually in your app’s bundle, is in the correct format (linear PCM or IMA4—pretty much anywhere that explains how to convert sounds for iOS will tell you how to do that), and is under 30 seconds.
I also tried to convert mp3 to caf using command below:
afconvert clockalarm.mp3 clockalarm.caf -d ima4 -f caff -v
One lesson here: I spent several hours to struggle with the sound of local notification. Tried to convert with different bitrate and format. But finally I found that there must be a defect with the iOS 6.1 emulator. I deployed the code to device, it works well, but on emulator, does not have any sound.
I ran into all of these problems (thanks for the answers) and was able to solve the problem by converting from .wav to .caf (sans 6.1 simulator). The last bit I needed was to make sure the .caf was in the my app's bundle. To confirm/correct this...
Right click the file in the "Project Navigator", select "Show File Inspector" and make sure the file has a check mark next to your project in the "Target Membership" area. In my case "LocalNotificationExampleTests" was the only item checked. Checking "LocalNotificationExample" fixed my lack of sound on the device.
For Swift 3 use UNNotificationSound.init for the notif object in notification scheduling function.
Here's a Swift 3 example:
func scheduleNotifications(inSeconds: TimeInterval, completion: #escaping (_ Success: Bool) ->()){
...
notif.sound = UNNotificationSound.init(named: "CustomSound.mp3")
...
}
Final note, according to Apple: "The sound file must be contained in the app’s bundle or in the Library/Sounds folder of the app's data container. If files exist in both locations then the file in ~/Library/Sounds will be preferred."
Swift 5.1
How do you change the sound that plays for local notifications. I use the code below to play the default sound:
content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "Sound_Name.mp3"))

UILocalNotification Not Working ... No Vibration on iPhone 4 iOS 4.3

I would write as much as I could write here, but the Title pretty much says it all. I've tested it in several different situations:
1) Phone on, silent mode off, app on, in foreground, screen unlocked
I know that this goes through app delegate's didReceiveLocalNotification and didn't expect a sound or vibration, except for the handling code that I included under didReceiveLocalNotification. The handling code actually called
NSURL *Sound = [[NSBundle mainBundle] URLForResource: self.currentSoundPVC
withExtension: #"caf"];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
AudioServicesPlayAlertSound (soundFileObject);
and that actually works! It plays a sound and vibrates the phone simultaneously.
2) phone on, silent mode off, app on, in background, screen unlocked
Now, I set up my uilocalnotification alarms in app delegate's applicationDidEnterBackground, using the following code
NSString *Sound = [self.currentSoundPVC stringByAppendingString:#".caf"];
UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
[alarm setSoundName:Sound];
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
and this only partially works! The sound gets played but there is NO VIBRATION! Now, this I think is a bug because I am quoting from the apple developer website,
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html, Scheduling the Delivery of Local Notifications section:
"When the notification is sent and the sound is played, the system also triggers a vibration on devices that support it. "
Now, obviously that is NOT THE CASE for my iPhone and obviously the simulator doesn't vibrate, so I can't test this and would like to have this addressed in the developer community!
3) phone on, silent mode off, app on, in foreground, screen locked
Same as #2
4) phone on, silent mode off, app on, in background, screen locked
Same as #2
5) phone on, silent mode off, app off (background process deleted)
Same as #2, because the uilocalnotifications were never cancelled, so the iOS still thinks they are valid.
6) phone on, silent mode on, app on, in background, screen unlocked
no sound, no vibration, NOTHING! This sucks! I would have hoped that apple would have come up with something that works straight out of the box, as usual!
These are the options and the effects (all assume the volume is set to some reasonable value):
Option 1:
Configuration:
[Developer controlled] Valid sound name set in UILocalNotification = No
[User controlled] Notification Centre setting for the app: Sound = N/A
[User controlled] Silent mode (switch on side of iPhone) = N/A
Behaviour:
Sound played with notification = No
Vibrate with notification = No
Option 2:
Configuration:
Valid sound name set in UILocalNotification = Yes
Notification Centre setting for the app: Sound = Off
Silent mode (switch on side of iPhone) = N/A
Behaviour:
Sound played with notification = No
Vibrate with notification = No
Option 3:
Configuration:
Valid sound name set in UILocalNotification = Yes
Notification Centre setting for the app: Sound = On
Silent mode (switch on side of iPhone) = No (Not selected)
Behaviour:
Sound played with notification = Yes
Vibrate with notification = No
Option 4:
Configuration:
Valid sound name set in UILocalNotification = Yes
Notification Centre setting for the app: Sound = On
Silent mode (switch on side of iPhone) = Yes (Selected)
Behaviour:
Sound played with notification = No
Vibrate with notification = Yes
the following works with a sound and a vibration:
// Create a new notification
UILocalNotification * notif = [[[UILocalNotification alloc] init] autorelease];
if (notif)
{
notif.repeatInterval = 0;
notif.alertBody = #"NOTIFICATION!!"];
notif.soundName = #"sound.caf";
notif.alertAction = NSLocalizedString(#"View", #"View");
[[UIApplication sharedApplication] presentLocalNotificationNow:notif];
}
I know this might sound a bit silly, but have you perhaps turned off vibration in your iPhone settings?
Sorry for posting this as an answer but my comment was too long to post under yours. So I just put a snippet there and the rest here. This was a reply to tony million's answer.
Hi Tony,
Thanks for your input, however I am not sure which of the things that you changed had an effect. If I am noting correctly you changed the following items:
1) scheduleLocalNotification --> presentLocalNotificationNow
2) explicitly setting UILN repeatInterval
3) explicitly setting UILN alertBody
4) explicitly setting UILN alertAction
Other than those four changes, I don't see anything else. Which of these causal changes would you think had the desired effect for you.
Lol...no need to feel like that was a silly question. Although, I did mention in my post, "silent mode on/off", I did not specify what that meant. I was talking particularly about the switch on the side of the phone. It didn't even occur to me to check the iPhone settings! Haha! So, it was a good question! Unfortunately, however, it wasn't turned off and it had successfully vibrated when in scenario #1 from OrigPost.

Sound working in emulator, not in real 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!

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