How can I play a recorded sound using a local notification?

回眸只為那壹抹淺笑 提交于 2020-01-21 06:13:33

问题


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!


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6514423/how-can-i-play-a-recorded-sound-using-a-local-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!