我想创建一个录音机应用程序,但是当我点击"开始录制"它会崩溃按钮。 我在android.media.MediaRecorder.start(Native Method)中收到一条错误说java.lang.IllegalStateException。 我也附上了日志。
package com.example.sahil.chuckit;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import java.io.File;
public class MainActivity extends Activity {
private static Button submit;
private static Button submit2;
private static Button submit3;
private static Button submit4;
private MediaPlayer mediaPlayer;
private MediaRecorder recorder;
private String output_file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output_file = Environment.getExternalStorageState() + "/audiorecorder.3gpp";
OnClickButtonListener();OnClickButtonListener1();
OnClickButtonListener3();OnClickButtonListener4();
}
public void OnClickButtonListener(){
submit =(Button)findViewById(R.id.button);
submit.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
beginRecording();
}
}
);
}
public void OnClickButtonListener1(){
submit2 =(Button)findViewById(R.id.button2);
submit2.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
stopRecording();
}
}
);
}
public void OnClickButtonListener3(){
submit3 =(Button)findViewById(R.id.button3);
submit3.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
playRecording();
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
}
public void OnClickButtonListener4(){
submit4 =(Button)findViewById(R.id.button4);
submit4.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
stopPlayback();
}
}
);
}
private void ditchMediaRecorder() {
if (recorder != null)
recorder.release();
}
private void beginRecording() {
ditchMediaRecorder();
File outFile=new File(output_file);
if (outFile.exists())
{ outFile.delete();}
recorder=new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(output_file);
recorder.start();
}
private void stopRecording() {
if(recorder!=null)
recorder.stop();
}
private void playRecording() throws Exception {
ditchMediaPlayer();
mediaPlayer=new MediaPlayer();
mediaPlayer.setDataSource(output_file);
mediaPlayer.prepare();
mediaPlayer.start();
}
private void ditchMediaPlayer() {
if(mediaPlayer!=null)
{
try{
mediaPlayer.release();
}catch (Exception e){
e.printStackTrace();
}
}
}
private void stopPlayback() {
if (mediaPlayer!=null)
mediaPlayer.stop();
}
}
logcat的:
LOG:06-30 05:11:12.603 24621-24621/com.example.sahil.chuckit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sahil.chuckit, PID: 24621 java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.example.sahil.chuckit.MainActivity.beginRecording(MainActivity.java:111)
at com.example.sahil.chuckit.MainActivity.access$000(MainActivity.java:22)
at com.example.sahil.chuckit.MainActivity$1.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-30 05:11:14.891 24621-24621/com.example.sahil.chuckit I/Process: Sending signal. PID: 24621 SIG: 9
答案 0 :(得分:1)
您忘记在recorder.prepare()
功能recordeer.start()
功能之前致电beginRecording
。
准备功能将关注许多事情,例如将模拟数据转换为数字音频以进行压缩以及存储文件的位置等
答案 1 :(得分:0)
您必须考虑到,MediaRecorder以及MediaPlayer都有自己的状态机,强制您按特定顺序执行某些操作。
您尝试在准备MediaRecorder
时开始录制。致电
recorder.prepare();
在:
recorder.start();
答案 2 :(得分:0)
在setOutFormat()之后但prepare()之前调用此函数。
这就是我编写此方法名称时我的android studio文档对话框所说的内容。
关键是您应该在prepare()之前调用此方法。
这是一个例子:
private void startRecording() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MediaMaster/Dub/");
Log.i(TAG, "startRecording: creating output file " + outputFolder.mkdirs());
File output = new File(outputFolder.getAbsolutePath()+"out" + new Date().getTime() + ".3gpp");
mediaRecorder.setOutputFile(output.getAbsolutePath());
mediaRecorder.setMaxDuration(3000);
try {
mediaRecorder.prepare();
} catch (IOException e) {
Log.e(TAG, "startRecording: ", e);
}
mediaRecorder.start();
}
答案 3 :(得分:0)
`if (audioRecorder != null) {
try {
audioRecorder.stop();
audioRecorder.reset();
audioRecorder.release();
audioRecorder = null;
} catch (IllegalStateException e) {
e.printStackTrace();
}
}`
因为我们不释放记录器,而只是停止记录器 重新启动时,我们会到达android.media.MediaRecorder.start(本机方法) 花了1个小时才弄清
答案 4 :(得分:0)
由以下原因引起:android.media.MediaRecorder.native_start(本机方法)处的java.lang.IllegalStateException
我遇到了有关媒体记录器的问题。它记录的很完美,但是我尝试再次记录屏幕,然后出现此错误,媒体记录器崩溃了。在尝试了许多东西之后,我尝试了这个 mediarecorder.release(); 每次停止记录时,使用这种方法都会释放媒体记录器并使我能够再次记录。