• Android 使用MediaRecorder录音


    package com.example.HyyRecord;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.media.MediaRecorder;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    
    import java.io.File;
    import java.io.IOException;
    
    public class MyActivity extends Activity {
        private MediaRecorder mediaRecorder;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //首先要保证这个文件夹存在
            File file = new File("/sdcard/0hyyRecord");
            if (!file.exists()) {
                file.mkdir();
            }
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.setOutputFile("/sdcard/0hyyRecord/" + System.currentTimeMillis() + ".3pg");
    
        }
    
        public void startRecord(View view) {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                try {
                    mediaRecorder.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaRecorder.start();
            } else {
                System.out.println("sd卡不可用");
            }
        }
    
        public void stopRecord(View view) {
            mediaRecorder.stop();
            mediaRecorder.reset();
        }
    
        @Override
        protected void onDestroy() {
            try {
                mediaRecorder.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
            super.onDestroy();
        }
    
       
    }

     界面文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <Button android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="开始录音"
                android:onClick="startRecord"
                />
    
        <Button android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="停止录音"
                android:onClick="stopRecord"
                />
    
      
    </LinearLayout>
  • 相关阅读:
    LG P4449 & JZOJ 于神之怒
    [国家集训队]Crash的数字表格
    LG P3768 简单的数学题
    NOI2018 屠龙勇士
    为什么从后台获取的id到前端后却变了?Long类型转json时前端js丢失精度解决方案-----@JsonSerialize和@JsonDeserialize
    vue的filters过滤器优化
    根据key查询redis中是否存在key对应的value,根据key获取值
    PowerDesigner逆向工程将MYSQL数据库转成pdm
    解决图片验证码不显示的问题
    报错:Unknown column 'province' in 'field list'
  • 原文地址:https://www.cnblogs.com/wuyou/p/3733417.html
Copyright © 2020-2023  润新知