iOS AVAudioSession interruption notification not working as expected

这一生的挚爱 提交于 2019-11-29 05:35:34
Aleksander Azizi

This is a widespread problem affecting any app using AV framework components (the same goes for native iOS apps).

As explained in 's documentation on the subject of audio interruptions, the InterruptionTypeEnded should actually be applied in the scenario mentioned:

If the user dismisses the interruption ... the system invokes your callback method, indicating that the interruption has ended.

However, it also states that the InterruptionTypeEnded might not be called at all:

There is no guarantee that a begin interruption will have an end interruption.

Therefore, a different approach is needed in the scenario mentioned.


When it comes to handling music interruptions, the issue won't be around for long. iOS 9 effectively prevents outside audio sources to be used while the app's audio handler is invoked.

A way to handle the exact issue of media interruption could be to listen to MPMusicPlayerController's playbackState, as shown in this stackoverflow question: Detecting if music is playing?.


A more direct way to handle the issue of interruptions would be to either:

Block outside audio interruptions completely by re-invoking your audio component at the time of InterruptionTypeBegan.

Or by giving a UI indication that an outside media source has interrupted the audio session (for example showing an inactive microphone).


Hopefully  will come up with a better solution to the problem, but in the meantime this should give you some options to solve the interruption issue.

ChrisHaze

If you haven't already, try setting your AVCaptureSession's property usesApplicationAudioSession to NO.

This question & answer may act as a good reference if you're looking for any more detail.

NSDeveloper

I try this and find InterruptionTypeEnded may called after music pause in some app, but other not called when pause.

My solution is update the UI to let user know record has stopped and do some related work such as file operation. When interruption ends, active AVAudioSession, if doesn't have error, start a new record.

If you want to join the file before and after interrupt, the answer to this question: AVAudioRecorder records only the audio after interruption may be helpful to you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!