语音识别功能有一项需要手机端录制pcm文件,以此记录一下,以后需要了方便复制
代码如下:

  
    ExecutorService executorService;
    private String voicePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myAutoFile/xxx.pcm";
    private File mAudioFile;
    private AudioRecord mAudioRecord;
    private byte[] buffer;
    private static final int BUFFERSIZE = 2048;

    private Handler mHander = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            int what = message.what;
            switch (what){
                case 100:
                    Toast.makeText(VoiceDialogActivity.this,"请重试!",Toast.LENGTH_SHORT).show();
                    break;
                case 200:
                //录制成功,可以做一些操作,不如上传录音文件
                   
                    break;

            }
            return true;
        }
    });

  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        initData();
      

    }



  
    private void initData() {
        //创建一个单线程来录制声音
        executorService = Executors.newSingleThreadExecutor();
        buffer = new byte[BUFFERSIZE];
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    doStart();
                }
            });

    }

    private void doStart() {
        try {
           
            //创建录音文件
            mAudioFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myAutoFile/sanyangplay.pcm");
            mAudioFile.getParentFile().mkdirs();
            mAudioFile.createNewFile();

            //创建文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream(mAudioFile);
            //配置audio
            //配置Audio Record
            int minBufferSize = AudioRecord.getMinBufferSize(16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
            mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
            mAudioRecord.startRecording();
            while (mIsRecording){
                int read = mAudioRecord.read(buffer, 0, BUFFERSIZE);
                if (read > 0){
                    //读取成功
                    fileOutputStream.write(buffer,0,read);

                }else {
                    //读取失败

                }
            }
            fileOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();

        }
    }

  
   
    private void stopRecord() {
        //停止录音 关闭文件输出流
        if (mAudioRecord != null){
            mAudioRecord.stop();
            mAudioRecord.release();
            mAudioRecord = null;

        }
        mHander.sendEmptyMessage(200);

    }

    private int dp2px(int dpValue) {
        return (int) getResources().getDisplayMetrics().density * dpValue;
    }

    public void  releaseRecorder(){
        if (executorService != null){
            executorService.shutdownNow();
            executorService = null;
        }
        if (mAudioRecord != null){
            mAudioRecord.release();
            mAudioRecord = null;
        }
        if (mAudioFile != null){

            if (mAudioFile.exists() && mAudioFile.length()>0){
                mAudioFile.delete();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseRecorder();
    }
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐