zoukankan      html  css  js  c++  java
  • 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;
             }
        }
    }
                

      

  • 相关阅读:
    Java Object类 和 String类 常见问答 6k字+总结
    又长又细,万字长文带你解读Redisson分布式锁的源码
    第三方API对接如何设计接口认证?
    免费正版 IntelliJ IDEA license 详细指南
    ClickHouse性能优化?试试物化视图
    全量同步Elasticsearch方案之Canal
    Canal高可用架构部署
    Spring官方发布新成员:Spring GraphQL
    为什么catch了异常,但事务还是回滚了?
    这几个事务案例会回滚吗?最后一个90%的人判断错了...
  • 原文地址:https://www.cnblogs.com/changeMsBlog/p/11221028.html
Copyright © 2011-2022 走看看