Playing video with AVPlayer and recording sound with AVAudioRecorder simultaneously

六眼飞鱼酱① 提交于 2019-11-30 04:03:57

I had the same problem (except I'm using the AVPlayer to play audio and not video though). I suggest you start listening for audio routing changes and you will see the route change multiple times when the AVPlayer and AVAudioRecorder are started at the same time.

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,
                                audioRouteChangeListenerCallback,
                                self);

In the callback print out the category and route (kAudioSessionProperty_AudioRoute). The only way I could get the audio playback and record to work simultaneously was if:

  1. The audio session was AVAudioSessionCategoryPlayAndRecord and kAudioSessionProperty_OverrideCategoryMixWithOthers property was true.

  2. I initiated the playback before the recording. After AVAudioRecorder prepareToRecord is called, don't start recording until you get the audio route change. Even though it does not fail if you start recording earlier, the audio route change seems to make the recording fail.

I tried other combinations, but this is the only one that works every time.

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