Swift2推送通知前台播放声音



当我的应用程序收到推送通知并在前台处于活动状态时,有人能帮我播放声音吗?我可以破解它,但苹果公司没有向开发者提供更优雅的内置功能吗。我正在使用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)
        }
    }

最新更新