我有一个应用程序记录传入和传出的语音呼叫,它在使用marshmallow或更高版本的设备上正常工作。但是当我尝试在Android 5.1.1上运行它时它会给我错误并且应用停止响应。
The error shown in logcat is-ava.lang.RuntimeException: Unable to start service com.android.hitech.calls.Unused.MyRecordingService@2cb0b090 with Intent { cmp=com.android.hitech.calls/.Unused.MyRecordingService (has extras) }: java.lang.IllegalStateException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2925)
at android.app.ActivityThread.access$2100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1408)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.android.hitech.calls.Unused.MyRecordingService.onStartCommand(MyRecordingservice.java:88)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2908)
我录制语音电话的代码是 -
recorder = new MediaRecorder();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
System.out.println("Present in MIC");
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALLS);
}
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOnErrorListener(this);
recorder.setOnInfoListener(this);
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
编辑 - 我按照下面的答案中的建议做了但是我仍然存在错误。调用状态现在仅通过稍微调整代码被调用一次但是应用程序仍然崩溃甚至异常都是相同的。< / p>
答案 0 :(得分:1)
因为在android 5.1中,android系统每次触发
CALL_STATE
两次...... !!即,一旦您的NEW_OUTGOING_CALL
启动,或者来电STATE_RINGING
,STATE_ONHOOK
和STATE_IDLE
被触发两次.. !!你必须通过代码管理它。
你可以在这里找到5.1问题的参考资料...... This is causing error