我正在从我的Android应用程序录制语音呼叫,每件事情都按预期工作但是当我在Android 5.1.1下在三星A8上测试时,我得到了一个非法状态异常,这是我的代码:
public MediaRecorder startRecordCall(String contactName, String contactE164Phone, String callType, Date callTime){
if (!isRecording){
try {
Toast.makeText(this, R.string.ancare_service_recording_started, Toast.LENGTH_LONG).show();
extractRecordingSettings();
pCurrentAudioVolume = pAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
int pMaxVolume = pAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
int vol = pMaxVolume;
switch (pAudioVolume) {
case 0:
vol = 1;
break;
case 1:
vol = (pMaxVolume / 2) + 1;
break;
case 2:
vol = pMaxVolume;
break;
}
pAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, vol, 0);
File currentRecordFile = MyStorage.makeRecordFile(contactName, contactE164Phone, callType, callTime);
pMediaRecorder = new MediaRecorder();
pMediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
pMediaRecorder.setOutputFormat(pAudioFormat);
pMediaRecorder.setOutputFile(currentRecordFile.getPath());
pMediaRecorder.setAudioEncoder(pAudioCodec);
pMediaRecorder.setMaxDuration(pAudioDuration * 1000);
pMediaRecorder.setMaxFileSize(pAudioSize * 1024);
pMediaRecorder.prepare();
pMediaRecorder.start();
pCurrentRecordStartTime = System.currentTimeMillis();
pCurrentObserver = new RecordObserver(this, currentRecordFile.getAbsolutePath());
pCurrentObserver.startWatching();
isRecording = true;
}catch (IllegalStateException isex) {
Toast.makeText(this, R.string.ancare_service_recording_failed, Toast.LENGTH_LONG).show();
isRecording = false;
if(pMediaRecorder != null){
pMediaRecorder.release();
pMediaRecorder = null;
}
}catch(IOException ioex) {
Toast.makeText(this, R.string.ancare_service_recording_failed, Toast.LENGTH_LONG).show();
isRecording = false;
if(pMediaRecorder != null){
pMediaRecorder.release();
pMediaRecorder = null;
}
}
}
return pMediaRecorder;
}
public void stopRecordCall(){
if(isRecording){
long callDuration = System.currentTimeMillis() - pCurrentRecordStartTime;
pCurrentObserver.setCallDuration(callDuration);
pMediaRecorder.stop();
pMediaRecorder.release();
pMediaRecorder = null;
isRecording = false;
Toast.makeText(this, R.string.ancare_service_recording_succeeded, Toast.LENGTH_LONG).show();
pAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, pCurrentAudioVolume, 0);
}
}
在Android 5.1.1上是否有任何改变,或者它来自我的代码,