Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record and play simultaneously on iOS (Phonegap build)

I'm developing iOS and Android application using Phonegap Build version 3.3.0.

Main focus of application is audio recording with another audio / music playing in the background.

For both instances i'm using phonegap media api with correct audio files for recording (iOs - *.wav / Android - *.amr ) and playing (iOs and Android - *.mp3).

Example:

var audioRec = new Media(audioRecSrc, onSuccess, onError);
audioRec.startRecord();

var audioPlay = new Media(audioPlaySrc, onSuccess, onError);
audioPlay.play();

Example works on Android without any problem. Sound is recorded and music played normally. But in iOS only one is possible. Whichever is called last, play or record. The other one returns error with code - 4.

Is this limitation of Phonegap Media API on iOs or am I missing something?

like image 350
matjazu Avatar asked Oct 21 '22 09:10

matjazu


1 Answers

In order to be able to simultaneously play media and record audio, one must set the category property of the AVAudioSession to AVAudioSessionCategoryPlayAndRecord. To do so you must deploy a custom iOS plugin that sets the corresponding value.

At the time speaking, this process is not quite straightforward because of a bug in the Cordova Media Plugin. Before starting to record, the plugin sets the category of the AVAudioSession to AVAudioSessionCategoryRecord indiscriminately. Because of that, after starting to record, playing back the desired media becomes impossible, unless you explicitly set the AVAudioSession category to play-and-record, after calling the record method. This clearly insinuates that you can only play your media, after recording has started.

However, this workaround may not be acceptable in many scenarios, which may require that media starts to play before sound is recorded. Thus, I have filed a bug report regarding the issue, which you may find here:

iOS Media plugin: cannot play video and record audio simultaneously

Plus, I have applied the fix and performed the following pull request:

Check if avSession.category is already set to "AVAudioSessionCategoryPlayAndRecord" before recording

It's really a minor fix (literally two lines of code), thus I believe that it will be soon applied to the master branch.

like image 61
nachos Avatar answered Oct 27 '22 23:10

nachos