UILocalNotification声音不起作用

时间:2015-06-16 15:58:48

标签: ios swift uilocalnotification

我对UILocalNotification的声音有一些问题。我在app delegate中拥有用户权限。

这是我的代码:

var nottypes:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
var notsettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: nottypes, categories: nil)
application.registerUserNotificationSettings(notsettings)

通知代码:

var notification:UILocalNotification = UILocalNotification()
notification.alertBody = "Simple"
notification.alertTitle = "Back To App!"
notification.fireDate = NSDate(timeIntervalSinceNow: 20)
notification.soundName = "Chord"
UIApplication.sharedApplication().scheduleLocalNotification(notification)

2 个答案:

答案 0 :(得分:3)

在S.O.发布问题之前,您应该先查看文档。如果您阅读soundName's属性定义的第一行,则说:

  

对于此属性,请在应用程序的主程序包或UILocalNotificationDefaultSoundName中指定声音资源的文件名(包括扩展名)以请求默认系统声音。当系统显示本地通知的警报或标记应用程序图标时,它会播放此声音。默认值为nil(无声音)。不支持持续时间超过30秒的声音。如果您指定的声音播放时间超过30秒,则会播放默认声音。

所以试试:

notification.soundName = "Chord.mp3"   // use .mp3 or your file extension if it is not an mp3 

答案 1 :(得分:0)

我遇到了同样的问题,我必须执行以下步骤:

  1. 将文件从mp3或wav转换为caf。 如其他答案中所述,您可以使用以下命令转换终端中的wav,aif和mp3文件:

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

  2. 您还可以使用以下内容将Quicktime 7导出为.aif 设置线性PCM速率44.100线性PCM样本大小16这些aif 似乎支持文件。

  3. 确保文件少于30秒,即29秒或更短。

  4. 将文件添加到捆绑包的根目录,您可以将其拖放到Xcode中或右键单击“将文件添加到(projectname)” 请注意,您必须选择以下选项: a)目的地复制项目(如果需要) b)添加到目标(您的构建名称) 4)通过名称引用文件,包括扩展名,例如 localNotification.soundName = "Chord.caf";

  5. 确保您在测试之间从设备/模拟器中删除该应用,否则您将看不到更改的结果。

    如果仍有问题,请检查声音文件文件是否在项目的根目录中。 检查声音文件在节目文件检查器中选择“目标成员资格”。