Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

ios - non-main bundle file as alert sound

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".

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Payload:

{
"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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...