前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AudioTrack和AudioRecord使用

AudioTrack和AudioRecord使用

作者头像
一只小虾米
发布2022-10-25 16:28:51
4660
发布2022-10-25 16:28:51
举报
文章被收录于专栏:Android点滴分享

本篇介绍

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

AudioRecord的使用

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

代码语言:javascript
复制
    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播放出来

代码语言:javascript
复制
 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)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本篇介绍
  • AudioRecord的使用
  • AudioTrack的使用
  • 查看pcm数据
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档