Android音频录制无法点击按钮

时间:2013-11-28 18:42:26

标签: android audio audio-recording android-mediarecorder

我正在尝试创建一个简单的应用程序来记录音频文件并将其存储在本地存储中,但由于某种原因,我的代码无效!我想知道我的代码是否有问题!请帮忙找出错误!

public class demo extends Activity implements OnClickListener {

    private static final String LOG_TAG = "AudioRecordTest";

    private static String mFileName = null;

    private MediaRecorder mRecorder = null;

    private MediaPlayer mPlayer = null;

    boolean mStartPlaying, mStartRecording;

    Button RecordButton, PlayButton;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    public demo() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "my_Record";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        mStartRecording = true;
        mStartPlaying = true;

        RecordButton = (Button) findViewById(R.id.btRecord);
        RecordButton.setOnClickListener(this);
        PlayButton = (Button) findViewById(R.id.btPlay);
        PlayButton.setOnClickListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch (arg0.getId()) {

        case R.id.btRecord: {

            onRecord(mStartRecording);
            if (mStartRecording) {
                RecordButton.setText("Stop recording");
            } else {
                RecordButton.setText("Start recording");
            }
            mStartRecording = !mStartRecording;
            break;
        }
        case R.id.btPlay: {

            onPlay(mStartPlaying);
            if (mStartPlaying) {
                PlayButton.setText("Stop playing");
            } else {
                PlayButton.setText("Start playing");
            }
            mStartPlaying = !mStartPlaying;
            break;
        }
        }
    }
}

LogCat输出:

11-28 13:57:01.562: E/AndroidRuntime(2237): FATAL EXCEPTION: main
11-28 13:57:01.562: E/AndroidRuntime(2237): Process: com.example.drishtikon, PID: 2237
11-28 13:57:01.562: E/AndroidRuntime(2237): java.lang.IllegalStateException
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.media.MediaRecorder.start(Native Method)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.example.drishtikon.demo.startRecording(demo.java:78)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.example.drishtikon.demo.onRecord(demo.java:35)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.example.drishtikon.demo.onClick(demo.java:129)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.view.View.performClick(View.java:4424)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.view.View$PerformClick.run(View.java:18383)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.os.Handler.handleCallback(Handler.java:733)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.os.Looper.loop(Looper.java:137)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at java.lang.reflect.Method.invoke(Method.java:515)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-28 13:57:01.562: E/AndroidRuntime(2237):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

这是文件路径(mFileName);

更正您的demo()方法。这就是它应该是这样的:

public demo() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/my_Record";
    }

当然,您需要在目录路径和文件名之间添加斜杠(/)。如果您使用OutputFormat.THREE_GPP,还可以尝试添加文件扩展名,例如“/my_Record.3gp”。