美文网首页
最简单的边录音边播放 AudioRecord AudioTrac

最简单的边录音边播放 AudioRecord AudioTrac

作者: 有_风 | 来源:发表于2019-12-05 11:46 被阅读0次
            button.setOnClickListener(v -> {
                simpleRateInHz = 22050;
                bufferSize = AudioRecord.getMinBufferSize(simpleRateInHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
                audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, simpleRateInHz, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
                whetherRecord = true;
                audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, simpleRateInHz, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
                //开始录音
                a();
            });
        }
    
        private void a() { 
            new Thread(() -> {
                audioRecord.startRecording();
                while (true) {
                    short[] audioBuffer = new short[bufferSize];
                    int bufferReadResult = audioRecord.read(audioBuffer, 0, bufferSize);
                    audioTrack.write(audioBuffer, 0, bufferReadResult);
                    audioTrack.play();
                }
            }).start();
        }
    

    最基础的实现方式, 资源释放, 及参数设置等, 参考其他大神的文章.

    相关文章

      网友评论

          本文标题:最简单的边录音边播放 AudioRecord AudioTrac

          本文链接:https://www.haomeiwen.com/subject/eordgctx.html