Android 按住录音 松手停止,根据声音来更换图片 播放短声

还是项目。。。项目中需要。。。这个主要是根据分贝的不同来改变图片。直接上代码。资源等我去公司了 再上传。。

1,给控件设置触摸事件。
把录制的文件存在本地指定位置
//触摸事件
①
mRecordVoiceIV.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == ACTION_DOWN) {
            //文件名
            //  String strDate = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss")
            //        .format(new Date());
            //这是文件夹路径
            String path = FTConfig.FILE_PATH + "/" + mydev.getDevId();
            //新建文件流
            File dirFirstFolder = new File(path);
            //判断是否有文件夹
            if (!dirFirstFolder.exists()) {
                FileUtil.createIfNotExist(path);
            }
            final String lastPath = path + "/" + "output" + ".mp3";
            File file = new File(lastPath);
            // 创建录音对象
            mr = new MediaRecorder();
            // 从麦克风源进行录音
            mr.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 设置输出格式
            mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            // 设置编码格式
            mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            // 设置输出文件
            mr.setOutputFile(file.getAbsolutePath());
            try {
                // 创建文件
                file.createNewFile();
                // 准备录制
                mr.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 开始录制
            task = new TimerTask() {
                public void run() {
                    dHandler.sendEmptyMessage(66);
                }
            };
            timer.schedule(task, 1000, 1000);
            mr.start();
            mVoiceStateTV.setVisibility(View.VISIBLE);
            mVoiceAnimIV.setVisibility(View.VISIBLE);
            updateMicStatus();
            mVoiceStateTV.setText(R.string.you_can_also_be_recorded_string_3clock_string);
            prompt_tv.setText(R.string.hint_loose_hand_string);
        }
        //松开手之后。
        if (event.getAction() == ACTION_UP) {
            if (mr != null) {
                try {
                    //下面三个参数必须加,不加的话会奔溃,在mediarecorder.stop();
                    //报错为:RuntimeException:stop failed
                    mr.setOnErrorListener(null);
                    mr.setOnInfoListener(null);
                    mr.setPreviewDisplay(null);
                    mr.stop();
                    task.cancel();
                } catch (Exception e){
                    // TODO: handle exception
                    Log.i("Exception", Log.getStackTraceString(e));
                }
                //added by ouyang end

                mr.release();
                mr = null;
                //影藏
                mVoiceStateTV.setVisibility(View.INVISIBLE);
                mVoiceAnimIV.setVisibility(View.INVISIBLE);
                mStartVoice.setVisibility(View.VISIBLE);
                mVoiceAnimIV.setImageDrawable(imgs[0]);
                prompt_tv.setText("提示:按住后开始录音");
            }


        }
        return true;
    }
});

②录制进行中 做出该有的判断。
 private Handler dHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 66) {
                its--;
                if (its == 0) {
                    its = 3;
                    task.cancel();
                    if (mr != null) {
                        try {
                            //下面三个参数必须加,不加的话会奔溃,在mediarecorder.stop();
                            //报错为:RuntimeException:stop failed
                            mr.setOnErrorListener(null);
                            mr.setOnInfoListener(null);
                            mr.setPreviewDisplay(null);
                            mr.stop();
                        } catch (Exception e) {
                            // TODO: handle exception
                            Log.i("Exception", Log.getStackTraceString(e));
                        }
                        //added by ouyang end  timer.schedule(task, 5000);  两个参数的schedule 就只是执行一次吧。
//                        要用三个参数的schedule。

                        mr.release();
                        mr = null;
                        //影藏
                        mVoiceStateTV.setVisibility(View.INVISIBLE);
                        mVoiceAnimIV.setVisibility(View.INVISIBLE);
                        mStartVoice.setVisibility(View.VISIBLE);
                        mVoiceAnimIV.setImageDrawable(imgs[0]);
                    }

                } else {
                    mVoiceStateTV.setText(R.string.you_can_also_be_recorded_string + its + R.string.clock_string);
                }

            }

        }

    };



2,获取分贝大小,改变图片 
 ①,放一个图片数组
 imgs = new Drawable[]{
            getResources().getDrawable(R.drawable.an_1),
            getResources().getDrawable(R.drawable.an_2),
            getResources().getDrawable(R.drawable.an_3),
            getResources().getDrawable(R.drawable.an_4),
            getResources().getDrawable(R.drawable.an_5),};
}
通知mHandler 改变图片
private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int what = msg.what;
        //根据mHandler发送what的大小决定话筒的图片是哪一张
        //说话声音越大,发送过来what值越大
        if (what > 4) {
            what = 4;
        }
        mVoiceAnimIV.setImageDrawable(imgs[what]);
    }
};

private void updateMicStatus() {
    if (mr != null) {
        int ratio = mr.getMaxAmplitude() / BASE;
        int db = 0;// 分贝
        if (ratio > 1)
            db = (int) (20 * Math.log10(ratio));
        //我对着手机说话声音最大的时候,db达到了35左右,
        mHandler.postDelayed(mThread, 300);
        mHandler.sendEmptyMessage(db / 18);
    }
}

3,播放
mStartVoice.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //读取本地播放的 mp3
        String path = FTConfig.FILE_PATH + "/" + mydev.getDevId();
        //新建文件流
        File dirFirstFolder = new File(path);
        //判断是否有文件夹
        if (!dirFirstFolder.exists()) {
            FileUtil.createIfNotExist(path);
        }
        final String lastPath = path + "/" + "output" + ".mp3";
        File mp3File = new File(lastPath);
        if (mp3File.exists()) {
            mPlayer.stop();
            mPlayer = null;
            mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(lastPath);
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
        }
    }
});
播放
//猫
mVoiceCatSw.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (lightOnCat==false) {
            mVoiceLongtimeSw.setChecked(false);
            mVoiceDogSw.setChecked(false);
            mPlayer.stop();//多个铃声的话,先把其他的关闭
            mPlayer = null;
            mPlayer = MediaPlayer.create(SetVoiceActivity.this, R.raw.cat);
            mPlayer.start();//在开启
            lightOnCat = true;
        } else {
            mVoiceLongtimeSw.setChecked(false);
            mVoiceDogSw.setChecked(false);
            lightOnCat = false;
        }
    }
});

这里面需要我提供的 东西尽管说。有啥bug 给我骂出来。不要光骂,不指出。。。这样太不厚道啦

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值