How do I add a file to the main bundle's /Library/Sounds directory?

前端 未结 3 659
闹比i
闹比i 2021-02-08 10:57

According to Apple\'s documentation, sound file in the ~/Library/Sounds will be search by system when trying to play a sound. How do I add a sound file to this folder?

I

相关标签:
3条回答
  • 2021-02-08 11:40

    the link you have is for Mac! the correct document for iOS should be here

    in summary, you can just add the sound file to your project as a nonlocalized resource of the app bundle.

    0 讨论(0)
  • 2021-02-08 11:44

    I struggled with this as well. You have to create the Library/Sounds directory programmatically and move your custom sound into it.

    let soundsDirectoryURL = fileManager.urls(for: .libraryDirectory, in: .userDomainMask).first!.appendingPathComponent("Sounds")
    
    //attempt to create the folder
    do {
        try fileManager.createDirectory(atPath: soundsDirectoryURL.path,
                                        withIntermediateDirectories: true, attributes: nil)
    } catch let error as NSError {
        print("Error: \(error.localizedDescription)")
    }
    
    0 讨论(0)
  • 2021-02-08 11:50

    I had the exact same problem at work. I would suggest -

    1. Have multiple sound files in your project.
    2. Persist user's sound selection.
    3. When you receive push notification, play the sound using what you persisted in step 2.
    0 讨论(0)
提交回复
热议问题