一、原理
AudioRecord的执行过程:
int buffersize = AudioRecord.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,11025, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
audioRecord.startRecording();//开始录音
audioRecord.read(buffer, 0, bufferReadSize);
AudioTrack的执行过程:
int bufsize = AudioTrack.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
audioplayer = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, bufsize,AudioTrack.MODE_STREAM);
audioplayer.play();//开始接受数据流播放
audioplayer.write(buffer, 0, size);//不断播放数据
二、实现
1、添加权限:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2、MAudioPlayer.java文件:
package com.example.tongjiangsong.audiorecordtrackdemo;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
public class MAudioPlayer {
private PipedInputStream instream;
private boolean isPlaying ;
private AudioTrack audioplayer;
private byte[] buffer;
public MAudioPlayer() {
isPlaying = false;
instream = null;
//初始化播音类
int bufsize = AudioTrack.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
audioplayer = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, bufsize,AudioTrack.MODE_STREAM);
}
//设置管道流,用于接受音频数据
public void setOutputStream(PipedOutputStream out) throws IOException{
instream = new PipedInputStream(out);
}
public void startPlayAudio(){ //调用之前先调用setOutputStream 函数
isPlaying = true;
audioplayer.play();//开始接受数据流播放
buffer = new byte[1024];
while (instream!=null&&isPlaying){
try {
while (instream.available()>0){
int size = instream.read(buffer);
audioplayer.write(buffer, 0
, size);//不断播放数据
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stopPlay(){//停止播放
isPlaying = false ;
try {
instream.close();
} catch (IOException e) {
e.printStackTrace();
}
audioplayer.stop();
}
}
3、MAudioRecord.java文件:
package com.example.tongjiangsong.audiorecordtrackdemo;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
/*
* To getaudio or play audio
* */
public class MAudioRecord {
private AudioRecord audioRecord;
private Context context;
private boolean isRecording = false ;
private PipedOutputStream outstream ;//利用管道传输数据
public MAudioRecord(Context context , PipedInputStream instream) throws IOException {
this.context = context;
//初始化管道流 用于向外传输数据
outstream = new PipedOutputStream();
outstream.connect(instream);
}
public void StartAudioData(){//得到录音数据
int frequency = 11025;
int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int buffersize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration, audioEncoding, buffersize);
byte[]buffer = new byte[buffersize];
audioRecord.startRecording();//开始录音
isRecording = true;
int bufferReadSize = 1024;
while (isRecording){
audioRecord.read(buffer, 0, bufferReadSize);
try {
outstream.write(buffer, 0, bufferReadSize);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stopRecord(){//停止录音
isRecording = false;
audioRecord.stop();
try {
outstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record"
tools:layout_editor_absoluteX="108dp"
tools:layout_editor_absoluteY="162dp" />
</LinearLayout>