gpt4 book ai didi

Android MediaRecorder 在某些设备上不工作

转载 作者:行者123 更新时间:2023-11-29 16:37:57 26 4
gpt4 key购买 nike

我在后台服务中运行 Media Recorder,它在 Samsung 6.0 及更高版本的设备上运行完美,但在较低的设备上无法运行。抛出以下异常

stop called in an invalid state: 4

FATAL EXCEPTION: main
Process: com.shoaibnwar.crighter, PID: 28547
java.lang.IllegalStateException
at android.media.MediaRecorder._stop(Native Method)
at android.media.MediaRecorder.stop(MediaRecorder.java:946)
at com.shoaibnwar.crighter.Services.RecordingAudio.stopRecording(RecordingAudio.java:114)
at com.shoaibnwar.crighter.Services.RecordingAudio.access$000(RecordingAudio.java:24)
at com.shoaibnwar.crighter.Services.RecordingAudio$1.run(RecordingAudio.java:69)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

这是我开始录制的代码

private void startRecording(File file) {

if (mediaRecorder != null) {
mediaRecorder.reset();
mediaRecorder.release();
}
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_2_TS);
// mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
//mediaRecorder.setOutputFile(file.getAbsolutePath());

mediaRecorder.setOutputFile(file.getAbsolutePath());
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
mediaRecorder.setAudioEncodingBitRate(48000);
} else {
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setAudioEncodingBitRate(64000);
}
mediaRecorder.setAudioSamplingRate(16000);*/
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
Log.e("giftlist", "io problems while preparing [" +
file.getAbsolutePath() + "]: " + e.getMessage());
}
}

停止录音的功能是

private void stopRecording() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
}

我正在设置如下文件路径

private File getOutputFile() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);

return new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()
+File.separator
+Environment.DIRECTORY_MUSIC
+File.separator
+ "/CRighterVoice/RECORDING_"
+ dateFormat.format(new Date())
+ ".m4a");


}

最佳答案

试着打电话

mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

之前

mediaRecorder.setOutputFile(file.getAbsolutePath());

关于Android MediaRecorder 在某些设备上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52084401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com