xcode 如何将文件添加到主包的 /Library/Sounds 目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35418988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:32:46  来源:igfitidea点击:

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

iosxcodeapple-push-notifications

提问by Cymric

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?

根据Apple 的文档,当尝试播放声音时,系统会搜索 ~/Library/Sounds 中的声音文件。如何将声音文件添加到此文件夹?

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.

我试图在运行时写入文件夹,但没有权限。我在 xcode 中添加了一个 Library/Sounds 文件夹,但它似乎没有复制过来。

enter image description here

在此处输入图片说明

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

xcode -> 窗口 -> 设备,选择我的应用程序并显示容器,文件夹不存在 在此处输入图片说明

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.

因此,声音文件需要由系统自动检测,并且可以在运行时由应用程序修改。将文件添加到主包将不起作用,因为它是只读的。我希望 ~/Library/Sound 中的文件是可编辑的。

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.

我对自定义声音问题的解决方案。

采纳答案by Allen

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

您拥有的链接适用于 Mac!iOS 的正确文档应该在这里

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

总之,您可以将声音文件作为应用程序包的非本地化资源添加到您的项目中。

回答by RMoore

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

我也为此苦苦挣扎。您必须以编程方式创建 Library/Sounds 目录并将您的自定义声音移动到其中。

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)")
}

回答by Puneet Arora

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.
  1. 在您的项目中有多个声音文件。
  2. 保持用户的声音选择。
  3. 当您收到推送通知时,使用您在第 2 步中坚持的内容播放声音。