录音(MediaRecorder)与播放

效果:

录音(MediaRecorder)与播放_第1张图片


MainActivity

package com.example.day0316;

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

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private Button mBtStart;
    private Button mBtEnd;
    private Button mBtPlay;
    private MediaRecorder mRecorder;
    private MediaPlayer mPlayer;
    private static String PATH_NAME = Environment.getExternalStorageDirectory().getAbsolutePath()+"/MyRecording/"; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
    }
    private void initViews() {
        mBtStart = (Button) findViewById(R.id.bt_start);
        mBtStart.setOnClickListener(this);
        mBtEnd = (Button) findViewById(R.id.bt_end);
        mBtEnd.setOnClickListener(this);
        mBtPlay = (Button) findViewById(R.id.bt_play);
        mBtPlay.setOnClickListener(this);

        File file = new File(PATH_NAME);
        if (!file.exists()) {
            file.mkdir();
        }
        PATH_NAME +="/audiorecordtest.3gp";


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_start:
            mRecorder = new MediaRecorder();  
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
            mRecorder.setOutputFile(PATH_NAME);  
            try {
                mRecorder.prepare();
                mRecorder.start();   
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case R.id.bt_end:
            if (mRecorder == null) 
            { 
                return ;
            }
            mRecorder.stop();  
            mRecorder.reset();   
            mRecorder.release();
            mRecorder = null;  
            Toast.makeText(this, "录音结束", Toast.LENGTH_SHORT).show();
            break;
        case R.id.bt_play:
            try {
                mPlayer = new MediaPlayer();
                mPlayer.setDataSource(PATH_NAME);
                mPlayer.prepare();
                mPlayer.start();

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }

    }

}

权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
    <uses-permission android:name="android.permission.RECORD_AUDIO" />  

转载于:http://blog.csdn.net/cxf7394373/article/details/8313980

你可能感兴趣的:(android学习路)