Call Recorder Issue- Xamarin.Android

2.3k Views Asked by At

I am using MediaRecorder class library to develop app for recording voice calls. I am using Moto G mobile (API Level 21) for USB Debugging. After making call, I start to record with below code. It ends up in exception and breaks at recorder.Start(). It creates a file but it doesn't contain any audio and it is zero bytes dummy file.

public MediaRecorder recorder = new MediaRecorder();

Start Call Record:

recorder.Reset();
recorder.SetAudioSource(AudioSource.VoiceCall);
recorder.SetOutputFormat(OutputFormat.ThreeGpp);
recorder.SetAudioEncoder(AudioEncoder.Aac);
recorder.SetOutputFile("/sdcard/Download" + "/123.amr");                    
recorder.Prepare();
recorder.Start(); // Exception Hits

End Call Record

recorder.Stop();
recorder.Release();

Permissions:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Exception

{Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.  
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in <filename unknown>:0   
  at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00062] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:554   
  at Android.Media.MediaRecorder.Start () [0x00043] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d419c934/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Media.MediaRecorder.cs:1475   
  at CallRecorderAndroidPOC.MainActivity+<>c__DisplayClass4.<OnCreate>b__0 (System.Object param0, System.EventArgs param1) [0x000a1] in d:\Android Application Development\POC's\CallRecorderAndroidPOC\CallRecorderAndroidPOC\MainActivity.cs:65 
  --- End of managed exception stack trace ---
java.lang.RuntimeException: start failed.
    at android.media.MediaRecorder.start(Native Method)
    at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
    at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
    at android.view.View.performClick(View.java:4761)
    at android.view.View$PerformClick.run(View.java:19767)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5312)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
}

I changed the file name extension to .aac, but also the same exception throws.

Can, anyone help me to solve this issue?

2

There are 2 best solutions below

0
On

You are given the Output format as : recorder.SetOutputFormat(OutputFormat.ThreeGpp);.

But,

When you set the output path it is given as : recorder.SetOutputFile("/sdcard/Download" + "/123.amr");

Modify it as : recorder.SetOutputFile("/sdcard/Download/123.3gpp");

0
On

A bit late, I know, but I ran into the same issue and solved it so I'd like to post the answer for anyone else who comes across this.

The problem could be that you're trying to record from an audio source which is not active. If that's the problem, then while you're making a call, that code should work fine.

Another reason this could be happening is you're trying to make a voicecall recording from a phone which doesn't support this feature, like a google phone.