AudioRecord recording and playback Demo

#AudioRecord recording and playback Demo

This article describes a simple AudioRecord recording and playback of the sample program.

Recording is dynamically obtain permission!

## major code

### 1.AudioRecord Recording Code

//开通输出流到指定的文件,audioFile是保存的音频文件File对象
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(audioFile)));
//根据定义好的几个配置,来获取合适的缓冲大小
int bufferSize = AudioRecord.getMinBufferSize(frequence, channelConfig, audioEncoding);
Log.i(TAG, "RecordTask: dataSize=" + bufferSize);//1280
//实例化AudioRecord//MediaRecorder.AudioSource.MIC
AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC, frequence, channelConfig, audioEncoding, bufferSize);
//开始录制
record.startRecording();
byte audioData[] = new byte[bufferSize];
//定义循环,根据isRecording的值来判断是否继续录制
long beforeTime = 0;
while (isRecording) {
    int number = record.read(audioData, 0, bufferSize);
    dos.write(audioData);
}
//录制结束
record.stop();


### 2.AudioTrack play an audio file code Code

int dataSize = AudioRecord.getMinBufferSize(frequence, channelConfig, audioEncoding);
Log.i(TAG, "PlayTask: dataSize=" + dataSize);
byte[] bytes = new byte[dataSize];          
    //定义输入流,将音频写入到AudioTrack类中,实现播放
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(audioFile)));
    //实例AudioTrack
    AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, frequence, channelConfig, audioEncoding, dataSize, AudioTrack.MODE_STREAM);
    //开始播放
    track.play();
    //由于AudioTrack播放的是流,所以,我们需要一边播放一边读取
    while (isPlaying && (dataSize = dis.read(bytes)) != -1) {
        Log.i(TAG, "dataSize:" + dataSize);
        if (dataSize > 0) {
            track.write(bytes, 0, dataSize);
        }
    }
    //播放结束
    track.flush();
        

Code control is not difficult, just to tidy up.
It is generally written in complete thread inside.

## my simple recording Demo

Achieved, recording, stop, play three functions
and program startup, dynamic request permission to read and write permissions and recording,
the program starts in /sdcard/Sounds/time.pcm(time years is the current time to generate a recording file day when minutes and seconds)

image:

1

I comes back recordings apk file and code
required can be further modified and iteration.

## other knowledge about Audio

AudioRecord is generated pcm audio file format is a format of an audio file,
and mp3, wav relevant data is encapsulated in an audio file.
To generate AudioRecord recording mp3 or wav is to add some data in the header,
the specific data format can surf the Internet and tools, even before the success, but there will be some pit!
AudioRecord recording parameters can be set more than MediaPlayer,
MediaPlayer is the result of a lot of packages, and can record directly to wav audio file.

AudioTrack only play pcm files can not play wav or mp3 files directly,
if you want to play is first processed data.
The MediaPlayer can play a wav and mp3's.

If an ordinary occasions MediaPlayer record and play audio files is indeed a lot of simple,
but there will be some scenes AudioRecord can only be used for recording,
such as some need to use the recorded data compression processing, segmentation upload this operation, an audio transmission is generally pcm data.

# Mutual encouragement: Do not go too much to enjoy in the old struggle, or you'll go to suffer and enjoy in this time.

Published 365 original articles · won praise 1587 · Views 1.6 million +

Guess you like

Origin blog.csdn.net/wenzhi20102321/article/details/88774094