当我的应用收到推送通知并且在前台活动时,有人可以帮助我播放声音吗?我可以破解它,但不是Apple提供给开发人员的更优雅的内置功能。我正在使用Swift2。
由于
答案 0 :(得分:0)
//Declare a AVaudioplayer var
var audioPlayer = AVAudioPlayer()
//Call this function whenever you want to play the sound
func playSound()
{
do {
if let bundle = NSBundle.mainBundle().pathForResource("mySound", ofType: "wav")
{
let alertSound = NSURL(fileURLWithPath: bundle)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.play()
print("Playing")
}
} catch {
print(error)
}
}