0

I am trying to record audio from an Android phone via the microphone during a phone call made using Twilio. It seems Twilio uses webRTC since there are several I/org.webrtc.Logging entries. I've narrowed down some issues related to storage, permissions, setAudioEncoder() / setOutputFormat() calls, and this SO Q/A suggests the error is related to another service using the phone's mic.

MediaRecorder start failed: -38

None the less, is it possible to capture the audio in parallel to webRTC or is there some method to hook into the audio stream?

I've tried several configs, but mainly I've tried to manipulate the order, type and settings which have given me varying degrees of success. This code below is as far as I've gotten. And seems to suggest that I can't due to another service using the microphone.

public void onConnected(Call call) {

                setAudioFocus(true);

                recorder = new MediaRecorder();
                Log.d(TAG, "created recorder");

                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                Log.d(TAG, "set source");

                recorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
                Log.d(TAG, "set output");

                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); // TODO: change format, codec
                Log.d(TAG, "set codec");

                fileName = getExternalCacheDir().getAbsolutePath();
                fileName += "/audiorecordtest.aac";

                recorder.setOutputFile(fileName);
                Log.d(TAG, "set file");

                try {
                    recorder.prepare();
                } catch (IOException e) {
                    Log.e(TAG, "prepare() failed");
                }
                recorder.start();

                Log.d(TAG, "Connected");
                activeCall = call;
            }

Here is the Logcat output...

2019-05-20 15:46:04.865 13078-13132/product I/org.webrtc.Logging: WebRtcAudioRecord: startRecording
2019-05-20 15:46:04.870 13078-13163/product I/org.webrtc.Logging: WebRtcAudioRecord: AudioRecordThread@[name=AudioRecordJavaThread, id=563]
2019-05-20 15:46:04.969 13078-13078/product D/CallActivity: created recorder
2019-05-20 15:46:04.972 13078-13078/product D/CallActivity: set source
2019-05-20 15:46:04.972 13078-13078/product D/CallActivity: set output
2019-05-20 15:46:04.973 13078-13078/product D/CallActivity: set codec
2019-05-20 15:46:04.975 13078-13078/product D/CallActivity: set file
2019-05-20 15:46:05.005 13078-13078/product E/MediaRecorder: start failed: -38
2019-05-20 15:46:05.005 13078-13078/product D/AndroidRuntime: Shutting down VM
2019-05-20 15:46:05.006 13078-13078/product E/AndroidRuntime: FATAL EXCEPTION: main
    Process: product, PID: 13078
    java.lang.IllegalStateException
        at android.media.MediaRecorder.start(Native Method)
        at product.CallActivity$2.onConnected(CallActivity.java:259)
        at com.twilio.voice.Call$1$2.run(Call.java:532)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-05-20 15:46:05.011 13078-13078/product I/Process: Sending signal. PID: 13078 SIG: 9
Michael Schmidt
  • 396
  • 1
  • 3
  • 15

0 Answers0