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

后端 未结 3 723
北海茫月
北海茫月 2020-12-18 17:43

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

相关标签:
3条回答
  • 2020-12-18 17:49

    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.

    0 讨论(0)
  • 2020-12-18 17:50

    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

    0 讨论(0)
  • 2020-12-18 17:54

    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.

    0 讨论(0)
提交回复
热议问题