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
How do I add a file to the main bundle's /Library/Sounds directory?
提问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 文件夹,但它似乎没有复制过来。
xcode -> window -> devices, select my app and show container, folder is not there
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)
采纳答案by Allen
回答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 -
我在工作中遇到了完全相同的问题。我会建议 -
- Have multiple sound files in your project.
- Persist user's sound selection.
- When you receive push notification, play the sound using what you persisted in step 2.
- 在您的项目中有多个声音文件。
- 保持用户的声音选择。
- 当您收到推送通知时,使用您在第 2 步中坚持的内容播放声音。