3

I am using this code to record a wav file. However 10 second audio is create 500kb file. How can i reduce the size. This is too much for 10 seconds voice.

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();

}
Marvin Emil Brach
  • 3,984
  • 1
  • 32
  • 62
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • Wouldn't ENCODING_PCM_8BIT reduce the size from 16-bit to 8-bit? If the name is any giveaway, that should cut the file size in half, but at the cost of quality you're going to have to ask yourself if it's the best move. – Cruceo Mar 11 '15 at 18:06
  • i tried , it gives buffer errors – Muhammad Umar Mar 11 '15 at 18:08
  • Did you tried to compress the data through http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipOutputStream.html before writing to file? – Marvin Emil Brach Mar 11 '15 at 18:47

2 Answers2

0

WAV is a raw format for that it's normal to need a lot of space. But you could change the sample rate resulting in smaller files.

But this will lower the quality, too. So my advice is to keep a sample rate and convert it to a compressed format like MP3 for persistence.

Another thing worth trying is to pack the WAV with ZIP: http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipOutputStream.html

Marvin Emil Brach
  • 3,984
  • 1
  • 32
  • 62
-1

You are basically asking to compress the file using a LOSSY format (like MP3); this requires a lot of work and you should evaluate if it is really necessary.

Another option is to reduce the sampleRate in order to have less information per audio chunk and use MONO channel instead of STEREO, which lead to a smaller output file.

Community
  • 1
  • 1
bonnyz
  • 13,458
  • 5
  • 46
  • 70