这里给出一个简单的Java版本的Android AudioRecorder案例:

import android.media.MediaRecorder;
import android.os.Environment;
import java.io.File;
import java.io.IOException;

public class AudioRecorder {
    private MediaRecorder recorder;
    private String filePath;
    private boolean isRecording = false;

    public AudioRecorder() {
        recorder = new MediaRecorder();
    }

    public void startRecording() {
        if (isRecording) return;

        String state = Environment.getExternalStorageState();
        if(!state.equals(Environment.MEDIA_MOUNTED)) {
            throw new RuntimeException("SD Card is not mounted.  It is " + state + ".");
        }

        File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "AudioRecorder");
        if (!directory.exists()) {
            directory.mkdirs();
        }

        String fileName = String.format("%d.amr", System.currentTimeMillis());
        filePath = directory.getAbsolutePath() + File.separator + fileName;

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(filePath);

        try {
            recorder.prepare();
        } catch (IOException e) {
            throw new RuntimeException("prepare() failed");
        }

        recorder.start();
        isRecording = true;
    }

    public void stopRecording() {
        if (!isRecording) return;

        recorder.stop();
        recorder.release();
        isRecording = false;
    }

    public String getFilePath() {
        return filePath;
    }
}

使用方法:

AudioRecorder recorder = new AudioRecorder();
recorder.startRecording();
// recording...
recorder.stopRecording();
String filePath = recorder.getFilePath();
写一个 java版本的安卓AudioRecorder案例

原文地址: https://www.cveoy.top/t/topic/bMUk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录