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

3

12

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 tried to write to the folder in runtime, but has no permission. I added a Library/Sounds folder in xcode but it doesn't seems to copy over.

enter image description here

xcode -> window -> devices, select my app and show container, folder is not there enter image description here

To add some context, I am doing custom sound for parse push notification. The server guy told me that when broadcasting to many users, sending a custom sound string in the payload for each user will be too difficult. As a work around, I am trying to use a single sound file that will be dynamically overwritten each time the user selects a new sound.

Thus the sound file need to be auto detected by the system and can be modified at run time by the app. Adding the file to the main bundle will not work as it is read only. I am hoping that a file in ~/Library/Sound will be editable.

I have no idea how to proceed at this point. Any help will be greatly appreciated.

Update: I incorrectly tried to create a directory at run time by using

        try fileManager.createDirectoryAtPath("Library/Sounds", withIntermediateDirectories: true, attributes: nil)

The correct code should be

        let libraryDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let directoryPath = "\(libraryDir.first!)/Sounds"
        try fileManager.createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)

My solution to the custom sound problem.

Kirimia answered 15/2, 2016 at 21:0 Comment(2)
for any renewable/editable resources you should use Cache folderFernandofernas
The problem is sound file in the cache folder will not be detected by apple for push notification soundKirimia
K
3

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.

Kacykaczer answered 20/2, 2016 at 3:8 Comment(5)
Thank you for the correction Allen. Adding the file directly to the main bundle will not work as the file will not be editable. The webpage you link state that Library/Sounds folder of the data container will also be auto detected by apple and I am hoping that files in this directory will be editable. Thus my question is asking how to add a file to this directory. I will mark an answer as correct even if a file in Library/Sound is not editable as long as someone can tell me how to do it.Kirimia
@Kirimia if you are looking for creating files in run-time in Library, then you should look at this SO post: #27216689Kacykaczer
I found out what I did wrong when creating directory in Library from that question, thats for pointing it out for meKirimia
The link is outdated. 404 page.Degradation
Here is new link developer.apple.com/library/archive/documentation/…Sardius
C
8

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)")
}
Creepie answered 21/1, 2018 at 2:10 Comment(0)
K
3

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.

Kacykaczer answered 20/2, 2016 at 3:8 Comment(5)
Thank you for the correction Allen. Adding the file directly to the main bundle will not work as the file will not be editable. The webpage you link state that Library/Sounds folder of the data container will also be auto detected by apple and I am hoping that files in this directory will be editable. Thus my question is asking how to add a file to this directory. I will mark an answer as correct even if a file in Library/Sound is not editable as long as someone can tell me how to do it.Kirimia
@Kirimia if you are looking for creating files in run-time in Library, then you should look at this SO post: #27216689Kacykaczer
I found out what I did wrong when creating directory in Library from that question, thats for pointing it out for meKirimia
The link is outdated. 404 page.Degradation
Here is new link developer.apple.com/library/archive/documentation/…Sardius
F
-1

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.
Fiberglass answered 18/2, 2016 at 19:48 Comment(4)
The sound played by the device is controlled by the string in the push notification payload, for example, if the server send sound1.caf, the app has no way to play sound2.caf instead. As stated in my question, the server is unable to send different sound string to different users.Kirimia
You are saying its "controlled" by the string in the push notification payload. But, server is unable to send different sound string to different users. So, how is it controlled by the string in the push notification payload? Why don't you have multiple sounds files and have some logic on the client side which would play the sound. Why do you want to edit files?Fiberglass
When the phone receive a remote push notification, the OS automatically search for the sound file specify in the notification's payload and play it. There is no way for the app to modify this process. The app might not even be running when the notification arrive.Kirimia
To play different sound with the same file, I need to overwrite the content of that file. I added a link to my solution in my question above if I wasn't explaining clearly enough.Kirimia

© 2022 - 2024 — McMap. All rights reserved.