安卓使用MediaRecorder录制音频(声音)


效果

安卓使用MediaRecorder录制音频(声音)_第1张图片

代码

package com.javen.devicemange.CrazyOne.media;

import android.Manifest;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.javen.devicemange.R;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/2/27 0027.
 * 使用MediaRecorder录制音频(声音)
 */

public class RecordSound extends AppCompatActivity implements View.OnClickListener {
    private Button record;
    private Button stop;
    private File file;
    private MediaRecorder mediaRecorder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recordsound);
        initView();
        requestPermission();

    }

    /**
     * 申请权限
     */
    private void requestPermission() {
        String[] needPermission = {
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        };
        ActivityCompat.requestPermissions(this, needPermission, 1);
    }

    private void initView() {
        record = (Button) findViewById(R.id.record);
        stop = (Button) findViewById(R.id.stop);

        record.setOnClickListener(this);
        stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.record:
                if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    Toast.makeText(this, "ad卡不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                //创建文件
                file = new File(Environment.getExternalStorageDirectory().toString() + "/sound.amr");

                mediaRecorder = new MediaRecorder();
                //设置录音的声音来源,MIC:麦克风的音频源
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                //设置录音的声音的格式,THREE_GPP:推荐录音的时候用3GPP(必须在设置声音编码格式之前设置)
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                //设置录音的编码格式
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                //设置录制的音频文件的保存位置
                mediaRecorder.setOutputFile(file.getAbsolutePath());
                try {
                    mediaRecorder.prepare();
                    //开始录音
                    mediaRecorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.stop:
                if (null != file && file.exists()) {
                    //停止录音
                    mediaRecorder.stop();
                    mediaRecorder.release();
                    mediaRecorder = null;
                }
                break;
        }
    }
}

布局xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    

AndroidManifest.xml


android:name="android.permission.RECORD_AUDIO"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.READ_EXTERNAL_STORAGE"/>

。。。

你可能感兴趣的:(安卓Media)