服务启动后,我会从startRecording()
拨打onStartCommand()
,当服务结束时,会调用onDestroy()
,我可以从中呼叫stoprecording()
。但是recorder.stop()
给了我一个IllegalStageException
void startRecording() {
recordList = new ArrayList<>();
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
recordListString = mSharedPreferences.getString("RecordList", null);
if(recordListString!=null){
recordList = new ArrayList(Arrays.asList(TextUtils.split(recordListString, ",")));
}
currentFormat = Integer.parseInt(mSharedPreferences.getString("PREF_REC_LIST", "1"));
tempRecNum = mSharedPreferences.getString("TempRecNum", null);
audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
recorder = new MediaRecorder();
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),0);
if (error){
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
}else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
cleanDate = System.currentTimeMillis();
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
/**recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(96000);**/
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
mSharedPreferences.edit().putBoolean("isRecording", true).apply();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (RuntimeException e) {
File filepath = Environment.getExternalStorageDirectory();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
String tempPath = file.getAbsolutePath();
if(contactExists) {
boolean err = new File(tempPath + "/" +
getContactDisplayNameByNumber(tempRecNum, this)+"___"+
tempRecNum+"|__"+recCount+file_exts[currentFormat]).delete();
recordList.remove(recordList.size()-1);
}else{
boolean err = new File(tempPath + "/" +
"___"+tempRecNum+"|__"+recCount+file_exts[currentFormat]).delete();
recordList.remove(recordList.size()-1);
}
error = true;
startRecording();
}
}
private String getFilename() {
File filepath = Environment.getExternalStorageDirectory();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdir();
}
path = file.getAbsolutePath() + "/";
if(contactExists(this, tempRecNum, getContentResolver())) {
contactExists = true;
fileName = getContactDisplayNameByNumber(tempRecNum, this)+
"___"+tempRecNum+"|__"+recCount+file_exts[currentFormat];
recordList.add(fileName);
return (path + "/" + fileName);
}else {
contactExists = false;
fileName = "___"+tempRecNum+"|__"+recCount+file_exts[currentFormat];
recordList.add(fileName);
return (path + "/" + fileName);
}
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
edit().putBoolean("isRecording", false).apply();
Toast.makeText(RecorderService.this,
"Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
}
};
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Toast.makeText(RecorderService.this,
"Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
.show();
}
};
@Override public void onDestroy(){
stopRecording();
super.onDestroy();
}
void stopRecording(){
try{
if (recorder != null) {
mSharedPreferences.edit().putString("RecordList", TextUtils.join(",", recordList)).apply();
mSharedPreferences.edit().putInt("RecCount", recCount+1).apply();
recorder.stop(); //This is where the error is thrown.
recorder.release();
recorder = null;
}
}catch(RuntimeException stopException){
stopException.printStackTrace();
}
}