5minho / DreamRecorder

mino & bran BoostCamp Project
6 stars 0 forks source link

AVAudioSession Responding to Interruptions #77

Closed YoonJuHo closed 6 years ago

YoonJuHo commented 6 years ago

AVAudioSession은 여러 앱들이 device audio session을 공유하기 때문에 다른 앱에서 요청을 하면 나의 앱에서 inactive가 된다. 따라서 더이상 AVQueuePlayer가 재생을 하고 있지 못하는 상태가 되어 원하는 알람음(AVPlayer를 통한 알람음 재생)을 재생하지 못하고 있었다.

YoonJuHo commented 6 years ago

공부: https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html

YoonJuHo commented 6 years ago

Audio Interruptions을 처리하는 방법은 NotificationCenter에서 AVAudioSessionInterruption을 구독으로써 해결할 수 있다. 먼저 해당 노티피케이션(AVAudioSessionInterruption)은 userInfo로 AVAudioSessionInterruptionTypeKey와 AVAudioSessionInterruptionOptionKey를 가지고 있다. type은 .begin과 .end로 나뉘고 option은 shouldResume을 가지고 있다.

따라서 방해를 받기 시작(.began)했을 때는 할일이 없고(상태값을 저장할 필요가 없다.) (물론 노티피케이션 추적이 중단되지만 이는 사용자가 AVAudioSessiond을 이용중이라는 것이고 알람(UserNotification)을 충분히 인지 할 수 있는 상태라는 것으로 간주한다.) 방해가 끝났을 때(.ended) AVPlayer가 다시 재생할 수 있도록 호출 하면된다.