Android 录音和播放录音

清单文件中的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>//SD卡存储的权限
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>//SD卡中删除创建文件的权限
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>//调用系统录音的权限

MainActivity代码:

public class MainActivity extends ActionBarActivity implements OnClickListener {
    private static final String LOG_TAG = "AudioRecordTest";//Log Key值
    private String FileName = null;//录音存放地址
    private MediaPlayer mPlayer = null;//媒体播放器
    private MediaRecorder mRecorder = null;//媒体录音

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button luyin = (Button) findViewById(R.id.luyin);
        Button jieshu = (Button) findViewById(R.id.jieshu);
        Button bofang = (Button) findViewById(R.id.bofang);
        Button tz = (Button) findViewById(R.id.tz);
        luyin.setOnClickListener(this);
        jieshu.setOnClickListener(this);
        bofang.setOnClickListener(this);
        tz.setOnClickListener(this);
        //录音存放地址
        FileName=Environment.getExternalStorageDirectory().getAbsolutePath();
        FileName += "/audiorecordtest.3gp";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.luyin:
            mRecorder=new MediaRecorder();//媒体录音
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音源
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//录入的格式
            mRecorder.setOutputFile(FileName);//存入的地址
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//声音编辑器
            try {
                mRecorder.prepare();//准备录音
            }catch (IOException e) {
                Log.e(LOG_TAG,"prepare() failed");
            }
            mRecorder.start();//开启录音
            break;
        case R.id.jieshu:
            if (mRecorder!=null) {
                mRecorder.stop();//停止录音
                mRecorder.release();//释放录音
                mRecorder=null;
            }       break;
        case R.id.bofang:
            mPlayer=new MediaPlayer();//媒体播放器
            try {
                mPlayer.setDataSource(FileName);//播放文件的路径
                mPlayer.prepare();//准备
                mPlayer.start();//开始播放
            }catch (IOException e) {
                Log.e(LOG_TAG,"播放失败");
            }
            break;
        case R.id.tz:
            if (mPlayer!=null) {
                mPlayer.release();//释放
                mPlayer=null;
            }           
            break;

        default:
            break;
        }

    }
}

XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <Button 
            android:id="@+id/luyin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始录音"
            />
        <Button 
            android:layout_marginTop="10dp"
            android:id="@+id/jieshu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="结束录音"
            />
        <Button 
            android:layout_marginTop="10dp"
            android:id="@+id/bofang"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="播放录音"
            />
        <Button 
            android:layout_marginTop="10dp"
            android:id="@+id/tz"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="结束录音"
            />
    </LinearLayout>

</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/a2978157/article/details/73108687
今日推荐