如何将文件添加到主包的 /Library/Sounds 目录?
How do I add a file to the main bundle's /Library/Sounds directory?
根据Apple's documentation,~/Library/Sounds中的声音文件在尝试播放声音时将被系统搜索。如何将声音文件添加到此文件夹?
我试图在 运行 时写入该文件夹,但没有权限。我在 xcode 中添加了一个 Library/Sounds 文件夹,但它似乎没有复制过来。
xcode -> window -> 设备,select 我的应用程序和显示容器,文件夹不存在
为了添加一些上下文,我正在为解析推送通知制作自定义声音。服务器人员告诉我,当向许多用户广播时,在有效负载中为每个用户发送自定义声音字符串太困难了。作为解决方法,我正在尝试使用单个声音文件,每次用户 select 发出新声音时该文件都会被动态覆盖。
因此声音文件需要由系统自动检测,并且可以在 运行 时间由应用程序修改。将文件添加到主包将不起作用,因为它是只读的。我希望 ~/Library/Sound 中的文件是可编辑的。
此时我不知道如何进行。任何帮助将不胜感激。
更新:
我在 运行 时错误地尝试使用
创建目录
try fileManager.createDirectoryAtPath("Library/Sounds", withIntermediateDirectories: true, attributes: nil)
正确的代码应该是
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.
我在工作中遇到了完全相同的问题。我建议 -
- 您的项目中有多个声音文件。
- 保留用户的声音选择。
- 当您收到推送通知时,使用您在第 2 步中坚持的内容播放声音。
你的 link 是给 Mac 的! iOS 的正确文档应该是 here
总而言之,您可以将声音文件作为应用程序包的非本地化资源添加到您的项目中。
我也为此苦苦挣扎。您必须以编程方式创建 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)")
}
根据Apple's documentation,~/Library/Sounds中的声音文件在尝试播放声音时将被系统搜索。如何将声音文件添加到此文件夹?
我试图在 运行 时写入该文件夹,但没有权限。我在 xcode 中添加了一个 Library/Sounds 文件夹,但它似乎没有复制过来。
xcode -> window -> 设备,select 我的应用程序和显示容器,文件夹不存在
为了添加一些上下文,我正在为解析推送通知制作自定义声音。服务器人员告诉我,当向许多用户广播时,在有效负载中为每个用户发送自定义声音字符串太困难了。作为解决方法,我正在尝试使用单个声音文件,每次用户 select 发出新声音时该文件都会被动态覆盖。
因此声音文件需要由系统自动检测,并且可以在 运行 时间由应用程序修改。将文件添加到主包将不起作用,因为它是只读的。我希望 ~/Library/Sound 中的文件是可编辑的。
此时我不知道如何进行。任何帮助将不胜感激。
更新: 我在 运行 时错误地尝试使用
创建目录 try fileManager.createDirectoryAtPath("Library/Sounds", withIntermediateDirectories: true, attributes: nil)
正确的代码应该是
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.
我在工作中遇到了完全相同的问题。我建议 -
- 您的项目中有多个声音文件。
- 保留用户的声音选择。
- 当您收到推送通知时,使用您在第 2 步中坚持的内容播放声音。
你的 link 是给 Mac 的! iOS 的正确文档应该是 here
总而言之,您可以将声音文件作为应用程序包的非本地化资源添加到您的项目中。
我也为此苦苦挣扎。您必须以编程方式创建 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)")
}