我正在尝试在我的应用程序收到通知时播放自定义声音,作为
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中时,请确保选中“添加到目标”框.