我们如何解决这个错误? java.lang.IllegalStateException

时间:2013-11-02 22:16:56

标签: java android

我们应该如何修复此代码。我们已经花了24小时直接尝试在Android中保存音频

我们疲惫的眼睛会非常感谢你的帮助!

我们正在尝试创建一个分析麦克风输入的音频分析应用

package com.example.opus2;

import android.content.ContentValues;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;



public class audioTest extends MainActivity{

    public int saveMarker=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.save_test);
    }


    public void onClick(View view){
        switch (view.getId()){

            case R.id.saveBttn: {

                Button saveButton= (Button) findViewById(R.id.saveBttn);
                if(saveMarker==0){
                    TextView done = (TextView) findViewById(R.id.done_text);
                    done.setText("Done2");
                    saveButton.setText("Saved?");
                    saveMarker=1;
                    String save_file = "viola";
                    try {
                        recordAudio(save_file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    break;
                }
                else{
                    saveButton.setText("Save");
                    saveMarker=0;

                    break;
                }

            }
        }
    }

    public void recordAudio(String fileName) throws IOException {
        TextView done = (TextView) findViewById(R.id.done_text);
        done.setText("Done1");

        final MediaRecorder recorder = new MediaRecorder();
        ContentValues values = new ContentValues(3);
        values.put(MediaStore.MediaColumns.TITLE, fileName);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        recorder.setOutputFile("/sdcard/sound/" + fileName+".mp4");

        try {
            recorder.prepare();
        } catch (Exception e){
            e.printStackTrace();
        }

        /* final ProgressDialog mProgressDialog = new ProgressDialog(audioTest.this);
        mProgressDialog.setTitle(R.string.lbl_recording);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                mProgressDialog.dismiss();
                recorder.stop();
                recorder.release();
            }
        });

        mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface p1) {
                recorder.stop();
                recorder.release();
            }
        });
        mProgressDialog.show();
        */

        recorder.start();
        done = (TextView) findViewById(R.id.done_text);
        done.setText("Done");
        }
}

---- ERRORS ------

11-02 22:29:18.416    3256-3256/com.example.opus2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3599)
            at android.view.View.performClick(View.java:4204)
            at android.view.View$PerformClick.run(View.java:17355)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3594)
            at android.view.View.performClick(View.java:4204)
            at android.view.View$PerformClick.run(View.java:17355)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException
            at android.media.MediaRecorder.start(Native Method)
            at com.example.opus2.audioTest.recordAudio(audioTest.java:96)
            at com.example.opus2.audioTest.onClick(audioTest.java:40)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3594)
            at android.view.View.performClick(View.java:4204)
            at android.view.View$PerformClick.run(View.java:17355)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

如果实际准备好,请尝试检查Media Recorder的状态。如果不是:等待几毫秒,直到它准备好,然后再调用开始记录。

另请查看android开发人员的参考资料,了解有关不同录像机状态的更多信息: http://developer.android.com/reference/android/media/MediaRecorder.html