ios 为本地通知选择自定义声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7170475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:14:15  来源:igfitidea点击:

Choose custom sound for local notifications

iphoneobjective-ciosipadnotifications

提问by Hyman Humphries

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

采纳答案by Noah Witherspoon

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.

那应该工作。确保声音实际上在您的应用程序包中,采用正确的格式(线性 PCM 或 IMA4——几乎任何解释如何为 iOS 转换声音的地方都会告诉您如何转换),并且在 30 秒内。

回答by Rafael Sanches

You can convert from wav and mp3 using:

您可以使用以下方法从 wav 和 mp3 转换:

afconvert -f caff -d LEI16@44100 -c 1 in.wav out.caf

回答by Mason Zhang

I also tried to convert mp3 to caf using command below:

我还尝试使用以下命令将 mp3 转换为 caf:

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.

这里的一个教训:我花了几个小时来与本地通知的声音作斗争。尝试使用不同的比特率和格式进行转换。但最后我发现iOS 6.1模拟器肯定有缺陷。我将代码部署到设备上,它运行良好,但在模拟器上,没有任何声音。

回答by Robert George

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

我遇到了所有这些问题(感谢您的回答)并且能够通过从 .wav 转换为 .caf(无 6.1 模拟器)来解决问题。我需要的最后一点是确保 .caf 在我的应用程序包中。要确认/更正此...

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.

右键单击“项目导航器”中的文件,选择“显示文件检查器”并确保该文件在“目标成员资格”区域中的项目旁边有一个复选标记。在我的情况下,“LocalNotificationExampleTests”是唯一检查的项目。检查“LocalNotificationExample”修复了我在设备上没有声音的问题。

回答by Repose

For Swift 3use UNNotificationSound.init for the notif object in notification scheduling function.

对于Swift 3,在通知调度函数中使用 UNNotificationSound.init 作为通知对象。

Here's a Swift 3example:

这是一个Swift 3示例:

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

最后一点,根据 Apple 的说法:“声音文件必须包含在应用程序的包中或应用程序数据容器的 Library/Sounds 文件夹中。如果文件存在于这两个位置,那么 ~/Library/Sounds 中的文件将是首选。 ”

回答by Raksha.

Swift 5.1

斯威夫特 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"))