Android 录制amr格式音频文件工具类


import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Handler;
import android.util.Log;

import com.sv.sipmag.webrtc.Constants;

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

public class AmrAudioRecorder {
    private static final String TAG = "AmrAudioRecorder";

    private static final int DEFAULT_MAX_DURATION = 60; // 默认最长录制时长,单位为秒

    private static AmrAudioRecorder instance;

    private MediaRecorder mediaRecorder;
    private File outputFile;
    private int maxDuration;
    private OnRecordCompleteListener onRecordCompleteListener;
    private Handler handler;

    private MediaPlayer mediaPlayer;

    public interface OnRecordCompleteListener {
        void onRecordComplete(File outputFile);
    }

    private AmrAudioRecorder() {
        this.maxDuration = DEFAULT_MAX_DURATION;
        handler = new Handler();
    }

    public static AmrAudioRecorder getInstance() {
        if (instance == null) {
            synchronized (AmrAudioRecorder.class) {
                if (instance == null) {
                    instance = new AmrAudioRecorder();
                }
            }
        }
        return instance;
    }

    public void setMaxDuration(int maxDuration) {
        this.maxDuration = maxDuration;
    }

    public void setOnRecordCompleteListener(OnRecordCompleteListener onRecordCompleteListener) {
        this.onRecordCompleteListener = onRecordCompleteListener;
    }

    public void startRecording() {
        if (mediaRecorder != null) {
            stopRecording();
        }
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        outputFile = new File(Constants.PATH_AUDIO_DIR + System.currentTimeMillis() + ".amr");
        mediaRecorder.setOutputFile(outputFile.getAbsolutePath());
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stopRecording();
                }
            }, maxDuration * 1000);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopRecording() {
        if (mediaRecorder != null) {
            try {
                mediaRecorder.stop();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
            mediaRecorder.release();
            mediaRecorder = null;
            if (onRecordCompleteListener != null) {
                onRecordCompleteListener.onRecordComplete(outputFile);
            }
        }
    }

    public void startPlaying(String audioFilePath) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnCompletionListener(mp -> {
                stopPlaying();
            });
        }

        try {
            mediaPlayer.setDataSource(audioFilePath);
            mediaPlayer.prepare();
            mediaPlayer.start();
            Log.d(TAG, "Started playing audio");
        } catch (IOException e) {
            Log.e(TAG, "Error preparing audio player", e);
        }
    }

    public void stopPlaying() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
            Log.d(TAG, "Stopped playing audio");
        }
    }

    public String getAudioFilePath() {
        return outputFile.getPath();
    }
}

你可能感兴趣的:(android,java,开发语言)