如何使用本地通知播放录制的声音?
我有一个应用程序,要求用户记录自己的消息以便在将来的某个时间播放。我的目的是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过按如下方式设置 UILocalNotification 的属性来将(自定义)声音分配给 UILocalNotification:
不幸的是,根据参考资料,无法使用未存储在主包中的声音文件或苹果声明:
另请参阅 UILocalNotification 类参考#soundName
You can assign a (custom) sound to a
UILocalNotification
by setting its property as follows: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:
See also UILocalNotification Class Reference #soundName
我认为这对于 ios 9 是可能的,这是苹果文档所说的:
我测试了将声音保存到 Library/Sounds 文件夹中,然后将其与本地通知一起使用,它在 iOS 9 上运行良好,之后我在 iOS 8 上尝试了相同的操作,但它不起作用,所以我的结论是这是可能的仅适用于 iOS 9
您可以通过以下方式访问库目录:
如果该目录不存在,则必须创建该目录:
然后您可以将声音保存在那里。
I think this is possible for ios 9, this is what apple documentation says:
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:
You have to create the directory if it doesn't exist:
and you can then save your sounds there.
本地通知的
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.