作为 Apple Docs 的一部分,我正在尝试在我的应用程序收到通知时播放自定义声音。这应该通过以下方式完成:
您可以将音频数据打包为 aiff、wav 或 caf 文件。然后,在 Xcode 中,将声音文件作为应用程序包的非本地化资源添加到您的项目中
我的问题是如何创建非本地化资源?当您将音频文件拖放到 Xcode 项目中时,它会自动创建吗?是在Info.plist中指定的吗?
我可以使用以下代码播放声音文件:
if let buttonBeep = self.setupAudioPlayerWithFile("0897", type:"aiff") {
self.buttonBeep = buttonBeep
}
buttonBeep?.volume = 1.0
buttonBeep?.play()
...
func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {
let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
let url = NSURL.fileURLWithPath(path!)
var audioPlayer:AVAudioPlayer?
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("Player not available")
}
return audioPlayer
}
但是当收到推送通知时没有声音播放。
最佳答案
将声音文件拖到 Xcode 中时,请确保选中“添加到目标”复选框。
关于ios - 如何在 iOS App Bundle 中创建非本地化资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33897697/