Android 实现清晰录音

本文介绍了如何在Android中使用ExtAudioRecorder类实现清晰的WAV格式录音,通过链接到StackOverflow和i-liger.com的文章提供了详细步骤。内容包括设置录音类及在activity_main.xml中配置开始和停止录音的按钮。
摘要由CSDN通过智能技术生成

一、ExtAudioRecorder

        录音类,来源于http://stackoverflow.com/questions/4871149/how-to-record-voice-in-wav-formt-using-android

package com.jltxgcy.wavrecord;

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

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;
import android.util.Log;

public class ExtAudioRecorder 
{
    private final static int[] sampleRates = {44100, 22050, 11025, 8000};

    public static ExtAudioRecorder getInstanse(Boolean recordingCompressed)
    {
        ExtAudioRecorder result = null;

        if(recordingCompressed)
        {
            result = new ExtAudioRecorder(    false, 
                                            AudioSource.MIC, 
                                            sampleRates[3], 
                                            AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                            AudioFormat.ENCODING_PCM_16BIT);
        }
        else
        {
            int i=0;
            do
            {
                result = new ExtAudioRecorder(    true, 
                                                AudioSource.MIC, 
                                                sampleRates[i], 
                                                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                                AudioFormat.ENCODING_PCM_16BIT);

            } while((++i<sampleRates.length) & !(result.getState() == ExtAudioRecorder.State.INITIALIZING));
        }
        return result;
    }

    /**
    * INITIALIZING : recorder is initializing;
    * READY : recorder has been initialized, recorder not yet started
    * RECORDING : recording
    * ERROR : reconstruction needed
    * STOPPED: reset needed
    */
    public enum State {INITIALIZING, READY, RECORDING, ERROR, STOPPED};

    public static final boolean RECORDING_UNCOMPRESSED = true;
    public static final boolean RECORDING_COMPRESSED = false;

    // The interval in which the recorded samples are output to the file
    // Used only in uncompressed mode
    private static final int TIMER_INTERVAL = 120;

    // Toggles uncompressed recording on/off; RECORDING_UNCOMPRESSED / RECORDING_COMPRESSED
    private boolean         rUncompressed;

    // Recorder used for uncompressed recording
    private AudioRecord     audioRecorder = null;

    // Recorder used for compressed recording
    private MediaRecorder   mediaRecorder = null;

    // Stores current amplitude (only in uncompressed mode)
    private int             cAmplitude= 0;

    // Output file path
    private String          filePath = null;

    // Recorder state; see State
    private State              state;

    // File writer (only in uncompressed mode)
    private RandomAccessFile randomAccessWriter;

    // Number of channels, sample rate, sample size(size in bits), buffer size, audio source, sample size(see AudioFormat)
    private short                    nChannels;
    private int                      sRate;
    private short                    bSamples;
    private int                      bufferSize;
    private int                      aSource;
    private int                      aFormat;

    // Number of frames written to file on each output(only in uncompressed mode)
    private int                      framePeriod;

    // Buffer for output(only in uncompressed mode)
    private byte[]                   buffer;

    // Number of bytes written to file after header(only in uncompressed mode)
    // after stop() is called, this size is written to the header/data chunk in the wave file
    private int                      payloadSize;

    /**
    *
    * Returns the state of the recorder in a RehearsalAudioRecord.State typed object.
    * Useful, as no exceptions are thrown.
    *
    * @return recorder state
    */
    public State getState()
    {
        return state;
    }

    /*
    *
    * Method used for recording.
    *
    */
    private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpd
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值