AudioRecord

AudioRecord简介

1.AudioRecord与MediaRecorder一样用来录制音频的
2.AudioRecord可以对录制的数据进行实时的处理,比如降噪,除杂,或者将音频进行实时传输,比如IP电话,对讲功能等操作。
3.AudioRecord比MediaRecorder更接近底层,录制的音频是PCM格式的,只能用AudioTraker进行播放,或者将PCM数据转换成amr,wav等格式播放。

AudioRecord使用

AudioRecord以pulling方式通过read()方法对缓冲区的数据进行轮询,来实时的获取缓冲区的数据。
下面来看如何使用:

// 读取数据流  返回int,读取到的字节数
public int read (byte[] audioData, int offsetInBytes, int sizeInBytes)
public int read (float[] audioData, int offsetInFloats, int sizeInFloats, int readMode)
public int read (ByteBuffer audioBuffer, int sizeInBytes, int readMode)

// 构造方法
public AudioRecord (int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes)

    audioSource:VOICE_CALL,VOICE_COMMUNICATION。。。。
    sampleRateInHz  44100,22050,16000
    channelConfig   CHANNEL_IN_MONO and CHANNEL_IN_STEREO

    audioFormat ENCODING_PCM_8BIT ENCODING_PCM_16BIT ENCODING_PCM_FLOAT.
    bufferSizeInBytes   由getMinBufferSize(int, int, int)决定


// 获取成功创建AudioRecord 的最小缓冲区的大小
public static int getMinBufferSize (int sampleRateInHz, int channelConfig, int audioFormat)

AudioTrack

说了录音,那么播放就得说到AudioTrack,专门用来播放PCM数据.

下面是录制与播放的代码,时间太晚了,还没有测试。

package com.asia.pcm2amr;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.annotation.TargetApi;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class AudioTest {

    static String mPath = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/AAA/";
    private AudioRecord mAudioRecord;
    private AudioTrack mAudioTrack;
    private boolean mIsAudioing = false;
    private boolean mIsPlaying = false;
    private int mMinBufferSize;
    private int mRecordTime = 20;
    private int mFileCount = 10;
    private static AudioTest mAudioTest;

    public static AudioTest getInstance() {

        if (mAudioTest == null) {

            mAudioTest = new AudioTest();
        }
        return mAudioTest;
    }

    public void createObject(int sampleRateInHz) {
        int audioSource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;
        int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
        int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

        mMinBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,
                channelConfig, audioEncoding);

        int bufferSizeInBytes = mMinBufferSize * 2;
        mAudioRecord = new AudioRecord(audioSource, sampleRateInHz,
                channelConfig, audioEncoding, bufferSizeInBytes);

        int playBufSize = AudioTrack.getMinBufferSize(sampleRateInHz,
                channelConfig, audioEncoding);
        mAudioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                sampleRateInHz, channelConfig, audioEncoding, playBufSize,
                AudioTrack.MODE_STREAM, mAudioRecord.getAudioSessionId());
    }

    public boolean isInitSucces() {

        boolean initRecorder = mAudioRecord != null
                && mAudioRecord.getState() == AudioRecord.STATE_INITIALIZED;
        boolean initPlayer = mAudioTrack != null
                && mAudioTrack.getState() == AudioTrack.STATE_INITIALIZED;
        return initRecorder && initPlayer;
    }

    public void startAudio() {

        mIsAudioing = true;
        new AudioThread(++mFileCount + ".pcm").start();
    }

    public void stopAudio() {

        mIsAudioing = false;
        if (mAudioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {

            mAudioRecord.stop();
        }
    }

    public void startPlayer() {

        mIsPlaying = true;
        new PlayThread().start();
    }

    public void stopPlayer() {

        mIsPlaying = false;
        if (mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {

            mAudioTrack.stop();
        }
    }

    /**
     * 结束录音与播放
     */
    public void stopVoice() {

        stopAudio();
        stopPlayer();

        mAudioRecord.release();
        mAudioRecord = null;
        mAudioTrack.release();
        mAudioTrack = null;
    }

    private class PlayThread extends Thread {

        public void run() {

            try {
                android.os.Process
                        .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
            } catch (Exception e) {
            }

            try { 
                File file = new File(mPath, 15 + ".pcm");
                FileInputStream inputStream = new FileInputStream(file);

                byte[] audioData = new byte[mMinBufferSize];
                mAudioTrack.play();
                while (mIsPlaying) {

                    // 数据源
                    int read = inputStream.read(audioData, 0, mMinBufferSize);
                    mAudioTrack.write(audioData, 0, read);
                }

                inputStream.close();
                mAudioTrack.stop();
            } catch (Exception e) {

            }
        }
    }

    private class AudioThread extends Thread {

        String fileName;

        public AudioThread(String fileName) {

            this.fileName = fileName;
        }

        @Override
        public void run() {

            try {
                android.os.Process
                        .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
            } catch (Exception e) {
            }

            try {
                mAudioRecord.startRecording();
                byte[] buffer = new byte[mMinBufferSize];

                File file = new File(mPath, "" + fileName);

                FileOutputStream outputStream = new FileOutputStream(file);

                while (mIsAudioing) {

                    int read = mAudioRecord.read(buffer, 0, mMinBufferSize);
                    for (int i = 0; i < read; i++) {
                        byte b = buffer[i];
                        // 写到文件
                        outputStream.write(buffer, 0, read);
                    }
                }

                mAudioRecord.stop();
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        };
    }
}

你可能感兴趣的:(多媒体开发)