0

我们应该如何修复此代码。在过去的 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");
        }
}

----错误------

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)
4

1 回答 1

1

如果它真的准备好了,请尝试检查您的媒体记录器的状态。如果不是:在调用开始记录之前等待几毫秒,直到它准备好。

另请查看有关 android 开发人员的参考,以获取有关不同记录器状态的更多信息:http: //developer.android.com/reference/android/media/MediaRecorder.html

于 2013-11-02T22:38:35.473 回答