简体   繁体   中英

How to get an Audio Stream from Media Recorder in android

I want to make streaming audio recorder in android. I am not sure how to fetch audio stream and how to set buffer size of audio stream.

Below is my media recorder class

public class MyMediaRecorder {

    final MediaRecorder recorder = new MediaRecorder();
    final File path;

    /**
     * Creates a new audio recording at the given path (relative to root of SD
     * card).
     */
    public MyMediaRecorder(File path) {
        this.path = path;
    }



    /**
     * Starts a new recording.
     */
    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            throw new IOException("SD Card is not mounted.  It is " + state
                    + ".");
        }

        // make sure the directory we plan to store the recording in exists


        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(path.getAbsolutePath());
        recorder.prepare();
        recorder.start();
    }

    /**
     * Stops a recording that has been previously started.
     */
    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
    }
}

on start of recording i need to fetch a buffer size and sent it to server parallely record audio. Should I use Audio Record and Audio track instaed of Media Recorder ? Please suggest what should i do

You should use AudioRecord . Here is a simple code that shows how to use AudioRecord class.

final int sampleRate = 48000;
final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

int minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
AudioRecord microphone = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, minBufferSize * 10);

microphone.startRecording();

//Since audioformat is 16 bit, we need to create a 16 bit (short data type) buffer
short[] buffer = new short[1024];

while(!stopped) {

    int readSize = microphone.read(buffer, 0, buffer.length);
    sendDataToServer(buffer, readSize);

}

microphone.stop();
microphone.release();

If you need a real-time audio recorder and streamer, you should be careful about performance and read all data as fast as possible and manage them to send to the server. There are lot's of projects in Github that you can reuse them and learn from their idea. I share some of them (specially recorder class) here but you can search and find more.
1. AudioRecorder
2. FullDuplexNetworkAudioCallService
3. MicRecorder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM