Choose custom sound for local notifications - iphone

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"))

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.

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

iphone keyboard sound

I'm looking for the default keyboard sound, when a key is pressed on the iPhone. Does anyone know if I can download a .caf or .wav of this sound. Or if I can access it from within a program>?
In the Finder, navigate to
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/UIKit.framework
The filename is Tock.aiff
Found this on my iPod Touch 1st generation, it kind of sounds like the keyclick but I'm not sure:
/System/Library/Frameworks/UIKit.framework/scrollerClick.wav
For simulators with version >10.0, keyboard click sounds (and other UI sounds) can found in :
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.0.sdk/System/Library/Audio/UISounds
The format is .caf which can be converted in .wav using Quick Time export.

UILocalNotification Or EventKIt

Is there a way to trigger some kind of task on the base of date event .My problem is that i want to play sound (alarm) on specific date it does work fine by using UILocalNotification, but it plays s short sound , and i want to play an infinite sound instead of .How can i achieve this goal ..
waiting for your early response.
Thanks
The apple documentation of UILocalNotification states that the soundName property will play for a maximum of 30 seconds only.
Sounds that lasts longer than 30 seconds are ignored and the default sound is played instead.
While adding the custom sound make sure the sound is in the correct format (linear PCM or IMA4)
You can convert from wav and mp3 using:
afconvert -f caff -d LEI16#44100 -c 1 in.wav out.caf
In case if u were wondering what is afcomvert ..it's nothing but command line program
Caf is container format..
Sometimes emulator won't play the custom sound but device does.
You can schedule a custom sound using the soundName property of UILocalNotification, but it can't be infinite. You could use the repeatInterval property to play the alert again, making it seem infinite...

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!