非主要捆绑文件作为警报声 [英] non-main bundle file as alert sound

查看:132
本文介绍了非主要捆绑文件作为警报声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我错了,但我不认为这是可能的。在本地和推送通知编程指南的准备自定义警报声音下,它说声音文件必须位于客户端应用程序的主包中。

I'm hoping I'm wrong but I don't think it's possible. In the Local and Push Notification Programming Guide, under "Preparing Custom Alert Sounds" it says that "The sound files must be in the main bundle of the client application".

如果您无法写入主捆绑,那么如何才能将用户生成的录制内容(例如,使用AVAudioRecorder)作为警报声播放?

If you can't write to the main bundle, then how can you get a user generated recording (using, say, AVAudioRecorder) to play as the alert sound?

开一方面它似乎是不可能的,但另一方面我认为有应用程序在那里做(并且我会寻找那些)。

On the one hand it seems impossible, but on the other I think there are apps out there that do it (and I'll look for those).

推荐答案

我通过将系统声音文件复制到〜/ Library / Sounds目录并将其命名为notification.caf来解决此问题。服务器警报有效负载将此值指定为要播放的声音的名称。每当用户选择另一个声音时,该声音将被复制到同一文件夹并覆盖旧声音。

I solved this by copying a system sound file to ~/Library/Sounds directory and name it as notification.caf. The server alert payload specify this as the name of the sound to be played. Whenever the user selects another sound, that sound will be copied to the same folder and overwrites the old sound.

有效载荷:

{
"aps": {
    "sound": "notification.caf"
}

}

// get the list of system sounds, there are other sounds in folders beside /New
let soundPath = "/System/Library/Audio/UISounds/New"
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)")
    }
}

推送通知声音可能有些错误,我必须在声音响起之前重新启动手机每次通知都可靠地播放。

The push notification sound can be buggy though, I have to reboot my phone before the sound will play reliably for every notification.

这篇关于非主要捆绑文件作为警报声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆