安卓麦克风录音及播放简单实现

2019-06-24  本文已影响0人  liuyx

简单笔记,仅供参考

添加权限

    <!-- 打开MICROPHONE -->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <!-- 往sdcard中写入数据 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

麦克风

import android.media.MediaRecorder;
import android.util.Log;

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

public class SoundRecorder {
    private static final String LOG_TAG = "SoundRecorder";
    private MediaRecorder mRecorder;
    private File audioFile;

    public void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        try {
            audioFile = File.createTempFile("record_", ".amr");
            mRecorder.setOutputFile(audioFile.getAbsolutePath());
            mRecorder.prepare();

        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed" + e.getMessage());
        }
        mRecorder.start();

    }

    public String getAudioFile() {
        return this.audioFile.getAbsolutePath();
    }

    public void stopRecording() {
        if (mRecorder != null) {
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
    }

}

播放器

import android.media.MediaPlayer;
import android.util.Log;

import java.io.IOException;

public class SoundPlayer {

    private static final String LOG_TAG = "SoundPlayer";
    private MediaPlayer mPlayer;


    public void startPlaying(String fileName) {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(fileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed" + e.getMessage());
        }
    }

    public void stopPlaying() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

主活动

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.yxliu.android.lifecycle.R;
import com.yxliu.android.media.util.SoundPlayer;
import com.yxliu.android.media.util.SoundRecorder;

public class MediaActivity extends AppCompatActivity implements View.OnClickListener {

    private SoundRecorder mSoundRecorder = null;
    private SoundPlayer mSoundPlayer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media);

        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSoundRecorder != null) {
            stopMicrophone();

            mSoundRecorder = null;
        }
        if (mSoundPlayer != null){
            mSoundPlayer.stopPlaying();
            mSoundPlayer = null;
        }
    }

    private void init() {
        findViewById(R.id.btn_open_microphone).setOnClickListener(this);
        findViewById(R.id.btn_stop_microphone).setOnClickListener(this);
        findViewById(R.id.btn_player).setOnClickListener(this);
    }

    private void openMicrophone() {
        mSoundRecorder = new SoundRecorder();
        mSoundRecorder.startRecording();
    }

    private void stopMicrophone(){
        mSoundRecorder.stopRecording();
    }

    private void playing(){
        mSoundPlayer = new SoundPlayer();
        mSoundPlayer.startPlaying(mSoundRecorder.getAudioFile());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_open_microphone:
                openMicrophone();
                break;
            case R.id.btn_stop_microphone:
                stopMicrophone();
                break;
            case R.id.btn_player:
                playing();
                break;
            default:
                break;
        }
    }
}

关键布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.yxliu.android.media.MediaActivity">

    <Button
        android:id="@+id/btn_open_microphone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/open_microphone"
        android:layout_margin="20dp"/>

    <Button
        android:id="@+id/btn_stop_microphone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/stop_microphone"
        android:layout_margin="20dp"/>

    <Button
        android:id="@+id/btn_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/player"
        android:layout_margin="20dp"/>
</LinearLayout>

上一篇下一篇

猜你喜欢

热点阅读