如何使用本地通知播放录制的声音?

发布于 2024-11-17 18:06:33 字数 219 浏览 7 评论 0原文

我有一个应用程序,要求用户记录自己的消息以便在将来的某个时间播放。我的目的是使用 UILocalNotification 来做到这一点。不幸的是,似乎与本地通知相关的声音必须存储在主包中,但用户无法录制并将其保存在主包中。

如何通过本地通知播放用户录制的声音文件?

或者 - 如果应用程序未运行,是否有办法捕获本地通知(无需等待用户响应警报),然后播放所需的声音文件?

谢谢!

I have an app that requires that the user record their own message to be played back at some future time. My intent was to do that using UILocalNotification. Unfortunately, it seems that the sound associated with the local notif has to be stored in the main bundle but the user cannot make a recording and save it in the bundle.

How do I get a user recorded sound file to be played via local notification?

Alternatively - is there a way if the app is not running to capture the local notification (without waiting for the user to respond to an alert) and then play the desired soundfile?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名。 2024-11-24 18:06:34

您可以通过按如下方式设置 UILocalNotification 的属性来将(自定义)声音分配给 UILocalNotification:

localNotification.soundName = @"MySoundFile.wav";

不幸的是,根据参考资料,无法使用未存储在主包中的声音文件或苹果声明:

对于此属性,请指定应用程序主包中的声音资源的文件名(包括扩展名)或 UILocalNotificationDefaultSoundName 以请求默认系统声音。

另请参阅 UILocalNotification 类参考#soundName

You can assign a (custom) sound to a UILocalNotification by setting its property as follows:

localNotification.soundName = @"MySoundFile.wav";

Unfortunately, according to the reference, it is not possible to use a sound file that is not stored in the main bundle or is declared by apple:

For this property, specify the filename (including extension) of a sound resource in the application’s main bundle or UILocalNotificationDefaultSoundName to request the default system sound.

See also UILocalNotification Class Reference #soundName

依 靠 2024-11-24 18:06:34

我认为这对于 ios 9 是可能的,这是苹果文档所说的:

对于 iOS 中的远程通知,您可以指定 iOS 在为应用程序呈现本地或远程通知时播放的自定义声音。声音文件可以位于客户端应用程序的主包中,也可以位于应用程序数据容器的 Library/Sounds 文件夹中。

我测试了将声音保存到 Library/Sounds 文件夹中,然后将其与本地通知一起使用,它在 iOS 9 上运行良好,之后我在 iOS 8 上尝试了相同的操作,但它不起作用,所以我的结论是这是可能的仅适用于 iOS 9

您可以通过以下方式访问库目录:

let fileManager = NSFileManager.defaultManager()
let libraryPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
let soundsPath = libraryPath + "/Sounds"

如果该目录不存在,则必须创建该目录:

fileManager.createDirectoryAtPath(soundsPath, withIntermediateDirectories: false, attributes: nil)

然后您可以将声音保存在那里。

I think this is possible for ios 9, this is what apple documentation says:

For remote notifications in iOS, you can specify a custom sound that iOS plays when it presents a local or remote notification for an app. The sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app’s data container.

I tested saving a sound into the Library/Sounds folder then using it with a local notification and it worked fine on iOS 9, after that I tried the same on iOS 8 and it didn't work, so my conclussion was that this is possible for iOS 9 only

You can access the library directory in this way:

let fileManager = NSFileManager.defaultManager()
let libraryPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
let soundsPath = libraryPath + "/Sounds"

You have to create the directory if it doesn't exist:

fileManager.createDirectoryAtPath(soundsPath, withIntermediateDirectories: false, attributes: nil)

and you can then save your sounds there.

寄离 2024-11-24 18:06:34

本地通知的 soundname 属性可以引用应用程序包中声音文件的路径。
无法使用用户语音的录音,因为它无法在运行时保存到应用程序包中。

除非用户点击了通知,否则您无法显示默认通知,然后打开应用程序并播放录制的语音。
这意味着,如果用户没有使用手机,他们收听录音的唯一机会就是拿起手机、解锁并点击通知。所以我相信这离你想要的还很远。

The local notification's soundname property can refer to the path of a sound file within the app's bundle.
A sound recording of the user's voice cannot be used as it cannot be saved to the app bundle at runtime.

You cannot have a default notification show up and then open the app and play the recorded voice, unless the user has tapped on the notification.
Which means, if the user is not using the phone, the only chance that they will listen to the recording is if they pick up the phone, unlock it, and tap on the notification. So I believe that this is far from what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文