麦克风录音的问题 - (android)

时间:2014-03-23 14:49:27

标签: java 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;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

        Button start_button;
        Button stop_button;
        TextView text;
        boolean reading = false;
        final String TAG = "TAG";
        int myBufferSize = 8192;
        AudioRecord audioRecord;

        @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);
                text = (TextView) findViewById(R.id.textView1);
                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:
                        Log.d(TAG, "record start");
                        reading = true;
                        startRecorder();
                case R.id.button2:
                        Log.d(TAG, "stop recorder");
                        reading = false;
                        stopRecorder();
                }
        }

        private void startRecorder() {
                reading = true;
                Log.d(TAG, "recording...");
                audioRecord.startRecording();

                new Thread(new Runnable() {
                        @Override
                        public void run() {
                                if (audioRecord == null)
                                        return;

                                byte[] myBuffer = new byte[myBufferSize];
                                byte[] data = new byte[myBufferSize];
                                int readCount = 0;
                                int totalCount = 0;
                                while (reading) {
                                        readCount = audioRecord.read(myBuffer, 0, myBufferSize);
                                        data = myBuffer;
                                        totalCount += readCount;
                                        Log.d(TAG, "readCount = " + readCount + ", totalCount = "
                                                        + totalCount);
                                        Log.d(TAG, "lenght: " + data.length);
                                        Log.d(TAG, "data1: " + data[0]);
                                        Log.d(TAG, "data2: " + data[1]);
                                }
                        }
                }).start();
        }

        private void stopRecorder() {
                reading = false;
                Log.d(TAG, "record stop!");
                audioRecord.stop();
        }

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

        @Override
        protected void onDestroy() {
                super.onDestroy();
                reading = false;
                if (audioRecord != null) {
                        audioRecord.release();
                }
        }

}

日志:

03-23 15:48:51.028: D/TAG(782): minInternalBufferSize = 640, internalBufferSize = 2560, myBufferSize = 8192
03-23 15:48:52.928: D/TAG(782): record start
03-23 15:48:52.928: D/TAG(782): recording...
03-23 15:48:52.948: D/TAG(782): stop recorder
03-23 15:48:52.958: D/TAG(782): record stop!
03-23 15:48:53.018: D/TAG(782): readCount = 160, totalCount = 160
03-23 15:48:53.018: D/TAG(782): lenght: 8192
03-23 15:48:53.018: D/TAG(782): data1: 0
03-23 15:48:53.028: D/TAG(782): data2: 0
03-23 15:48:54.758: D/TAG(782): stop recorder
03-23 15:48:54.758: D/TAG(782): record stop!

通知?写入一个字节数组以停止录制。

为什么'停止记录'两次单词?虽然我点击停止按钮一次..

1 个答案:

答案 0 :(得分:0)

为什么不使用FileOutputStream?

开始记录:

   FileOutputStream fos = openFileOutput(__FILENAME__,Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            recorder.setOutputFile(fos.getFD());    
        recorder.prepare();
        recorder.start();

不要忘记停止并释放MediaRecorder。