Swift2推送通知前台播放音效
Swift2 push notification foreground play sound
当我的应用程序收到推送通知并在前台处于活动状态时,有人可以帮我播放声音吗?我可以破解它,但没有更优雅的内置功能可以由 Apple 提供给开发人员。我正在使用 Swift2。
谢谢
//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)
}
}
当我的应用程序收到推送通知并在前台处于活动状态时,有人可以帮我播放声音吗?我可以破解它,但没有更优雅的内置功能可以由 Apple 提供给开发人员。我正在使用 Swift2。
谢谢
//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)
}
}