2015-11-18 62 views
-1

有人可以幫我播放聲音,當我的應用程序收到推送通知,並在前臺活動?我可以破解它,但沒有更多優雅的內置於Apple開發人員可用的功能。我正在使用Swift2。Swift2推送通知前景播放聲音

感謝

+0

了下來投票的解釋是nice..Apple還沒有更新他們的文件,以滿足他們的新的編程語言,所以我怎麼能學會了問? – Luben

+0

當應用處於前臺時,通知將不會顯示,但會通過'didReceiveRemoteNotification'傳遞。您必須以編程方式播放聲音。 – vadian

+0

是的Vadian多數民衆贊成我最終如何發揮聲音;答案在下面。 – Luben

回答

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) 
     } 
    }