一、参考链接
https://blog.csdn.net/hlj_include/article/details/52131903
https://blog.csdn.net/Snow_Ice_Yang/article/details/85000427
该问题基本完成,以下代码可以参考。
二、代码逻辑
1. AudioRecord 获取audio数据流
2. 通过文件操作api把audio数据流按找2个字节左声道,2个字节右声道的方式送到两个不同的文件中去。
三、参考代码
while( m_stop_thread == false ){//kjc start record thread
ret = audioRecord.read(buf, 0, buf.length);
if( ret > 0 ){
synchronized (m_listeners){
for( IAudioRecordListener listener : m_listeners ){
listener.on_audio_frame( buf, ret );//kjc save audio data into mp4 files
//this code is for dual mic external data seprate if needed
//deleted here iavoid mp4 record unnormal
try{
if( m_tf_left == null ) {
m_tf_left = new File("/sdcard/test_left.pcm");
}
if( m_tf_os_left == null ) {
m_tf_os_left = new FileOutputStream(m_tf_left);
}
if( m_tf_right == null ) {
m_tf_right = new File("/sdcard/test_right.pcm");
}
if( m_tf_os_right == null ) {
m_tf_os_right = new FileOutputStream(m_tf_right);
}
for(i=0; i< buf_sz; i+=4){
m_tf_os_left.write(buf, i, 2);
m_tf_os_right.write(buf, i+2, 2);
}
}catch(Exception e){
}
}
}
}
}
完整代码文件如下:
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;
import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class AudioRecordAgent {
// ================================ single instance ===========================
private static AudioRecordAgent s_instance = null;
private static final String LOG_TAG = "kjc_AudioRecord";
private AudioRecordAgent(){
}
public static AudioRecordAgent getInstance(){
if( s_instance == null ) {
s_instance = new AudioRecordAgent();
}
return s_instance;
}
// ================================= listener ==================================
public interface IAudioRecordListener {
void on_audio_frame( byte[] data, int size );
}
private ArrayList<IAudioRecordListener> m_listeners = new ArrayList<>();
public void connect( IAudioRecordListener listener ){
synchronized (m_listeners) {
if( m_listeners.size() == 0 ){
start_recording();
}
m_listeners.add(listener);
}
}
public void disconnect( IAudioRecordListener listener ){
synchronized (m_listeners) {
m_listeners.remove(listener);
if( m_listeners.size() == 0 ){
stop_recording();
}
}
}
// =========================== audio recoding ===================================
public static final int S_AUDIO_SAMPLE_RATE = 16000;
public static final int S_AUDIO_CHANNEL_NUM = 2;
private Thread m_record_thread = null;
private boolean m_stop_thread = false;
private Runnable m_run_record = new Runnable() {
@Override
public void run() {//kjc config record parameters
Log.d( LOG_TAG, "kjc>>>AudioRecord: config record parameters and start record thread\n");
int channel_config = (S_AUDIO_CHANNEL_NUM == 1) ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO;
int buf_sz = AudioRecord.getMinBufferSize(
S_AUDIO_SAMPLE_RATE, channel_config, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
S_AUDIO_SAMPLE_RATE, channel_config, AudioFormat.ENCODING_PCM_16BIT, buf_sz );
Log.d( LOG_TAG, "kjc>>>AudioRecord: buf_sz="+buf_sz);
int ret;
int i = 0;
byte[] buf = new byte[buf_sz];
File m_tf_left = null;
FileOutputStream m_tf_os_left = null;
File m_tf_right = null;
FileOutputStream m_tf_os_right = null;
audioRecord.startRecording();
while( m_stop_thread == false ){//kjc start record thread
ret = audioRecord.read(buf, 0, buf.length);
if( ret > 0 ){
synchronized (m_listeners){
for( IAudioRecordListener listener : m_listeners ){
listener.on_audio_frame( buf, ret );//kjc save audio data into mp4 files
//this code is for dual mic external data seprate if needed
//deleted here iavoid mp4 record unnormal
try{
if( m_tf_left == null ) {
m_tf_left = new File("/sdcard/test_left.pcm");
}
if( m_tf_os_left == null ) {
m_tf_os_left = new FileOutputStream(m_tf_left);
}
if( m_tf_right == null ) {
m_tf_right = new File("/sdcard/test_right.pcm");
}
if( m_tf_os_right == null ) {
m_tf_os_right = new FileOutputStream(m_tf_right);
}
for(i=0; i< buf_sz; i+=4){
m_tf_os_left.write(buf, i, 2);
m_tf_os_right.write(buf, i+2, 2);
}
}catch(Exception e){
}
}
}
}
}
audioRecord.stop();
audioRecord.release();
}
};
private void start_recording(){
if( m_record_thread != null ){
return;
}
m_stop_thread = false;
m_record_thread = new Thread(m_run_record);
m_record_thread.start();
}
private void stop_recording(){
if( m_record_thread == null ){
return;
}
try {
m_stop_thread = true;
// m_record_thread.join();
m_record_thread = null;
}
catch (Exception e) {
e.printStackTrace();
}
}
}