Android 调用未初始化的 AudioRecord 错误
Posted
技术标签:
【中文标题】Android 调用未初始化的 AudioRecord 错误【英文标题】:Android called on an uninitialized AudioRecord Error 【发布时间】:2015-05-06 20:31:08 【问题描述】:我正在开发一个从连接到音频插孔的设备中获取一些信息的 API,但是当我尝试在 ASUS MEMO Pad 7(使用 android 4.3)上调试我的应用程序时,我收到了这个错误:
java.lang.RuntimeException: Unable to start service com.walker.integration.WalkerService@21a3f7e0 with Intent cmp=mx.com.mitec.testmit/com.walker.integration.WalkerService (has extras) : java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2877)
at android.app.ActivityThread.access$1900(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
at android.media.AudioRecord.startRecording(AudioRecord.java:537)
at com.bbpos.emvswipe.k.b(SourceFile:73)
at com.bbpos.emvswipe.EmvSwipeController.startAudio(SourceFile:3410)
at com.walker.integration.WalkerService.initEmvSwipeController(SourceFile:61)
at com.walker.integration.WalkerService.onStart(SourceFile:96)
at android.app.Service.onStartCommand(Service.java:450)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2860)
at android.app.ActivityThread.access$1900(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
当我尝试在我的手机 Moto G(使用 android 4.4)上运行它时没有发生某些事情,我不知道发生了什么或如何解决它。
我查看我的清单,看看我是否输入了权限或我输入了错误的 minsdkversion,但这不是问题。
这是我的清单:
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.GET_TASKS"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
【问题讨论】:
【参考方案1】:错误应该准确地描述问题。 AudioRecord 对象未初始化,您在其上调用了 startRecording。
要初始化 AudioRecord 对象,请尝试
audioRecordObj = new AudioRecord(MediaRecorder.Audiosource.DEFAULT, sampleRate, channelConfiguration, audioEncoding, bufferSize);
在哪里`
int sampleRate = 8000; //Hz
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int bufferSize = 60000;
只是一个例子。在docs 中查看更多信息。
【讨论】:
以上是关于Android 调用未初始化的 AudioRecord 错误的主要内容,如果未能解决你的问题,请参考以下文章
Android-Things GpsService 未初始化