How can I play the recorded sound using local notification?

I have an application that requires the user to record their own message for playback in the future. My intention was to use UILocalNotification. Unfortunately, it seems that the sound associated with the local notif should be stored in the main set, but the user cannot record and save it in the set.

How to get a user-recorded sound file that will play through a local notification?

Alternatively - is there a way if the application does not work to capture a local notification (without waiting for the user to respond to the warning), and then play the desired sound file?

Thanks!

0
source share
3 answers

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

 localNotification.soundName = @"MySoundFile.wav"; 

Unfortunately, according to the link, it is impossible to use an audio file that is not stored in the main package or declared by apple:

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

See also Link to the UILocalNotification #soundName Class

+4
source

I think this is possible for ios 9, here is what the Apple documentation says:

For remote notifications in iOS, you can specify the custom sound that iOS plays when it represents a local or remote notification for the application. Sound files can be located in the main set of the client application or in the "Library / Sounds" folder in the application data container.

I tested saving the sound in the Library / Sounds folder and then used it with local notification, and it worked fine on iOS 9, after which I tried the same on iOS 8, and it didn’t work, so my competition was that this is only possible for iOS 9

You can access the library directory as follows:

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

You need to create a directory if it does not exist:

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

and you can save your sounds there.

+3
source

The local notification property soundname can refer to the path of the sound file in the application bundle. Sound recording of a user's voice cannot be used, because it cannot be saved in the application bundle at run time.

You cannot receive a default notification, and then open the application and play the recorded voice if the user does not listen to the notification. This means that if the user does not use the phone, the only chance that they will listen to the recording is to take the phone, unlock it and click on the notification. Therefore, I believe that this is far from what you want.

0
source

Source: https://habr.com/ru/post/923256/


All Articles