• Android MediaRecorder录制播放音频


    1、请求录制音频权限

    <user-permission android:name="android.permission.RECORD_AUDIO"/>

    RECORD_AUDIO 权限从6.0后需要进行用户授权,通过ActivityCompat.requestPermissions();

    2、创建运行MediaRecorder

    2.1 设置音频源setAudioSource(int audio_source) ,多少情况使用MediaRecorder.AudioSource.MIC

    2.2 设置输出音频文件格式 setOutputFormat(int output_format)

    *(Android 8.0 开始支持MPEG2_TS 格式)

    2.3  设置输出文件名 setOutputFile(String filepath) 

    2.4 设置音频编码器 setAudioEncoder(int audio_encoder)

    2.5 通过完成初始化 prepare()

    2.6  start()

    3 停止释放资源

    stop()  release()

    4、创建MediaPlayer 

    MediaPlayer mediaPlayer=new MediaPlayer();
    mediaPlayer.setDataSource(recordFile.getPath());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                mp.reset();
                            }
                        });

    package com.rexkell.mediaapplication;
    
    import android.media.MediaPlayer;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    
    import java.io.File;
    import java.io.IOException;
    
    /**
     * author: rexkell
     * explain:MediaRecord 录制音频
     */
    public class MediaRecordActivity extends AppCompatActivity {
        private boolean isRecording=false;
        MediaRecorder mediaRecorder;
        MediaPlayer mediaPlayer;
        File  recordFile;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ac_mediarecord);
            init();
        }
        public void init(){
            mediaRecorder=new MediaRecorder();
            //设置音频的来源
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            //设置音频的输出格式
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);
            //设置音频文件的编码
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
            mediaPlayer=new MediaPlayer();
            recordFile=new File(getExternalFilesDir(null).getPath()+"/test.amr");
    
    
        }
        public void record(View v){
            isRecording=!isRecording;
            String text=isRecording?"暂停":"录制";
            ((Button)v).setText(text);
            if (isRecording){
                startRecord();
            }else {
                stopRecord();
            }
        }
        public void play(View v){
    
                    if (mediaPlayer.isPlaying()){
                        return;
                    }
                    //设置数据源
                    try {
                        mediaPlayer.setDataSource(recordFile.getPath());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                mp.reset();
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
    
        }
        public void startRecord(){
            if (recordFile.exists()){
                recordFile.delete();
            }
                try {
                    recordFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            mediaRecorder.setOutputFile(recordFile.getPath());
            try {
                mediaRecorder.prepare();
                mediaRecorder.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void stopRecord(){
            if (mediaRecorder!=null&&isRecording){
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder=null;
            }
        }
        public void stopPlay(){
             if(mediaPlayer!=null){
                 mediaPlayer.stop();
                 mediaPlayer.release();
                 mediaPlayer=null;
             }
        }
    }
                

      

  • 相关阅读:
    密码加盐
    怎么查看别人网站信息
    Tomcat虚拟路径访问本地图片失败的问题
    SSM整合Swagger
    Can't read swagger JSON from http://localhost:8080/Test/api-docs
    swagger使用一新手篇
    @JsonFormat的导包问题
    Could not find acceptable representation
    PostMan打不开怎么解决
    Project facet Java version 1.8 not supported
  • 原文地址:https://www.cnblogs.com/changeMsBlog/p/11221028.html
Copyright © 2020-2023  润新知