package com.bairuitech.anychat;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.util.Log;
// AnyChat 音频包装类,实现音频采集、播放等
public class AnyChatAudioHelper {
private final static String TAG = "ANYCHAT";
private AudioTrack mAudioTrack = null;
private AudioRecord mAudioRecord = null;
private PlayAudioThread mPlayAudioThread = null; // 播放线程
private boolean mPlayThreadExitFlag = false; // 播放线程退出标志
private int mMinPlayBufSize = 0;
private boolean mAudioPlayReleased = false;
private RecordAudioThread mRecordAudioThread = null; // 采集线程
private boolean mRecordThreadExitFlag = false; // 采集线程退出标志
private int mMinRecordBufSize = 0;
private boolean mAudioRecordReleased = false;
// 初始化音频播放器
public int InitAudioPlayer( int profile) {
if(mAudioTrack != null)
return 0;
Log.d(TAG, "InitAudioPlayer, profile: " + profile);
int channel, samplerate, samplebit;
// 根据上层设定的profile来配置参数
if(profile==1) {
samplerate = 16000;
channel = AudioFormat.CHANNEL_CONFIGURATION_MONO;
samplebit = AudioFormat.ENCODING_PCM_16BIT;
}
else if(profile==2) {
samplerate = 44100;
channel = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
samplebit = AudioFormat.ENCODING_PCM_16BIT;
}
else {
return -1;
}
try {
mAudioPlayReleased = false;
// 获得构建对象的最小缓冲区大小
mMinPlayBufSize = AudioTrack.getMinBufferSize(samplerate, channel, samplebit);
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, samplerate, channel, samplebit, mMinPlayBufSize, AudioTrack.MODE_STREAM);
if (mPlayAudioThread == null)
{
mPlayThreadExitFlag = false;
mPlayAudioThread = new PlayAudioThread();
mPlayAudioThread.start();
}
Log.d(TAG, "mMinPlayBufSize = " + mMinPlayBufSize);
}
catch(Exception e)
{
return -1;
}
return 0;
}