I am trying to read audio as 16 bit integers in my android app. I have written this app using Xamarin.android . I am using AudioRecord low level Library for this because i need to read raw bytes from phones mic and not a high level operation .
At StartRecording() function there is an Exception from Java , saying that the Uninitialized AudioRecord.
AudioRecord au = new AudioRecord(AudioSource.Mic, 44100, ChannelIn.Mono, Android.Media.Encoding.Pcm16bit, buffer.Length);
au.StartRecording(); //<-- Here
//(Java.Lang.IllegalStateException: startRecording()
// called on an uninitialized AudioRecord.)
I went throw some qns in stack overflow and i tried releasing any prior allocation in a try catch block . The problem still persists .
AudioRecord au = new AudioRecord(AudioSource.Mic, 44100, ChannelIn.Mono, Android.Media.Encoding.Pcm16bit, buffer.Length);
try {
au.StartRecording();
au.Read(buffer, 0, buffer.Length);
}
catch (Java.Lang.IllegalStateException e ) {
au.Release();
au = new AudioRecord(AudioSource.Mic, 44100, ChannelIn.Mono, Android.Media.Encoding.Pcm16bit, buffer.Length);
au.StartRecording();
au.Read(buffer, 0, buffer.Length);
}
This is my compleate code in a separate code file .
using System;
using System.Text;
using System.Collections.Generic;
using Android.Media;
namespace core
{
public static class RawAudioInput
{
public static List<short> Listen()
{
int bufferSizeInBytes = AudioRecord
.GetMinBufferSize(44100, ChannelIn.Mono,
Android.Media.Encoding.Pcm16bit);
byte[] buffer = new byte[bufferSizeInBytes];
AudioRecord au = new AudioRecord(AudioSource.Mic, 44100, ChannelIn.Mono, Android.Media.Encoding.Pcm16bit, buffer.Length);
try {
au.StartRecording();
au.Read(buffer, 0, buffer.Length);
}
catch (Java.Lang.IllegalStateException e ) {
au.Release();
au = new AudioRecord(AudioSource.Mic, 44100, ChannelIn.Mono, Android.Media.Encoding.Pcm16bit, buffer.Length);
au.StartRecording();//<-- (Java.Lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.)
au.Read(buffer, 0, buffer.Length);
}
au.Stop();
List<short> result = new List<short>();
for (int i = 0; i < buffer.Length; i += 2)
{
result.Add(BitConverter.ToInt16(buffer, i));
}
au.Release();
return result;
}
}
}
I dont understand where i am doing things wrong .Please help me solve this issue.
Edit I managed to get the full stack trace of the exception from System.Diagnostics.Debug.WriteLine() Here it is :
[0:] Java.Lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <286213b9e14c442ba8d8d94cc9dbec8e>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <286213b9e14c442ba8d8d94cc9dbec8e>:0
at Android.Media.AudioRecord.StartRecording () [0x0000a] in <b781ed64f1d743e7881ac038e0fbdf85>:0
at core.RawAudioInput.Listen () [0x00028] in C:\Users\aditya_nadig\source\repos\RawAudio\RawAudio\RawAudioInput.cs:17
at RawAudio.MainActivity.OnRecordButtonClick (System.Object sender, System.EventArgs e) [0x00004] in C:\Users\aditya_nadig\source\repos\RawAudio\RawAudio\MainActivity.cs:31
--- End of managed Java.Lang.IllegalStateException stack trace ---
java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.media.AudioRecord.startRecording(AudioRecord.java:1011)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
at android.view.View.performClick(View.java:6993)
at android.widget.TextView.performClick(TextView.java:12752)
at android.view.View$PerformClick.run(View.java:26512)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
StartRecording
should be called in a separate thread. You should use Async Task.it
,and achieve AudioManager
in your code, Here is simple demo, you could refer to it.
https://developer.xamarin.com/samples/monodroid/Example_WorkingWithAudio/
update
And do not forget to request these permission in AndroidManifest,
"android.permission.MODIFY_AUDIO_SETTINGS"
"android.permission.READ_EXTERNAL_STORAGE"
"android.permission.WRITE_EXTERNAL_STORAGE"
"android.permission.RECORD_AUDIO"