创新实训——第五周1

        这几天主要在实现录音功能。

        首先自定义了一个音频记录管理器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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值