美文网首页
AudioTrack和AudioRecord使用

AudioTrack和AudioRecord使用

作者: android小奉先 | 来源:发表于2021-08-21 17:20 被阅读0次

    本篇介绍

    本篇介绍下AudioTrack和AudioRecord的使用,通过AudioRecord录音,通过AudioTrack播放录制的音频。

    AudioRecord的使用

    AudioRecord负责采集音频,下面是一个录音的例子:

        private void startRecord() {
            int sampleRate = 44100;
            int bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT);
            recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT, bufferSize);
            recorder.startRecording();
            currentState = 0;
            Log.d(TAG, "start record");
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    byte byteBuffer [] = new byte[1024];
                    FileOutputStream outputStream = null;
                    try {
                        outputStream = new FileOutputStream(PCM_FILE);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        return;
                    }
                    while (currentState != 1) {
                        int read = recorder.read(byteBuffer, 0, byteBuffer.length);
                        Log.d(TAG, "size is " + read);
                        try {
                            outputStream.write(byteBuffer, 0, read);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        outputStream.flush();
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    recorder.stop();
                }
            });
        }
    

    先创建一个AudioRecord,创建的时候需要指定音频源,采集频率,采集位数,声道数,还有buffer大小,AUdioRecord提供了专门的方法来计算buffer大小。创建好后,调用startRecording就可以采集了。具体采集需要在单独的线程里面做,调用AudioRecord的read就可以,读出来的内容就是采集的数据,这儿是写到了一个文件里面。

    AudioTrack的使用

    AudioTrack负责播放pcm数据,下面的代码是将刚采集的pcm播放出来

     AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).setUsage(AudioAttributes.USAGE_MEDIA).build();
            AudioFormat audioFormat = new AudioFormat.Builder().setEncoding(AudioFormat.ENCODING_PCM_8BIT).setSampleRate(44100).setChannelMask(AudioFormat.CHANNEL_OUT_MONO).build();
            int bufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT);
            tracker = new AudioTrack(audioAttributes, audioFormat, bufferSize, AudioTrack.MODE_STREAM, AudioManager.AUDIO_SESSION_ID_GENERATE);
            tracker.play();
            currentState = 2;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    byte[] data = new byte[1024]; // small buffer size to not overflow AudioTrack's internal buffer
                    FileInputStream fileInputStream = null;
                    try {
                        fileInputStream = new FileInputStream(new File(PCM_FILE));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    int readSize = 0;
                    while (readSize != -1) {
                        try {
                            readSize = fileInputStream.read(data);
                        } catch (IOException e) {
                            e.printStackTrace();
                            continue;
                        }
                        Log.d(TAG, "start play read size is " + readSize);
                        if (readSize > 0) {
                            tracker.write(data, 0, readSize);
                        }
                    }
                    try {
                        fileInputStream.close();
                    }
                    catch (IOException e) {
                        // handle exception
                    }
    
                    tracker.stop();
                    tracker.release();
                }
            });
    

    首先是创建AudioTrack,也是需要指定采集位数,采集频率,输出声道,播放buffer,然后调用play就可以播放了,具体的播放就是在单独线程里面调用AudioTrack的write即可。

    查看pcm数据

    使用audacity就可以查看pcm数据,(下载路径 https://www.audacityteam.org/)
    效果如下:

    image.png
    完整代码路径(https://github.com/leehaoran/opengl/tree/main/audioexample)

    相关文章

      网友评论

          本文标题:AudioTrack和AudioRecord使用

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