Playing video with AVPlayer and recording sound with AVAudioRecorder simultaneously

后端 未结 1 565
萌比男神i
萌比男神i 2020-12-28 23:36

I was googling this problem for many days but I didn\'t find a solution for my simple problem.

I\'m trying to play a video while recording sound. Recording sound wor

1条回答
  •  既然无缘
    2020-12-28 23:52

    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.

    0 讨论(0)
提交回复
热议问题