zoukankan      html  css  js  c++  java
  • Android之录音工具类

    /**
     * 录音工具类
     * 
     * @author rendongwei
     * 
     */
    public class RecordUtil {
        private static final int SAMPLE_RATE_IN_HZ = 8000;
        private MediaRecorder recorder = new MediaRecorder();
        // 录音的路径
        private String mPath;
    
        public RecordUtil(String path) {
            mPath = path;
        }
    
        /**
         * 开始录音
         * 
         * @throws IOException
         */
        public void start() throws IOException {
            String state = android.os.Environment.getExternalStorageState();
            if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
                throw new IOException("SD Card is not mounted,It is  " + state
                        + ".");
            }
            File directory = new File(mPath).getParentFile();
            if (!directory.exists() && !directory.mkdirs()) {
                throw new IOException("Path to file could not be created");
            }
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setAudioSamplingRate(SAMPLE_RATE_IN_HZ);
            recorder.setOutputFile(mPath);
            recorder.prepare();
            recorder.start();
        }
    
        /**
         * 结束录音
         * 
         * @throws IOException
         */
        public void stop() throws IOException {
            recorder.stop();
            recorder.release();
        }
    
        /**
         * 获取录音时间
         * 
         * @return
         */
        public double getAmplitude() {
            if (recorder != null) {
                return (recorder.getMaxAmplitude());
            }
            return 0;
        }
    }
  • 相关阅读:
    Django 常见问题
    post和get的区别
    Django 基础学习笔记二
    Django 中的分页器
    Python 微服务框架 Nameko 微服务通信(RabbitMQ)
    《大数据白皮书 2020.12》解读
    练习Div+Css
    利用JAVAScript调用WebService
    统计在线人数和历史访问人数
    自己写的一个DBHelper
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3461399.html
Copyright © 2011-2022 走看看