这几天主要在实现录音功能。
首先自定义了一个音频记录管理器AudioRecordManager用于录音,定义prepareAudio方法,用于设置输出路径,音频源、麦克风,输出格式和音频编码。
public void prepareAudio(){
try {
hasPrepare = false;
File dir = new File(mAudioDir);
if (!dir.exists()){
dir.mkdirs();
}
mMediaRecorder = new MediaRecorder();
String fileName = UUID.randomUUID().toString() + ".arm";
File file = new File(dir,fileName);
mCurrentFilePath = file.getAbsolutePath();
// 设置输出路径
mMediaRecorder.setOutputFile(mCurrentFilePath);
// 设置音频源,麦克风
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置输出格式
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
// 设置音频编码
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 准备
mMediaRecorder.prepare();
mMediaRecorder.start();
hasPrepare = true;
if (mStateListener != null){
mStateListener.prepareFinish(mCurrentFilePath);
}
} catch (IOException e) {
if (mStateListener != null){
mStateListener.prepareError(e.getMessage());
}
e.printStackTrace();
}
}
接下来获取音量等级,等级上限作为参数传入
public int getVoiceLevel(int maxLevel){
try {
if (hasPrepare){
// getMaxAmplitude = 0 - 32767
return maxLevel * mMediaRecorder.getMaxAmplitude() / 32768 + 1;
}
}catch (Exception e){
return 1;
}
return 1;
}
public void releaseAudio(){
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
hasPrepare = false;
}
public void cancelAudio(){
releaseAudio();
if (mCurrentFilePath != null){
File file = new File(mCurrentFilePath);
file.delete();
}
}
接下来定义音频对话管理器AudioDialogManager类用于在和好友的对话中发送录音。
private Context mContext;
private Dialog mDialog;
private LayoutInflater mInflater;
private ImageView mIvRecord;
private ImageView mIvVoiceLevel;
private TextView mTvTip;
public RecordDialogManager(Context context){
mContext = context;
mInflater = LayoutInflater.from(mContext);
}
显示之前的消息记录中的录音的方法:
public void showDialogRecord(){
View view = mInflater.inflate(R.layout.dialog_audio_record_button,null);
mDialog = new Dialog(mContext, R.style.Theme_Audio_Record_Button);
mDialog.setContentView(view);
mIvRecord = (ImageView) mDialog.findViewById(R.id.iv_record);
mIvVoiceLevel = (ImageView) mDialog.findViewById(R.id.iv_voice_level);
mTvTip = (TextView) mDialog.findViewById(R.id.tv_dialog_tip);
mDialog.show();
}
在聊天中发送录音的操作指示,包括取消发送、提示录音时间过短、取消发送
public void showRecording(){
if (mDialog != null && mDialog.isShowing()){
mIvRecord.setImageResource(R.drawable.recorder);
mIvVoiceLevel.setVisibility(View.VISIBLE);
mTvTip.setText(mContext.getString(R.string.move_up_cancel));
}
}
public void showDialogToShort(){
if (mDialog != null && mDialog.isShowing()){
mIvRecord.setImageResource(R.drawable.voice_to_short);
mIvVoiceLevel.setVisibility(View.GONE);
mTvTip.setText(mContext.getString(R.string.record_to_short));
}
}
public void showDialogWantCancel(){
if (mDialog != null && mDialog.isShowing()){
mIvRecord.setImageResource(R.drawable.cancel);
mIvVoiceLevel.setVisibility(View.GONE);
mTvTip.setText(mContext.getString(R.string.release_cancel));
}
}
根据音量大小更新音量图标高度
public void updateVoiceLevel(int level){
if (mDialog != null && mDialog.isShowing()){
int resId = mContext.getResources().getIdentifier("v"+level,
"drawable",mContext.getPackageName());
mIvVoiceLevel.setImageResource(resId);
}
}