Android学习笔记-录音

android使用MediaRecorder录音大概步骤:

  1. 创建 android.media.MediaRecorder实例
  2. 设置声音来源(MediaRecorder.setAudioSource()),一般使用MediaRecorder.AudioSource.MIC
  3. 设置输出格式(MediaRecorder.setOutputFormat())
  4. 设置输出文件(MediaRecorder.setOutputFile())
  5. 设置声音编码(MediaRecorder.setAudioEncoder())
  6. 设置完成调用 MediaRecorder.prepare()
  7. 开始录音MediaRecorder.start()
  8. 停止录音MediaRecorder.stop()
  9. 录音完成后调用MediaRecorder.release()释放资源
public class AudioRecord extends AppCompatActivity {
    Button record;
    Button play;
    private MediaRecorder mRecorder = null;
    private MediaPlayer player=null;
    private String fileName;
    private boolean isPlay=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_record);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        iniView();
        iniListener();

    }
     private void iniView(){
         record= (Button) findViewById(R.id.record);
         play=(Button)findViewById(R.id.play);
     }

    @Override
    protected void onPause() {
        super.onPause();
        stopPlay();
    }

    private void iniListener(){
        record.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        beginRecord();
                        break;
                    case MotionEvent.ACTION_UP:
                        stopRecord();
                        break;
                }
                return true;
            }
        });

    }
    //开始录音
    private void beginRecord(){
        mRecorder=new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        fileName=getFileName(".3gp");
        mRecorder.setOutputFile(fileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        System.out.println("start record");
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mRecorder.start();
    }
    //停止录音
    private void stopRecord(){
        System.out.println("stop record");
        if(mRecorder!=null) {
            mRecorder.stop();
            mRecorder.reset();
            mRecorder.release();
            mRecorder=null;
        }
    }
    //开始播放
    private void startPlay(String fileName){
        player=new MediaPlayer();
        isPlay=true;
        try {
            player.setDataSource(fileName);
            player.prepare();
            player.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //停止播放
    private void stopPlay(){
        if(player!=null){
            player.stop();
            player.release();
        }
        isPlay=false;
    }

    public void play(View v){
        System.out.println("clicked");
        if(!isPlay)
            startPlay(fileName);
        else stopPlay();
    }

    private String getFileName(String format){
        String fileName;
        fileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        fileName += "/"+System.currentTimeMillis()+format;
        return fileName;
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值