以wav格式缩小Android中录制声音的大小

时间:2015-03-11 18:02:19

标签: java android audio

我正在使用此代码录制wav文件。但是,10秒音频创建500kb文件。我怎样才能减小尺寸。这对于10秒的声音来说太过分了。

private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final int RECORDER_SAMPLERATE = 11025;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

private AudioRecord recorder = null;
private int bufferSize = 0;
private Thread recordingThread = null;
private boolean isRecording = false;

private String mainFilename;
private String tempFilename;

private Context mContext;
/**
 * Creates a new audio recording at the given path (relative to root of SD card).
 */
public WavAudioRecorder(Activity mContext)
{
    this.mContext = mContext;
    bufferSize = AudioRecord.getMinBufferSize(4000,
            AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

    tempFilename = getTempFilename();
    mainFilename = getFilename();
}

/**
 * Starts a new recording.
 */
public void start() throws IOException
{
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
    int i = recorder.getState();
    if(i==1)
        recorder.startRecording();

    isRecording = true;

    recordingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            writeAudioDataToFile();
        }
    },"AudioRecorder Thread");

    recordingThread.start();

}

2 个答案:

答案 0 :(得分:0)

WAV是一种原始格式,因此需要大量空间是正常的。但您可以更改采样率,从而产生较小的文件。

但这也会降低质量。 所以我的建议是保持采样率并将其转换为MP3等压缩格式以实现持久性。

另一件值得尝试的事情是使用ZIP打包WAV:http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipOutputStream.html

答案 1 :(得分:-1)

你基本上要求compress the file using a LOSSY format(如MP3);这需要大量的工作,你应该评估是否真的有必要。

另一个选择是减少sampleRate ,以便每个音频块的信息更少,并使用MONO通道而不是STEREO,这会导致更小的输出文件。