媒体录制器工作正常,但有时会出错。在不同设备上测试但发生相同的错误。他们没有像麦克风那样运行的后台服务......
E/MediaRecorder: start failed: -38
06-04 14:09:26.977 13458-13458/com.aykuttasil.callrecorder W/System.err: java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:946)
at com.aykuttasil.callrecord.receiver.CallRecordReceiver.startRecord(CallRecordReceiver.java:105)
at
..... com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 代码是
private void startRecord(Context context, String seed, String phoneNumber) {
try {
boolean isSaveFile = PrefsHelper.readPrefBool(context, CallRecord.PREF_SAVE_FILE);
Log.i(TAG, "isSaveFile: " + isSaveFile);
// dosya kayıt edilsin mi?
if (!isSaveFile) {
return;
}
if (isRecordStarted) {
try {
recorder.stop(); // stop the recording
} catch (RuntimeException e) {
// RuntimeException is thrown when stop() is called immediately after start().
// In this case the output file is not properly constructed ans should be deleted.
Log.d(TAG, "RuntimeException: stop() is called immediately after start()");
//noinspection ResultOfMethodCallIgnored
audiofile.delete();
}
releaseMediaRecorder();
isRecordStarted = false;
} else {
if (prepareAudioRecorder(context, seed, phoneNumber)) {
recorder.start();
isRecordStarted = true;
onRecordingStarted(context, callRecord, audiofile);
Log.i(TAG, "record start");
} else {
releaseMediaRecorder();
}
//new MediaPrepareTask().execute(null, null, null);
}
} catch (IllegalStateException e) {
e.printStackTrace();
releaseMediaRecorder();
} catch (RuntimeException e) {
e.printStackTrace();
releaseMediaRecorder();
} catch (Exception e) {
e.printStackTrace();
releaseMediaRecorder();
}
}
private boolean prepareAudioRecorder(Context context, String seed, String phoneNumber) {
try {
String file_name = PrefsHelper.readPrefString(context, CallRecord.PREF_FILE_NAME);
String dir_path = PrefsHelper.readPrefString(context, CallRecord.PREF_DIR_PATH);
String dir_name = PrefsHelper.readPrefString(context, CallRecord.PREF_DIR_NAME);
boolean show_seed = PrefsHelper.readPrefBool(context, CallRecord.PREF_SHOW_SEED);
boolean show_phone_number = PrefsHelper.readPrefBool(context, CallRecord.PREF_SHOW_PHONE_NUMBER);
int output_format = PrefsHelper.readPrefInt(context, CallRecord.PREF_OUTPUT_FORMAT);
int audio_source = PrefsHelper.readPrefInt(context, CallRecord.PREF_AUDIO_SOURCE);
int audio_encoder = PrefsHelper.readPrefInt(context, CallRecord.PREF_AUDIO_ENCODER);
File sampleDir = new File(dir_path /*+ "/" + dir_name*/);
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
StringBuilder fileNameBuilder = new StringBuilder();
fileNameBuilder.append(file_name);
fileNameBuilder.append("_");
if (show_seed) {
fileNameBuilder.append(seed);
fileNameBuilder.append("_");
}
if (show_phone_number) {
fileNameBuilder.append(phoneNumber);
fileNameBuilder.append("_");
}
file_name = fileNameBuilder.toString();
String suffix = "";
switch (output_format) {
case MediaRecorder.OutputFormat.AMR_WB: {
suffix = ".amr";
break;
}
case MediaRecorder.OutputFormat.THREE_GPP: {
suffix = ".3gp";
break;
}
default: {
suffix = ".amr";
break;
}
}
audiofile = File.createTempFile(file_name, suffix, sampleDir);
recorder = new MediaRecorder();
recorder.setAudioSource(audio_source);
recorder.setOutputFormat(output_format);
recorder.setAudioEncoder(audio_encoder);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mediaRecorder, int i, int i1) {
}
});
try {
recorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}