其他分享
首页 > 其他分享> > 如何在Android中使用音频api录制音频?

如何在Android中使用音频api录制音频?

作者:互联网

我有一个版本为2.3.3的LG Android手机.我需要连接耳机插孔中的麦克风,因为我想创建一个读取声音样本的应用程序.

如何以编程方式读取样本?

解决方法:

如果您真的想阅读AudioSamples,我建议您使用AudioRecord而不是MediaRecorder,因为它可以让您更好地控制AudioSamples …为此您可以使用以下代码,AudioCapturer是我的包装类,我用它来获取样本AudioRecord object..IAudioReceiver是一个具有处理音频数据的方法的接口.

public class AudioCapturer implements Runnable {

    private AudioRecord audioRecorder = null;
    private int bufferSize;
    private int samplePerSec = 16000;
    private String LOG_TAG = "AudioCapturer";
    private Thread thread = null;

    private boolean isRecording;
    private static AudioCapturer audioCapturer;

    private IAudioReceiver iAudioReceiver;

    private AudioCapturer(IAudioReceiver audioReceiver) {
        this.iAudioReceiver = audioReceiver;
    }

    public static AudioCapturer getInstance(IAudioReceiver audioReceiver) {
        if (audioCapturer == null) {
            audioCapturer = new AudioCapturer(audioReceiver);
        }
        return audioCapturer;
    }

    public void start() {

        bufferSize = AudioRecord.getMinBufferSize(samplePerSec, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

        if (bufferSize != AudioRecord.ERROR_BAD_VALUE && bufferSize != AudioRecord.ERROR) {

            audioRecorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, this.samplePerSec, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, this.bufferSize * 10); // bufferSize
                                                                            // 10x

            if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
                Log.i(LOG_TAG, "Audio Recorder created");


                audioRecorder.startRecording();
                isRecording = true;
                thread = new Thread(this);
                thread.start();

            } else {
                Log.e(LOG_TAG, "Unable to create AudioRecord instance");
            }

        } else {
            Log.e(LOG_TAG, "Unable to get minimum buffer size");
        }
    }

    public void stop() {
        isRecording = false;
        if (audioRecorder != null) {
            if (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
                // System.out
                // .println("Stopping the recorder inside AudioRecorder");
                audioRecorder.stop();
            }
            if (audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
                audioRecorder.release();
            }
        }
    }

    public boolean isRecording() {
        return (audioRecorder != null) ? (audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) : false;
    }

    @Override
    public void run() {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);  
        while (isRecording && audioRecorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
            short[] tempBuf = new short[Constants.FRAME_SIZE / 2];
            audioRecorder.read(tempBuf, 0, tempBuf.length);     
            iAudioReceiver.capturedAudioReceived(tempBuf, false);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#finalize()
     */
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("AudioCapturer finalizer");
        if (audioRecorder != null && audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) {
            audioRecorder.stop();
            audioRecorder.release();
        }
        audioRecorder = null;
            iAudioReceiver = null;  
        thread = null;
    }

}

现在你可以在你的程序的Main类中使用这个类的对象,它将开始为你提供音频样本,你可以在你的IAudioReceiver(使用这些样本的类)中处理它们.

如果您仍想使用MediaRecorder,this link对您有用,

标签:android,microphone
来源: https://codeday.me/bug/20190613/1232590.html