现在的位置: 首页 > 综合 > 正文

录音时获取音量值

2017年12月06日 ⁄ 综合 ⁄ 共 1651字 ⁄ 字号 评论关闭

    private void startRecord() {
        try {
            long dateTaken = System.currentTimeMillis();
            mCurrentDate = DateFormat.millisecondFormat(dateTaken, "yyyy-MM-dd");
            mCurrentTime = DateFormat.currentTime();
            String strTempFile = Long.toString(dateTaken);
            mRecordAudioFile = new File(mPath + "/" + strTempFile + ".amr");
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setOnErrorListener(recordErr);
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mMediaRecorder.setOutputFile(mRecordAudioFile.getAbsolutePath());
            mMediaRecorder.getMaxAmplitude();//这个一定要有
            mMediaRecorder.prepare();

            mMediaRecorder.start();

            startThread();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

private void startThread() {

        mThread = new Thread(new Runnable() {

            @Override
            public void run() {
                        Message msg = mHandler.obtainMessage();
                        msg.what = MSG_WHAT_PARAMS;
                        Bundle b = new Bundle();

                        try {
                            mThread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        if (mMediaRecorder != null) {

                            try {
                                int amplitude = mMediaRecorder.getMaxAmplitude();
                                b.putLong("currentValue", amplitude);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            b.putLong("currentValue", 0);
                        }
                        msg.setData(b);
                        mHandler.sendMessage(msg);
                    }
        });
        mThread.start();
    }

抱歉!评论已关闭.