我正在尝试在我的Android应用中将音频读取为16位整数。我已经使用Xamarin.android编写了此应用程序。我为此使用AudioRecord低级库,因为我需要从电话麦克风读取原始字节,而不是高级操作。
在StartRecording()函数中,有一个Java异常,即未初始化的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.)
我在堆栈溢出中抛出了一些qns,并尝试在try catch块中释放任何先前的分配。问题仍然存在。
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);
}
这是我完整的代码,位于单独的代码文件中。
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;
}
}
}
我不知道我在哪里做错了。请帮助我解决此问题。
修改 我设法从System.Diagnostics.Debug.WriteLine()获取异常的完整堆栈跟踪 这是:
[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)
答案 0 :(得分:0)
StartRecording
应该在单独的线程中调用。您应该使用Async Task.it
并在代码中实现AudioManager
,这是简单的演示,您可以参考它。
https://developer.xamarin.com/samples/monodroid/Example_WorkingWithAudio/
更新
不要忘记在AndroidManifest中请求这些权限,
“ android.permission.MODIFY_AUDIO_SETTINGS”
“ android.permission.READ_EXTERNAL_STORAGE”
“ android.permission.WRITE_EXTERNAL_STORAGE”
“ android.permission.RECORD_AUDIO”