[英]UILocalNotification: Playing a Custom Audio File saved in Documents Directory
当前,我正在创建一个警报应用程序,该应用程序播放来自服务器的自定义音频片段。 我实现此目标的计划是,将所有音频片段保存在本地,然后相应地设置soundName。
但是我有一些问题。 目前,我在将音频文件保存在bundle目录中时遇到麻烦,只能将文件保存在文档目录中。 是否可以从文档目录而不是分发包目录设置soundName?
要么
我可以将音频文件从服务器保存到捆绑目录吗?
var localNotification = UILocalNotification()
localNotification.fireDate = self.timePicker.date
localNotification.alertBody = "Alert Fired"
localNotification.soundName = "fakesound.caf" // File saved in Document Directory
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
谢谢,如果您对我的问题有任何疑问,请告诉我。 或者,如果您可以考虑另一种解决方案,以解决该问题。
不幸的是,答案是否定的。
您只能在本地通知中播放来自捆绑软件的声音,并且捆绑软件是只读的。
您可以从本地通知中播放的唯一声音必须随您的应用一起提供。 没有其他选择。
幸运的是,是。 访问此链接: 如何将文件添加到主捆绑包的/ Library / Sounds目录中?
在这里,我已将系统铃声复制到Library / Sounds,同样,您也必须通过以下方式从数据目录中进行复制:将路径作为源路径和目标,如下所示,并创建一个具有Sounds名称的目录。
// get the list of system sounds, there are other sounds in folders beside /New
let soundPath = "/System/Library/Audio/UISounds/New"
var arrayOFSoundNames = [String] ()
// MARK: - scene setup
func doAnyAdditionalSetup()
{
arrayOFSoundNames = getSoundList()
}
// MARK: - File manager methods
func getSoundList() -> [String] {
var result:[String] = []
let fileManager = NSFileManager.defaultManager()
let enumerator:NSDirectoryEnumerator =
fileManager.enumeratorAtPath(soundPath)!
for url in enumerator.allObjects {
let string = url as! String
let newString = string.stringByReplacingOccurrencesOfString(".caf", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
result.append(newString)
}
return result
}
// copy sound file to /Library/Sounds directory, it will be auto detect and played when a push notification arrive
class func copyFileToDirectory(fromPath:String, fileName:String) {
let fileManager = NSFileManager.defaultManager()
do {
let libraryDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let directoryPath = "\(libraryDir.first!)/Sounds"
try fileManager.createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)
let systemSoundPath = "\(fromPath)/\(fileName)"
let notificationSoundPath = "\(directoryPath)/notification.caf"
let fileExist = fileManager.fileExistsAtPath(notificationSoundPath)
if (fileExist) {
try fileManager.removeItemAtPath(notificationSoundPath)
}
try fileManager.copyItemAtPath(systemSoundPath, toPath: notificationSoundPath)
}
catch let error as NSError {
print("Error: \(error)")
}
}
// MARK: - tableview methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOFSoundNames.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
copyFileToDirectory(fromPath:soundPath, fileName:arrayOFSoundNames[indexPath.row])
}
您将得到答案,还检查苹果文件
还有另一种方法,您可以将自定义音频文件直接保存在“库/声音”中,而不是保存在文档目录中。 然后只需将带有扩展名的文件名放在通知有效负载中,它将在本地/推送通知中播放您的自定义音频; 提供的时间不超过30秒。
如果需要,请参考代码:
let libraryDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let directoryPath = "\(libraryDir.first!)/Sounds"
try fileManager.createDirectoryAtPath(directoryPath, withIntermediateDirectories: true, attributes: nil)
在这里,您可以在directoryPath变量中获取Library / Sounds的路径,使用它可以保存或执行其他操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.