• 使用MediaRecorder录音


    Android提供了两个API用于录音的实现:MediaRecorder 和AudioRecord。

    1. MediaRecorder:录制的音频文件是经过压缩后的,需要设置编码器。并且录制的音频文件可以用系统自带的Music播放器播放。MediaRecorder已经集成了录音、编码、压缩等,并支持少量的录音音频格式,但是这也是他的缺点,支持的格式过少并且无法实时处理音频数据。

    2. AudioRecord:主要实现对音频实时处理以及边录边播功能,相对MediaRecorder比较专业,输出是PCM语音数据,如果保存成音频文件,是不能够被播放器播放的,所以必须先写代码实现数据编码以及压缩。

    首先贴出mediaRecorder的使用

    public class MainActivity extends AppCompatActivity  {
        MediaRecorder mMediaRecorder;
        String filePath;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startRecord();
                }
            });
            findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopRecord();
                }
            });
    
        }
            public void startRecord() {
                // 开始录音
                /* ①Initial:实例化MediaRecorder对象 */
                if (mMediaRecorder == null)
                    mMediaRecorder = new MediaRecorder();
                try {
                    /* ②setAudioSource/setVedioSource */
                    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
                    /*
                     * ②设置输出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default THREE_GPP(3gp格式
                     * ,H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
                     */
                    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                    /* ②设置音频文件的编码:AAC/AMR_NB/AMR_MB/Default 声音的(波形)的采样 */
                    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                    String fileName = DateFormat.format("yyyyMMdd_HHmmss", Calendar.getInstance(Locale.CHINA)) + ".m4a";
                    File file = new File("/sdcard/AAAData");
                    if (!file.exists()) {
                        file.mkdir();
                    }
                 filePath = file.getAbsolutePath() + "/"+fileName;
                    /* ③准备 */
                    mMediaRecorder.setOutputFile(filePath);
                    mMediaRecorder.prepare();
                    /* ④开始 */
                    mMediaRecorder.start();
                } catch (IllegalStateException e) {
                    System.out.println(e.getMessage());
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        public void stopRecord() {
            try {
                mMediaRecorder.stop();
                mMediaRecorder.release();
                mMediaRecorder = null;
                filePath = "";
            } catch (RuntimeException e) {
                System.out.println(e.toString());
                mMediaRecorder.reset();
                mMediaRecorder.release();
                mMediaRecorder = null;
    
                File file = new File(filePath);
                if (file.exists())
                    file.delete();
    
                filePath = "";
            }
        }
    }
      xmlns:tools="http://schemas.android.com/tools"
        package="com.january.spring">
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    PHP7.27: connect mysql 5.7 using new mysqli
    PHP: Browser, Operating System (OS), Device, and Language Detect
    PHP 在WIN10 下配置
    MySQL chartset
    學習Echart 2.2.7
    The open source JavaScript graphing library that powers Plotly
    D3.js 制作中国地图
    FastReport.Net
    CSS 3D transforms
    SparkCore的调优之开发调优
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/10977991.html
Copyright © 2020-2023  润新知