在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android 录音、播放 示例源码

android 录音、播放 示例源码

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:1.41M
  • 下载次数:28
  • 浏览次数:270
  • 发布时间:2016-08-03
  • 实例类别:Android平台开发
  • 发 布 人:孤牧栀笙
  • 文件格式:.zip
  • 所需积分:3
 相关标签: 录音与播放

实例介绍

【实例简介】

【实例截图】

【核心代码】

package com.youmu.voicemanager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.youmu.voicemanager.utils.CommonDefine;
import com.youmu.voicemanager.utils.FileUtils;
import com.youmu.voicemanager.utils.TimeMethod;

public class VoiceManager {
    private ArrayList<File> mRecList = new ArrayList<File>();
    private Activity mActivity = null;
    private View mVRecRoot, mVPlaRoot, mVRecFinish;
    private ImageView mIVRecOperate, mIVPlaOperate, mIVPlaStop;
    private TextView mTVRecText, mTVRecTime, mTVPlaCurrent, mTVPlaTotal;
    private SeekBar mSBPlayProgress;
    private int mSavedState, mDeviceState = CommonDefine.MEDIA_STATE_UNDEFINE;
    private MediaRecorder mMediaRecorder = null;
    private MediaPlayer mMediaPlayer = null;
    private String mRecTimePrev, mPlaFilePath;
    private long mRecTimeSum = 0;
    private String filePath = "";// 录音所存放的位置  "/com.youmu.voicemanager/audio"
    private VoiceCallBack voiceCallBack;

    private MediaPlayer.OnCompletionListener mPlayCompetedListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mDeviceState = CommonDefine.MEDIA_STATE_PLAY_STOP;
            mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);

            mMediaPlayer.stop();
            mMediaPlayer.release();

            mSBPlayProgress.setProgress(0);
            mTVPlaCurrent.setText("00:00:00");
            mIVPlaOperate.setImageResource(R.mipmap.voice_play);
        }
    };

    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            TimeMethod ts;
            int current;
            try {
                switch (msg.what) {
                    case CommonDefine.MSG_TIME_INTERVAL:
                        if (mDeviceState == CommonDefine.MEDIA_STATE_RECORD_DOING) {
                            ts = TimeMethod.timeSpanToNow(mRecTimePrev);
                            mRecTimeSum  = ts.mDiffSecond;
                            mRecTimePrev = TimeMethod.getTimeStrFromMillis(ts.mNowTime);

                            ts = TimeMethod.timeSpanSecond(mRecTimeSum);
                            mTVRecTime.setText(String.format("%02d:%02d:%02d",
                                    ts.mSpanHour, ts.mSpanMinute, ts.mSpanSecond));

                            mHandler.sendEmptyMessageDelayed(CommonDefine.MSG_TIME_INTERVAL, 1000);

                        } else if (mDeviceState == CommonDefine.MEDIA_STATE_PLAY_DOING) {
                            current = mMediaPlayer.getCurrentPosition();
                            mSBPlayProgress.setProgress(current);

                            ts = TimeMethod.timeSpanSecond(current / 1000);
                            mTVPlaCurrent.setText(String.format("%02d:%02d:%02d",
                                    ts.mSpanHour, ts.mSpanMinute, ts.mSpanSecond));

                            mHandler.sendEmptyMessageDelayed(CommonDefine.MSG_TIME_INTERVAL, 1000);
                        }
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
            }
        }
    };

    public void setVoiceListener (VoiceCallBack callBack) {
        voiceCallBack = callBack;
    }

    public VoiceManager(Activity act, String filePath) {
        this.mActivity = act;
        this.filePath = filePath;

        mVRecRoot = mActivity.findViewById(R.id.unit_voice_record_root);
        mIVRecOperate = (ImageView) mActivity.findViewById(R.id.unit_voice_record_operate);
        mTVRecText = (TextView) mActivity.findViewById(R.id.unit_voice_record_text);
        mTVRecTime = (TextView) mActivity.findViewById(R.id.unit_voice_record_time);
        mVRecFinish = mActivity.findViewById(R.id.unit_voice_record_finish);

        mVPlaRoot = mActivity.findViewById(R.id.unit_voice_play_root);
        mIVPlaOperate = (ImageView) mActivity.findViewById(R.id.unit_voice_play_operate);
        mIVPlaStop = (ImageView) mActivity.findViewById(R.id.unit_voice_play_stop);
        mSBPlayProgress = (SeekBar) mActivity.findViewById(R.id.unit_voice_play_progress);
        mTVPlaCurrent = (TextView) mActivity.findViewById(R.id.unit_voice_play_current);
        mTVPlaTotal = (TextView) mActivity.findViewById(R.id.unit_voice_play_total);

        if (mIVRecOperate != null) {
            mIVRecOperate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (mDeviceState == CommonDefine.MEDIA_STATE_RECORD_DOING) {
                        mDeviceState = CommonDefine.MEDIA_STATE_RECORD_PAUSE;
                        stopRecorder(mMediaRecorder, true);
                        mMediaRecorder = null;

                        mTVRecText.setText("已暂停");
                        mIVRecOperate.setImageResource(R.mipmap.voice_pause);
                    } else {
                        sessionRecord(false);
                    }
                }
            });
        }

        if (mVRecFinish != null) {
            mVRecFinish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickRecordFinish();
                }
            });
        }

        if (mIVPlaOperate != null) {
            mIVPlaOperate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (mDeviceState == CommonDefine.MEDIA_STATE_PLAY_DOING) {
                        mDeviceState = CommonDefine.MEDIA_STATE_PLAY_PAUSE;
                        pauseMedia(mMediaPlayer);
                        mIVPlaOperate.setImageResource(R.mipmap.voice_play);

                    } else if (mDeviceState == CommonDefine.MEDIA_STATE_PLAY_PAUSE) {
                        mDeviceState = CommonDefine.MEDIA_STATE_PLAY_DOING;
                        playMedia(mMediaPlayer);

                        mIVPlaOperate.setImageResource(R.mipmap.voice_pause);
                        mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);
                        mHandler.sendEmptyMessage(CommonDefine.MSG_TIME_INTERVAL);

                    } else if (mDeviceState == CommonDefine.MEDIA_STATE_PLAY_STOP) {
                        if (!TextUtils.isEmpty(mPlaFilePath)) {
                            sessionPlay(false, mPlaFilePath);
                        }
                    }
                }
            });
        }

        if (mIVPlaStop != null) {
            mIVPlaStop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);
                        mDeviceState = CommonDefine.MEDIA_STATE_PLAY_STOP;

                        mIVPlaOperate.setImageResource(R.mipmap.voice_play);
                        mTVPlaCurrent.setText("00:00:00");

                        stopMedia(mMediaPlayer, true);
                        mMediaPlayer = null;

                    } catch (Exception e) {

                    } finally {
                        mVPlaRoot.setVisibility(View.GONE);
                    }
                }
            });
        }

        if (mSBPlayProgress != null) {
            mSBPlayProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);
                    mSavedState = mDeviceState;
                    if (mSavedState == CommonDefine.MEDIA_STATE_PLAY_DOING) {
                        pauseMedia(mMediaPlayer);
                    }
                }

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);

                    TimeMethod ts = TimeMethod.timeSpanSecond(progress / 1000);
                    mTVPlaCurrent.setText(String.format("%02d:%02d:%02d",
                            ts.mSpanHour, ts.mSpanMinute, ts.mSpanSecond));
                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    seektoMedia(mMediaPlayer, mSBPlayProgress.getProgress());

                    if (mSavedState == CommonDefine.MEDIA_STATE_PLAY_DOING) {
                        playMedia(mMediaPlayer);
                        mHandler.sendEmptyMessage(CommonDefine.MSG_TIME_INTERVAL);
                    }
                }
            });
        }
    }

    public void sessionRecord(boolean init) {
        if (!FileUtils.isSDCardAvailable()) return;
        if (init) {
            mRecTimeSum = 0;
            cleanFieArrayList(mRecList);
        }

        stopRecorder(mMediaRecorder, true);
        mMediaRecorder = null;

        stopMedia(mMediaPlayer, true);
        mMediaPlayer = null;

        if (mVPlaRoot != null) {
            mVPlaRoot.setVisibility(View.GONE);
        }
        mVRecRoot.setVisibility(View.VISIBLE);
        mIVRecOperate.setImageResource(R.mipmap.voice_record);

        mMediaRecorder = new MediaRecorder();
        File file = prepareRecorder(mMediaRecorder, true);
        if (file != null) {
            mTVRecText.setText("正在录音");
            mDeviceState = CommonDefine.MEDIA_STATE_RECORD_DOING;
            mRecTimePrev = TimeMethod.getTimeStrFromMillis(System.currentTimeMillis());
            mRecList.add(file);

            mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);
            mHandler.sendEmptyMessage(CommonDefine.MSG_TIME_INTERVAL);
        }
    }

    public void sessionRecordEx(boolean init, int resTimeTextId) {
        if (!FileUtils.isSDCardAvailable()) return;
        if (init) {
            mRecTimeSum = 0;
            cleanFieArrayList(mRecList);
        }

        mTVRecTime = (TextView) mActivity.findViewById(resTimeTextId);

        stopRecorder(mMediaRecorder, true);
        mMediaRecorder = null;

        stopMedia(mMediaPlayer, true);
        mMediaPlayer = null;

        if (mVPlaRoot != null) {
            mVPlaRoot.setVisibility(View.GONE);
        }

        mMediaRecorder = new MediaRecorder();
        File file = prepareRecorder(mMediaRecorder, true);
        if (file != null) {
            mDeviceState = CommonDefine.MEDIA_STATE_RECORD_DOING;
            mRecTimePrev = TimeMethod.getTimeStrFromMillis(System.currentTimeMillis());
            mRecList.add(file);

            mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);
            mHandler.sendEmptyMessage(CommonDefine.MSG_TIME_INTERVAL);
        }
    }

    /**
     * 完成录音
     */
    public void clickRecordFinish() {
        try {
            mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);
            mDeviceState = CommonDefine.MEDIA_STATE_RECORD_STOP;
            stopRecorder(mMediaRecorder, true);
            mMediaRecorder = null;

            if (mVRecRoot != null) {
                mVRecRoot.setVisibility(View.GONE);
            }
            if (TimeMethod.timeSpanSecond(mRecTimeSum).mSpanSecond == 0) {
                Toast.makeText(mActivity, "时间过短", Toast.LENGTH_SHORT).show();
            } else {

                File file = getOutputVoiceFile(mRecList);
                if (file != null && file.length() > 0) {
                    cleanFieArrayList(mRecList);
                    //TODO 这里可以返回数据 setResult
                    voiceCallBack.recFinish();
                    voiceCallBack.voicePath(file.getAbsolutePath());
                }
            }

        } catch (Exception e) {
        }
    }

    /**
     * 录音播放
     *
     * @param init
     * @param path
     */
    public void sessionPlay(boolean init, String path) {
        if (TextUtils.isEmpty(path)) return;
        mPlaFilePath = path;

        try {
            mVPlaRoot.setVisibility(View.VISIBLE);
            if (mVRecRoot != null) {
                mVRecRoot.setVisibility(View.GONE);
            }

            mIVPlaOperate.setImageResource(R.mipmap.voice_pause);
            mTVPlaCurrent.setText("00:00:00");

            stopRecorder(mMediaRecorder, true);
            mMediaRecorder = null;

            stopMedia(mMediaPlayer, true);
            mMediaPlayer = null;

            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnCompletionListener(mPlayCompetedListener);

            if (prepareMedia(mMediaPlayer, path)) {
                mDeviceState = CommonDefine.MEDIA_STATE_PLAY_DOING;

                TimeMethod ts = TimeMethod.timeSpanSecond(mMediaPlayer.getDuration() / 1000);
                mTVPlaTotal.setText(String.format("%02d:%02d:%02d",
                        ts.mSpanHour, ts.mSpanMinute, ts.mSpanSecond));

                mSBPlayProgress.setMax(Math.max(1, mMediaPlayer.getDuration()));

                if (init) {
                    mSBPlayProgress.setProgress(0);
                    seektoMedia(mMediaPlayer, 0);
                } else {
                    seektoMedia(mMediaPlayer, mSBPlayProgress.getProgress());
                }

                if (playMedia(mMediaPlayer)) {
                    mHandler.removeMessages(CommonDefine.MSG_TIME_INTERVAL);
                    mHandler.sendEmptyMessage(CommonDefine.MSG_TIME_INTERVAL);
                }
            }
        } catch (Exception e) {
        }
    }

    /**
     * 合并录音
     *
     * @param list
     * @return
     */
    private File getOutputVoiceFile(ArrayList<File> list) {
        String mMinute1 = TimeMethod.getTime();
        String recFilePath = FileUtils.recAudioPath(filePath);
        File recDirFile = FileUtils.recAudioDir(recFilePath);

        // 创建音频文件,合并的文件放这里
        File resFile = new File(recDirFile, mMinute1   ".amr");
        FileOutputStream fileOutputStream = null;

        if (!resFile.exists()) {
            try {
                resFile.createNewFile();
            } catch (IOException e) {
            }
        }
        try {
            fileOutputStream = new FileOutputStream(resFile);
        } catch (IOException e) {
        }
        // list里面为暂停录音 所产生的 几段录音文件的名字,中间几段文件的减去前面的6个字节头文件
        for (int i = 0; i < list.size(); i  ) {
            File file = list.get(i);
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] myByte = new byte[fileInputStream.available()];
                // 文件长度
                int length = myByte.length;
                // 头文件
                if (i == 0) {
                    while (fileInputStream.read(myByte) != -1) {
                        fileOutputStream.write(myByte, 0, length);
                    }
                }
                // 之后的文件,去掉头文件就可以了
                else {
                    while (fileInputStream.read(myByte) != -1) {
                        fileOutputStream.write(myByte, 6, length - 6);
                    }
                }
                fileOutputStream.flush();
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 结束后关闭流
        try {
            fileOutputStream.close();
        } catch (IOException e) {
        }

        return resFile;
    }

    private void cleanFieArrayList(ArrayList<File> list) {
        for (File file : list) {
            file.delete();
        }
        list.clear();
    }

    /**
     * 播放录音准备工作
     *
     * @param mp
     * @param file
     * @return
     */
    private boolean prepareMedia(MediaPlayer mp, String file) {
        boolean result = false;

        try {
            mp.setDataSource(file);
            mp.prepare();
            result = true;
        } catch (Exception e) {
        }

        return result;
    }

    /**
     * 播放录音开始
     *
     * @param mp
     * @return
     */
    private boolean playMedia(MediaPlayer mp) {
        boolean result = false;

        try {
            if (mp != null) {
                mp.start();
                result = true;
            }
        } catch (Exception e) {
        }

        return result;
    }

    /**
     * 拖动播放进度条
     *
     * @param mp
     * @param pos
     * @return
     */
    private boolean seektoMedia(MediaPlayer mp, int pos) {
        boolean result = false;

        try {
            if (mp != null && pos >= 0) {
                mp.seekTo(pos);
                result = true;
            }
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * 停止播放
     *
     * @param mp
     * @param release
     * @return
     */
    private boolean stopMedia(MediaPlayer mp, boolean release) {
        boolean result = false;

        try {
            if (mp != null) {
                mp.stop();

                if (release) {
                    mp.release();
                }
                result = true;
            }
        } catch (Exception e) {
        }

        return result;
    }

    /**
     * 暂停播放
     *
     * @param mp
     * @return
     */
    private boolean pauseMedia(MediaPlayer mp) {
        boolean result = false;

        try {
            if (mp != null) {
                mp.pause();
                result = true;
            }
        } catch (Exception e) {
        }

        return result;
    }

    /**
     * 停止录音
     *
     * @param mr
     * @param release
     * @return
     */
    private boolean stopRecorder(MediaRecorder mr, boolean release) {
        boolean result = false;

        try {
            if (mr != null) {
                mr.stop();
                if (release) {
                    mr.release();
                }
                result = true;
            }
        } catch (Exception e) {
        }

        return result;
    }

    /**
     * 录音准备工作 ,开始录音
     *
     * @param mr
     * @param start
     * @return
     */
    @SuppressWarnings("deprecation")
    private File prepareRecorder(MediaRecorder mr, boolean start) {
        File recFile = null;
        if (mr == null) return null;

        try {
            String path = FileUtils.recAudioPath(filePath);
            recFile = new File(path, TimeMethod.getTime()   ".amr");

            mr.setAudioSource(MediaRecorder.AudioSource.MIC);
            mr.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
            mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mr.setOutputFile(recFile.getAbsolutePath());
            mr.prepare();

            if (start) {
                mr.start();
            }
        } catch (Exception e) {
        }
        return recFile;
    }
}

标签: 录音与播放

实例下载地址

android 录音、播放 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警