android 调用系统自带录音实现,语音录制与播放

时间:2025-03-08 22:41:31

相关权限:

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

添加布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="/apk/res/android"
    xmlns:tools="/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    tools:context="">
    <Button
        android:text="播放录音"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/btn_three_play" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_three_recorder"
        android:textSize="20dp"
        android:text="开始录音并保存" />
    <TextView
        android:id="@+id/tv_progress3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="40sp"
        android:textStyle="bold"
        android:text="0"/>
</LinearLayout>
 代码如下
public class MainActivity extends Activity implements ,  {
    private MediaPlayer mediaPlayer;
    private boolean isRecording = false;
    private TextView mTv_progress3;
    Button btn_three_play, btn_three_recorder;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(.activity_main);
        btn_three_play = (Button) findViewById(.btn_three_play);
        btn_three_recorder = (Button) findViewById(.btn_three_recorder);
        mTv_progress3 = (TextView) findViewById(.tv_progress3);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnPreparedListener(this);
        btn_three_play.setEnabled(false);
        btn_three_play.setOnClickListener(new () {
            @Override
            public void onClick(View v) {
                playRecorder();
            }
        });

        btn_three_recorder.setOnClickListener(new () {
            @Override
            public void onClick(View v) {
                try {
                    recorder_Audio();
                    btn_three_play.setEnabled(true);
                } catch (IOException e) {
                    ();
                }
            }
        });
    }

    private boolean isPlaying = false;
    private int frequency = 11025;
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    int audiosource = .MIC;
    int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    File recordingFile = null;
     recordAudio = null;
     playAudio = null;

    public void recorder_Audio() throws IOException {
        //AudioRecord不会直接保存音频,需要自己保存
        File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        ();

        try {
            recordingFile = File.createTempFile("recording", ".pcm", path);
        } catch (IOException e) {
            ();
        }
        recordAudio = new ();
        recordAudio.execute();
    }

    public void playRecorder() {
        isRecording = false;
        playAudio = new ();
        playAudio.execute();
    }


    public void onCompletion(MediaPlayer mp) {

    }


    public void onPrepared(MediaPlayer mp) {
        mediaPlayer.start();
    }

    private class PlayAudio extends AsyncTask<Void, Integer, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            isPlaying = true;
            int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfig, audioFormat);
            short[] buffer = new short[bufferSize / 4];
            DataInputStream dis = null;
            try {
                dis = new DataInputStream(new BufferedInputStream(new FileInputStream(recordingFile)));
            } catch (FileNotFoundException e) {
                ();
            }
            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, channelConfig, audioFormat, bufferSize, AudioTrack.MODE_STREAM);
            ();
            try {
                while (isPlaying && () > 0) {
                    int i = 0;
                    while (() > 0 && i < buffer.length) {
                        buffer[i] = ();
                        i++;
                    }
                    (buffer, 0, buffer.length);
                }
                ();

            } catch (IOException e) {
                ();
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            Toast.makeText(MainActivity.this, "语音开始播放", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Toast.makeText(MainActivity.this, "语音播放完了", Toast.LENGTH_SHORT).show();

        }
    }

    private class RecordAudio extends AsyncTask<Void, Integer, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            isRecording = true;
            DataOutputStream dos = null;
            try {
                dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(recordingFile)));
                int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfig, audioFormat);
                AudioRecord audioRecord = new AudioRecord(audiosource, frequency, channelConfig, audioFormat, bufferSize);
                short[] buffer = new short[bufferSize];
                ();
                int r = 0;
                while (isRecording) {
                    int bufferReadResult = (buffer, 0, bufferSize);
                    for (int i = 0; i < bufferReadResult; i++) {
                        try {
                            (buffer[i]);

                        } catch (IOException e) {
                            ();
                        }
                    }
                    publishProgress(new Integer(r));
                    r++;
                }
                ();
                ();
            } catch (Exception e) {
                ();
            }

            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(MainActivity.this, "正在录音", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            mTv_progress3.setText(values[0] + "");
        }
    }
}

其中先设置播放按钮为不可点击,录音后才可点击,不然会报空指针异常  

或者可以用按住录音 松开播放来实现 代码如下

btn_three_recorder .setOnTouchListener(new () {

@Override   public boolean  onTouch(View view, MotionEvent motionEvent) { 
       switch (()) {         
   case MotionEvent.ACTION_DOWN: //按下             
   try {                 
   recorder_Audio();                          
        } catch (IOException e) {   
                 ();               
 }            
    break;          
  case MotionEvent.ACTION_MOVE: //移动         
       break;          
  case MotionEvent.ACTION_UP: //抬起          
      playRecorder();              
  break;       
 }        
return true;   
 }});