如何使用文档目录中存在的声音文件进行本地通知? [英] How can I use a sound file present in the documents directory for a local notification?

查看:68
本文介绍了如何使用文档目录中存在的声音文件进行本地通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS应用程序中,如何播放存储在应用程序文档目录中的声音文件以获取本地通知,而不是在应用程序捆绑包中播放声音文件?

Within my iOS application, how can I play a sound file stored in the application's documents directory for a local notification, rather than one inside the application bundle?

我在App Store上看到了几个应用程序可以做到这一点,我想知道这怎么可能。

I've seen a couple of applications on the App Store do this, and I was wondering how this is possible.

推荐答案

实际上可以在应用程序的主捆绑包(NSBundle)中没有的本地通知中播放自定义声音。

It is actually possible to play a custom sound in a local notification that isn't in the app's main bundle (NSBundle).

声音必须存储在 Library / Sounds 目录中,例如:
/ var / mobile / Containers /Data/Application/DD7E6BA0-2FC0-4DE0-95BF-C2E9C9A94E08/Library/Sounds/mySound.caf

The sound has to be stored in the Library/Sounds directory, for example: /var/mobile/Containers/Data/Application/DD7E6BA0-2FC0-4DE0-95BF-C2E9C9A94E08/Library/Sounds/mySound.caf

当您引用声音文件时在UILocalNotification soundName 属性中,您只需编写文件名,而不是完整目录路径:

When you reference the sound file in the UILocalNotification soundName property, you simply write the filename, rather than the full directory path:

alert.soundName = @"mySound.caf";

经过测试并在iOS9.3.5上运行。我还没有尝试过其他iOS版本。

Tested and working on iOS9.3.5. I haven't tried on other iOS versions yet.

脚注:

在搜索有关远程通知的Apple文档时,我碰巧发现了此信息此处-特别是有效载荷键部分,其中提到了声音。

I happened to find this information when searching the Apple Documentation regarding Remote Notifications Here - Specifically, the 'Payload Keys' section, where it mentions sound.

Apple的Local&远程通知(此处)还提到,如果声音文件位于应用程序的主捆绑包或数据容器中,则可以使用它们:

Apple's documentation for Local & Remote Notifications (Here) also mentions that the sound files can be utilised if they are in the app's main bundle OR the data container:


要播放声音,请将声音分配给soundName属性。您可以在应用程序的主捆绑包(或数据容器)中分配非本地化的自定义声音的文件名,也可以分配UILocalNotificationDefaultSoundName来获取默认的系统声音。警报消息的显示或图标的标记应始终伴有声音;在没有其他通知类型的情况下,不应播放声音。

To play a sound, assign a sound to the soundName property. You can assign the filename of a nonlocalized custom sound in the app’s main bundle (or data container) or you can assign UILocalNotificationDefaultSoundName to get the default system sound. A sound should always accompany the display of an alert message or the badging of an icon; a sound should not be played in the absence of other notification types.

这篇关于如何使用文档目录中存在的声音文件进行本地通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆