title: recording and saving audio on Android
body: <p>I'm developing an android (2.2) app to record audio. The code seems to run fine and even saves the files on the SD Card, but when I try and play the audio files on my Mac nothing happens. Almost like the files are empty. I've tried saving them as .mp3 and .wav but no luck. My code is below:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  
  import android.media.MediaRecorder;
  import android.os.Environment;
  
  public class AudioRecorder {
  
      private MediaRecorder recorder = new MediaRecorder();
  
      private File outfile = null;
  
      public AudioRecorder(){}
  
      public void startRecording(String audioFile) 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
          File directory = new File(path).getParentFile();
          if (!directory.exists() &amp;&amp; !directory.mkdirs()) {
              throw new IOException("Path to file could not be created.");
          }
          try{
              File storageDir = new File(Environment
                      .getExternalStorageDirectory(), "/audio/");
              storageDir.mkdir();
              outfile=File.createTempFile(audioFile, ".wav",storageDir);
              recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
              recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
              recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
              recorder.setOutputFile(outfile.getAbsolutePath());
          }catch(IOException e){
              e.printStackTrace();
          }
  
          try{
              recorder.prepare();
          }catch(IllegalStateException e){
              e.printStackTrace();
          }
  
          recorder.start();
          }
  
      public void stop() throws IOException {
          recorder.stop();
          recorder.release();
          }
  }
  </code></pre>
  
  <p>My Activity:</p>
  
  <pre><code>/**
   * Recording Activity
   */
  
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import com.seekika.android.app.helpers.AudioRecorder;
  
  import android.app.Activity;
  import android.media.MediaRecorder;
  import android.os.Bundle;
  import android.os.Environment;
  import android.os.SystemClock;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.Chronometer;
  
  public class Record extends Activity {
  
      private static final String TAG="RecordActivity";
  
      private int SETTINGS=Menu.FIRST;
      private int LOGOUT=Menu.FIRST + 1;
      private int EXIT=Menu.FIRST + 2;
  
      //components
      private Button mBtnStartRecording;
      private Button mBtnStopRecording;
  
      private Chronometer mChronometer;
  
  
      private String audioFileName="";
      final AudioRecorder recorder = new AudioRecorder();
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.recordstory);
          initComponents();
      }
  
      public void initComponents(){
          mChronometer=(Chronometer)findViewById(R.id.chrono);
          mBtnStopRecording=(Button)findViewById(R.id.btn_stop_recording);
          mBtnStopRecording.setEnabled(false);
  
          mBtnStopRecording.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  //stop timer
                  stopRecording();
                  //stop recording and save audio file to SD card
  
              }
          });
  
          mBtnStartRecording=(Button)findViewById(R.id.btn_start_recording);
          mBtnStartRecording.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  startRecording();
                  mBtnStartRecording.setEnabled(false);
                  mBtnStopRecording.setEnabled(true);
                  //start recording audio
                  //start timer
  
              }
          });
  
      }
  
      private void startRecording(){
          mChronometer.setBase(SystemClock.elapsedRealtime());
          mChronometer.start();
  
  
          try{
              String myRecording="Seekika-" + System.currentTimeMillis();
              Log.i(TAG, "Start Recording");
              recorder.startRecording(myRecording);
          }catch(IOException e){
              Log.e(TAG,"IOException error");
              e.printStackTrace();
          }
  
      }
  
      private void stopRecording(){
          mChronometer.stop();
  
          try{
              recorder.stop();
          }catch(IOException e){
              e.printStackTrace();
          }
  
      }
  
  
  }
  </code></pre>