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

Media Recorder が実際に準備されているかどうかを確認してください。そうでない場合: 開始レコードを呼び出す前に、準備が整うまで数ミリ秒待ちます。

また、さまざまなレコーダーの状態の詳細については、Android 開発者のリファレンスを確認してください: http://developer.android.com/reference/android/media/MediaRecorder.html

于 2013-11-02T22:38:35.473 に答える