关闭

用AudioTrack进行音频录制传输

221人阅读 评论(0) 收藏 举报
AudioTrack用的也是socket进程间传输机制,用read方法获得底层音频数据。本程序中封装代码:

public class CustomAudioRecorder {
private AudioRecordResult audioResult = null;
private boolean bRecordThreadRuning = false;
private int m_in_buf_size = 0;
private byte[] m_in_bytes = null;
private AudioRecord m_in_rec = null;
private Thread recordThread = null;

public CustomAudioRecorder(AudioRecordResult paramAudioRecordResult) {
this.audioResult = paramAudioRecordResult;
initRecorder();
}
public void StartRecord()
 {
try {
if (this.bRecordThreadRuning)
       return;
     this.bRecordThreadRuning = true;
     this.recordThread = new Thread(new RecordThread());
     this.recordThread.start();
catch (Exception e) {
// TODO: handle exception
releaseRecord();
}
finally{
}
 }

public void StopRecord() {
synchronized (this) {
if (m_in_rec == null) {
return;
}

bRecordThreadRuning = false;
try {
recordThread.join();
} catch (Exception e) {
// TODO: handle exception
}
recordThread = null;
}
}

public boolean initRecorder() {
Log.i("info", "initRecorder()");
this.m_in_buf_size = AudioRecord.getMinBufferSize(8000, 2, 2);
this.m_in_rec = new AudioRecord(1, 8000, 2, 2, this.m_in_buf_size);
if (this.m_in_rec == null) {
return false;
}
this.m_in_bytes = new byte[this.m_in_buf_size];
return true;
}

public void releaseRecord() {
Log.d("tag", "releaseRecord");
this.m_in_rec.release();
this.m_in_rec = null;
}

public static abstract interface AudioRecordResult {
public abstract void AudioRecordData(byte[] paramArrayOfByte,
int paramInt);
}

class RecordThread implements Runnable {
RecordThread() {
}

public void run() {
m_in_rec.startRecording();
while (bRecordThreadRuning) {
int i = m_in_rec.read(m_in_bytes, 0, m_in_buf_size);
if (i == 0) {
return;
}
if (audioResult == null)
continue;
                              / /回调函数,返回数据
audioResult.AudioRecordData(m_in_bytes, i);
}
if (!bRecordThreadRuning) {
m_in_rec.stop();
return;
}
}
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24130次
    • 积分:202
    • 等级:
    • 排名:千里之外
    • 原创:95篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档