【问题标题】:Swift2 push notification foreground play soundSwift2 推送通知前台播放声音
【发布时间】:2015-11-18 12:51:05
【问题描述】:

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

谢谢

【问题讨论】:

  • 对否决票的解释会很好..Apple 没有更新他们的文档以适应他们的新编程语言,所以我如何不问就学习?
  • 当应用程序在前台时,通知不会显示,但会在didReceiveRemoteNotification 中传递。您必须以编程方式播放声音。
  • 是的,Vadian 这就是我最终演奏声音的方式;答案如下。

标签: apple-push-notifications swift2 ios9


【解决方案1】:
//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)
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2018-11-18
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多