简体   繁体   English

UILocalNotification:播放保存在文档目录中的自定义音频文件

[英]UILocalNotification: Playing a Custom Audio File saved in Documents Directory

Currently I am creating an alarm app that plays custom audio clips from the server. 当前,我正在创建一个警报应用程序,该应用程序播放来自服务器的自定义音频片段。 My plan on implementing this is by saving all the audio clips locally and then setting the soundName accordingly. 我实现此目标的计划是,将所有音频片段保存在本地,然后相应地设置soundName。

But I am having a few issues. 但是我有一些问题。 Currently I am having troubles saving the audio files in the bundle directory and only able to save the files in the document directory. 目前,我在将音频文件保存在bundle目录中时遇到麻烦,只能将文件保存在文档目录中。 Would it be possible to set the soundName from the document directory instead of the bundle directory? 是否可以从文档目录而不是分发包目录设置soundName?
OR 要么
would it be possible for me to save the audio file from the server to the bundle directory? 我可以将音频文件从服务器保存到捆绑目录吗?

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)

Thanks, please let me know if you have any confusion with my question. 谢谢,如果您对我的问题有任何疑问,请告诉我。 Or if you can think of another solution on how I could solve this problem. 或者,如果您可以考虑另一种解决方案,以解决该问题。

The answer, unfortunately, is no and no. 不幸的是,答案是否定的。

You can only play sounds from the bundle in a local notification, and the bundle is read-only. 您只能在本地通知中播放来自捆绑软件的声音,并且捆绑软件是只读的。

The only sounds you can play from a local notification must be shipped with your app. 您可以从本地通知中播放的唯一声音必须随您的应用一起提供。 No other option. 没有其他选择。

Fortunately, it is YES. 幸运的是,是。 Vist this link : How do I add a file to the main bundle's /Library/Sounds directory? 访问此链接: 如何将文件添加到主捆绑包的/ Library / Sounds目录中?

Here I have copied the system ringtone to Library/Sounds, likewise you have to copy from your data directory by putting the path as source path and destination as given below, by creating a directory with Sounds name. 在这里,我已将系统铃声复制到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]) 

}

You will get your answer, also check the apple document 您将得到答案,还检查苹果文件

There is one more way, that you can directly save your custom audio file in Library/Sounds rather than saving in the document directory. 还有另一种方法,您可以将自定义音频文件直接保存在“库/声音”中,而不是保存在文档目录中。 And then just put the name of the file with extension in the notification payload, it will play your custom audio on local/push notification; 然后只需将带有扩展名的文件名放在通知有效负载中,它将在本地/推送通知中播放您的自定义音频; provided its not more than 30 seconds. 提供的时间不超过30秒。

Refer code if needed: 如果需要,请参考代码:

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

Here, you get the path of Library/Sounds in directoryPath variable, using this you can save or do other operations. 在这里,您可以在directoryPath变量中获取Library / Sounds的路径,使用它可以保存或执行其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM