2016-09-13 14 views
1

アプリがバックグラウンドでも音楽が再生され、AVPlayerが無効になったときに通知を送信したいと考えています(たとえば、ユーザーが上に乗っている別のアプリを使用している場合など)。Swift - AVPlayerがバックグラウンドモードでオーバーライドされたときに通知を送信するにはどうすればよいですか?

現在、私の解決策は、notificationを設定し、アプリは時間のx量の後にチェックインしていない場合、その後、notificationがオフになりますが、それはチェックインをした場合には、通知を削除し、別のものを設定しcheckInTimerを使用することです。しかし、このソリューションは吸う..

アイデア?

答えて

3

あなたはaudio interruptionsを観察する必要があります。

import AVFoundation 

func setup() { 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(myInterruptionHandler), name: AVAudioSessionInterruptionNotification, object: nil) 
} 

func myInterruptionHandler(notification: NSNotification) { 
    var info = notification.userInfo! 
    var intValue: UInt = 0 
    (info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue) 
    if let type = AVAudioSessionInterruptionType(rawValue: intValue) { 
     switch type { 
     case .Began: 
      // interruption began 
     case .Ended: 
      // interruption ended 
     } 
    } 
} 
+0

これは非常によく働いた、ありがとうございました! – Charles

関連する問題