Swift2推送通知前景播放声音

时间:2015-11-18 12:51:06

标签: apple-push-notifications swift2 ios9

当我的应用收到推送通知并且在前台活动时,有人可以帮助我播放声音吗?我可以破解它,但不是Apple提供给开发人员的更优雅的内置功能。我正在使用Swift2。

由于

1 个答案:

答案 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)
        }
    }
相关问题