设为首页 加入收藏

TOP

Android仿微信录音功能,自定义控件的设计技巧(二)
2015-02-02 14:37:06 来源: 作者: 【 】 浏览:27
Tags:Android 录音 功能 定义 控件 设计 技巧
= null && running) {
? ? ? ? ? ? ? ? ? ? // 如果用户仍在录音
? ? ? ? ? ? ? ? ? ? int volumn = mAudioUtil.getVolumn();
? ? ? ? ? ? ? ? ? ? if (volumn != 0)
? ? ? ? ? ? ? ? ? ? ? ? mVolumeHandler.sendEmptyMessage(volumn);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? exit();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? private final OnDismissListener onDismiss = new OnDismissListener() {
? ? ? ? @Override
? ? ? ? public void onDismiss(DialogInterface dialog) {
? ? ? ? ? ? stopRecording();
? ? ? ? }
? ? };
? ? static class ShowVolumeHandler extends Handler {
? ? ? ? private final WeakReference mOuterInstance;
? ? ? ? public ShowVolumeHandler(RecordButton outer) {
? ? ? ? ? ? mOuterInstance = new WeakReference(outer);
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void handleMessage(Message msg) {
? ? ? ? ? ? RecordButton outerButton = mOuterInstance.get();
? ? ? ? ? ? if (msg.what != -1) {
? ? ? ? ? ? ? ? // 大于0时 表示当前录音的音量
? ? ? ? ? ? ? ? if (outerButton.mVolumeListener != null) {
? ? ? ? ? ? ? ? ? ? outerButton.mVolumeListener.onVolumeChange(mRecordDialog,
? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.what);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? // -1 时表示录音超时
? ? ? ? ? ? ? ? outerButton.finishRecord();
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? /** 音量改变的监听器 */
? ? public interface OnVolumeChangeListener {
? ? ? ? void onVolumeChange(Dialog dialog, int volume);
? ? }
? ? public interface OnFinishedRecordListener {
? ? ? ? /** 用户手动取消 */
? ? ? ? public void onCancleRecord();
? ? ? ? /** 录音完成 */
? ? ? ? public void onFinishedRecord(String audioPath, int recordTime);
? ? }
}


?


/**
?* {@link #RecordButton}需要的工具类
?*
?* @author kymjs(kymjs123@gmail.com)
?*/
public class RecordButtonUtil {
? ? public static final String AUDOI_DIR = Environment
? ? ? ? ? ? .getExternalStorageDirectory().getAbsolutePath() + "/oschina/audio"; // 录音音频保存根路径
?
? ? private String mAudioPath; // 要播放的声音的路径
? ? private boolean mIsRecording;// 是否正在录音
? ? private boolean mIsPlaying;// 是否正在播放
? ? private OnPlayListener listener;
?
? ? // 初始化 录音器
? ? private void initRecorder() {
? ? ? ? mRecorder = new MediaRecorder();
? ? ? ? mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
? ? ? ? mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
? ? ? ? mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
? ? ? ? mRecorder.setOutputFile(mAudioPath);
? ? ? ? mIsRecording = true;
? ? }
?
? ? /** 开始录音,并保存到文件中 */
? ? public void recordAudio() {
? ? ? ? initRecorder();
? ? ? ? try {
? ? ? ? ? ? mRecorder.prepare();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? mRecorder.start();
? ? }
?
? ? /** 获取音量值,只是针对录音音量 */
? ? public int getVolumn() {
? ? ? ? int volumn = 0;
? ? ? ? // 录音
? ? ? ? if (mRecorder != null && mIsRecording) {
? ? ? ? ? ? volumn = mRecorder.getMaxAmplitude();
? ? ? ? ? ? if (volumn != 0)
? ? ? ? ? ? ? ? volumn = (int) (10 * Math.log(volumn) / Math.log(10)) / 7;
? ? ? ? }
? ? ? ? return volumn;
? ? }
?
? ? /** 停止录音 */
? ? public void stopRecord() {
? ? ? ? if (mRecorder != null) {
? ? ? ? ? ? mRecorder.stop();
? ? ? ? ? ? mRecorder.release();
? ? ? ? ? ? mRecorder = null;
? ? ? ? ? ? mIsRecording = false;
? ? ? ? }
? ? }
?
? ? public void startPlay(String audioPath) {
? ? ? ? if (!mIsPlaying) {
? ? ? ? ? ? if (!StringUtils.isEmpty(audioPath)) {
? ? ? ? ? ? ? ? mPlayer = new MediaPlayer();
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? mPlayer.setDataSource(audioPath);
? ? ? ? ? ? ? ? ? ? mPlayer.prepare();
? ? ? ? ? ? ? ? ? ? mPlayer.start();
? ? ? ? ? ? ? ? ? ? if (listener != null) {
? ? ? ? ? ? ? ? ? ? ? ? listener.starPlay();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mIsPlaying = true;
? ? ? ? ? ? ? ? ? ? mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onCompletion(MediaPlayer mp) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇为什么 C++ 成员函数指针是 16 字.. 下一篇浅谈 C++ 中的 new/delete 和 new..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: