31

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";
Jack Humphries
  • 13,056
  • 14
  • 84
  • 125

6 Answers6

37

You can convert from wav and mp3 using:

afconvert -f caff -d LEI16@44100 -c 1 in.wav out.caf
Rafael Sanches
  • 1,823
  • 21
  • 28
29

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.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • Drag it and drop it into Xcode, it's under thirty seconds, will CAF work? – Jack Humphries Aug 24 '11 at 04:42
  • 3
    CAF is a container format; you can’t know just from the extension that a given CAF contains the right kind of data. Open the sound in Quicktime Player, hit Cmd-I to open the Movie Inspector, and check the format. A sound I had lying around said its format was “Linear PCM, 16 bit little-endian signed integer” etc.; if your sound file shows something other than that, that’s probably the problem. – Noah Witherspoon Aug 24 '11 at 04:44
  • I instead used the IMA4 format and got it to work. Thanks for your help! – Jack Humphries Aug 24 '11 at 04:48
  • 1
    First, thanks for that! I noticed, that if your sound file is in a folder within your bundle it has to be included e.g. @"ringtones/notification.caf". Second, i just tried it with a mp3 file and it works just fine. Is this a iOS7/8 feature and can anyone say something about its backward compatibility? (i try to avoid the caf files as they bloat my bundle size unnecessarily) – Maximilian Körner Jan 02 '15 at 10:16
  • command for converting .mp3 format to .caf format. just open terminal and type - afconvert inputFilePath outputFilePath -d ima4 -f caff -v – Surjeet Rajput Jun 05 '17 at 07:10
17

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.

Mason Zhang
  • 3,423
  • 24
  • 35
9

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.

Robert George
  • 91
  • 1
  • 1
3

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

Repose
  • 2,157
  • 1
  • 24
  • 26
1

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

Raksha.
  • 479
  • 6
  • 6