簡體   English   中英

UILocalNotification:播放保存在文檔目錄中的自定義音頻文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM