2016-09-13 1 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

관련 문제