如何使用音频API在Android中进行录音? [英] How to record audio using audio api in Android?

查看:275
本文介绍了如何使用音频API在Android中进行录音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an LG Android Phone with Version 2.3.3. I need to connect a microphone in the headphone jack because I want to create an app that reads sound samples.

我怎样才能读取样本编程?

How can I read samples programmatically?.

推荐答案

如果你真的想读AudioSamples,我建议你使用,而不是MediaRecorder AudioRecord,因为它给你的AudioSamples更多的控制......对于您可以使用下面的code,AudioCapturer是我的,我用获取来自AudioRecord object..IAudioReceiver样品包装类是具有处理音频数据的方法的接口。

If you really want to read AudioSamples, I would suggest you to use AudioRecord instead of MediaRecorder since it gives you more control on AudioSamples... For that you can use following code, AudioCapturer is my wrapper class which I use for getting the samples from AudioRecord object..IAudioReceiver is an interface which has methods for handling audio data.

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;
    }

}

现在你可以使用这个类的对象从主类,你的程序中,它会开始给你的音频采样,你可以处理它们在你的IAudioReceiver(类,使用这些样本)。

Now you can use this class's object from the Main class of you program and it will start giving you audio Samples you can handle them inside your IAudioReceiver (class which uses these samples)..

如果你仍然想使用MediaRecorder,此链接可以对你有用,

If you still want to use MediaRecorder, this link can be useful to you,

这篇关于如何使用音频API在Android中进行录音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆