一个按钮的两个动作android(android)

时间:2014-04-04 19:13:08

标签: java android

Android应用程序的代码存在问题

我尝试将多个操作分配给一个按钮。这是代码:

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    AudioRecord audioRecord;
    private Thread recordingThread = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start_button = (Button) findViewById(R.id.button1);
        stop_button = (Button) findViewById(R.id.button2);
        start_button.setOnClickListener(this);
        stop_button.setOnClickListener(this);
        createAudioRecorder();
    }

    private void createAudioRecorder() {
        int sampleRate = 16000;
        int channelConfig = AudioFormat.CHANNEL_IN_MONO;
        int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

        int minInternalBufferSize = AudioRecord.getMinBufferSize(sampleRate,
                channelConfig, audioFormat);
        int internalBufferSize = minInternalBufferSize * 4;
        Log.d(TAG, "minInternalBufferSize = " + minInternalBufferSize
                + ", internalBufferSize = " + internalBufferSize
                + ", myBufferSize = " + myBufferSize);

        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                sampleRate, channelConfig, audioFormat, internalBufferSize);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            reading = true;
            startRecorder();
            break;
        case R.id.button2:
            reading = false;
            stopRecorder();
            break;
        }
    }

    private void startRecorder() {
        recordingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                AudioData();
            }
        }, "AudioRecorder Thread");

        recordingThread.start();
    }

    protected void AudioData() {
        audioRecord.startRecording();
        byte data[] = new byte[myBufferSize];
        byte[] myBuffer = new byte[myBufferSize];
        int readCount = 0;
        int totalCount = 0;
        while (reading) {
            readCount = audioRecord.read(myBuffer, 0, myBufferSize);
            data = myBuffer;
            totalCount += readCount;
        }
    }

    private void stopRecorder() {
        if (null != audioRecord) {
            reading = false;
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
            recordingThread = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

当我第一次运行此代码时,但不遵循..

日志显示此处的错误(NullPointerException):

audioRecord.startRecording();

帮助解决问题!

1 个答案:

答案 0 :(得分:0)

点击button2后,系统会调用stopRecorder(),为audioRecord分配空值。因此,下次button1被点击时,生成的新主题会在调用NullPointerException时抛出AudioData()

也许你可以消除createAudioRecorder()并将其逻辑移到AudioData()的开头。

另外,我会将AudioData()重命名为recordAudioData(),它看起来像是一个类名。这将更符合标准的Java命名约定。