【Android 多媒体应用】使用MediaRecoder录制,MediaPlayer播放音频数据

1.MainActivity.java

import android.annotation.TargetApi;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends Activity implements View.OnClickListener {
    private final static String TAG = "debug--";
    private Button btnRecord,btnStop,btnPlay;
    private File soundFile;
    private MediaRecorder mRecoder;
    private MediaPlayer mPlayer;
    private TextView mTxt ;
    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            Log.d(TAG, Integer.toString(msg.what));
            mTxt.setText(msg.what/60+":"+msg.what%60);
            super.handleMessage(msg);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnRecord = (Button)findViewById(R.id.start);
        btnStop = (Button)findViewById(R.id.stop);
        btnPlay = (Button)findViewById(R.id.play);
        mTxt = (TextView)findViewById(R.id.txtTime);
        btnRecord.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        btnPlay.setOnClickListener(this);
    }

    public void onDestroy() {

        super.onDestroy();
        if((mRecoder != null) && (soundFile !=null) && (soundFile.exists())) {
            mRecoder.stop();
            mRecoder.release();
            mRecoder = null;
        }

    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.start:
                if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                {
                    Toast.makeText(MainActivity.this,"check SD Card Faild!",Toast.LENGTH_SHORT).show();
                    return;
                }

                try {
                    soundFile = new File("sdcard/sound.amr");
                    mRecoder = new MediaRecorder();
                    mRecoder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mRecoder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                    mRecoder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    mRecoder.setOutputFile(soundFile.getAbsolutePath());
                    mRecoder.prepare();
                    mRecoder.start();
                    showRecordTime();
                    Log.d(TAG, "start");
                    Toast.makeText(MainActivity.this,"start Recording!",Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.stop:
                if(soundFile != null && soundFile.exists())
                {
                    mRecoder.stop();
                    mRecoder.release();
                    mRecoder = null;
                    Log.d(TAG, "stop");
                    Toast.makeText(MainActivity.this,"Stop Recording!",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.play:
            {
                Log.d(TAG,btnPlay.getText().toString());
                if (btnPlay.getText().toString().equalsIgnoreCase("Play") ) {
                    Log.d(TAG,"play@@@@@@@");
                    mPlayer = new MediaPlayer();
                    try {
                        mPlayer.setDataSource("sdcard/sound.amr");
                        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mPlayer.prepare();
                        mPlayer.start();
                        btnPlay.setText("Stop");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else if (btnPlay.getText().toString().equalsIgnoreCase("Stop") )
                {
                    if(mPlayer != null)
                    {
                        mPlayer.stop();
                        mPlayer.release();
                        btnPlay.setText("Play");
                    }
                }
            }
            break;
        }

    }

    private void showRecordTime() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                int time_sec = 0;
                while(mRecoder != null)
                {
                    try {
                        Thread.sleep(1000);
                        Message message = new Message();
                        time_sec +=1;
                        message.what = time_sec ;
                        handler.sendMessage(message);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();

    }
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:textSize="40dip"
        android:textColor="#000000"
        android:id="@+id/txtTime"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="1dp">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/start"
            android:text="start"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/stop"
            android:text="stop"/>
    </LinearLayout>

    <Button
        android:text="play"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/play" />


</LinearLayout>

3.AndroidMainfest.xml添加权限

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
原文地址:https://www.cnblogs.com/CoderTian/p/6198570.html