title: Android call recorder application
body: <p>I have developed basic call recorder application, its working fine on devices with versions below 'lollipop', but when I have to run lollipop devices (Android 5.0) I'm getting this error.</p>
  
  <pre><code>E/MediaRecorder(29786): start failed: -2147483648
  java.lang.RuntimeException: start failed.
  at android.media.MediaRecorder.start(Native Method)
  at com.androiddoc.callRecorder_anirbanjana.CallRecordingService.startRec(CallRecordingService.java:234)
  at com.androiddoc.callRecorder_anirbanjana.CallRecordingService.onCreate(CallRecordingService.java:54)
  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2763)
  at android.app.ActivityThread.access$1800(ActivityThread.java:148)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5312)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
  </code></pre>
  
  <p>My code is below. When I call recorder.prepare(); and recorder.start(); Now I'm getting above error and I can't work out why.  Can anyone advise?</p>
  
  <pre><code>public class CallRecordingService extends Service {
  
      MediaRecorder myRec = new MediaRecorder();
      boolean isRecMode = false;
  
      private static final int RECORDER_SAMPLERATE = 8000;
      private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
      private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
      private AudioRecord recorder = null;
      private Thread recordingThread = null;
      private boolean isRecording = false;
  
      int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
              RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
  
      @Override
      public IBinder onBind(Intent arg0) {
          return null;
      }
  
      public int onStartCommand(Intent intent, int flags, int startId) {
          return START_STICKY_COMPATIBILITY;
      }
  
      @Override
      public void onCreate() {
          // Start recording
           startRec();
  
  
          System.out.println("BUFFER SIZE VALUE IS " + bufferSize);
          super.onCreate();
      }
  
      public void stopRec() {
          myRec.reset();
          Toast.makeText(CallRecordingService.this, "Rec Stoped",
                  Toast.LENGTH_SHORT).show();
          Log.i("CallRecordingService", "Rec Stoped");
          isRecMode = false;
      }
  
  
      public void startRec() {
          try {
              // String selectedPath = this.getApplicationContext().getFilesDir()
              // + "/system_sound";
              String selectedPath = Environment.getExternalStorageDirectory()
                      + "/Testing";
  
              SimpleDateFormat sdf = new SimpleDateFormat(
                      "dd_MM_yyyy_hh_mm_ss aa");
              String currentDateandTime = sdf.format(new Date());
  
              // Crate folder if not exist
              // String packageName = this.getPackageName();
              File yourDir = new File(Environment.getExternalStorageDirectory()
                      + "/Testing");
              // File yourDir = new
              // File(this.getApplicationContext().getFilesDir() +
              // "/system_sound");
              if (!yourDir.exists()) {
                  yourDir.mkdirs();
              }
              // String selectedPath =
              // Environment.getExternalStorageDirectory().getAbsolutePath() +
              // "/Android/data/" + packageName + "/system_sound";
  
              myRec.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
              myRec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
              myRec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
              // myRec.setOutputFile(Environment.getExternalStorageDirectory().getPath()+"/my_rec_voice.mp3");
              myRec.setOutputFile(selectedPath + "/" + currentDateandTime + "_"
                      + getFromPreference("phoneNo") + ".amr");
  
              AudioManager am; // Audio manager
  
              am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
              am.setMode(AudioManager.MODE_IN_CALL);
              int volume_level = am
                      .getStreamVolume(AudioManager.STREAM_VOICE_CALL);
              int max_volume = am
                      .getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
              if (volume_level &lt; max_volume) {
                  am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, max_volume,
                          AudioManager.FLAG_SHOW_UI);
              }
          } catch (IllegalStateException e) {
              // Log.e("Call recorder IllegalStateException: ", "");
              terminateAndEraseFile();
          } catch (Exception e) {
              // Log.e("Call recorder Exception: ", "");
              terminateAndEraseFile();
          }
  
          OnErrorListener errorListener = new OnErrorListener() {
  
              public void onError(MediaRecorder arg0, int arg1, int arg2) {
                  terminateAndEraseFile();
              }
  
          };
          myRec.setOnErrorListener(errorListener);
  
          try {
              Log.v("Call Recorder", "Media recorder prepared");
              myRec.prepare();
          } catch (IllegalStateException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          try {
              Log.v("Call Recorder", "Media recorder started ");
              myRec.start();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          Toast.makeText(CallRecordingService.this, "Rec Start",
                  Toast.LENGTH_SHORT).show();
          Log.i("CallRecordingService", "Rec Start");
          isRecMode = true;
      }
  
      @Override
      public void onDestroy() {
           stopRec();
          //stopRecording();
          if (isRecMode) {
              // Stop recording
              myRec.stop();
          }
  
          myRec.release();
          super.onDestroy();
      }
  
      // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      // Preference Variable
      // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
      // --------------------------------------------
      // method to save variable in preference
      // --------------------------------------------
      public void saveInPreference(String name, String content) {
          SharedPreferences preferences = PreferenceManager
                  .getDefaultSharedPreferences(this);
          SharedPreferences.Editor editor = preferences.edit();
          editor.putString(name, content);
          editor.commit();
      }
  
      // --------------------------------------------
      // getting content from preferences
      // --------------------------------------------
      public String getFromPreference(String variable_name) {
          String preference_return;
          SharedPreferences preferences = PreferenceManager
                  .getDefaultSharedPreferences(this);
          preference_return = preferences.getString(variable_name, "");
  
          return preference_return;
      }
  
      // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      // Preference Variable
      // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
      private void terminateAndEraseFile() {
          try {
  
              Toast toast = Toast.makeText(this,
                      this.getString(R.string.reciever_end_call),
                      Toast.LENGTH_SHORT);
              toast.show();
          } catch (IllegalStateException e) {
              e.printStackTrace();
          }
  
          Toast toast = Toast.makeText(this,
                  this.getString(R.string.record_impossible), Toast.LENGTH_LONG);
          toast.show();
      }
  
  }
  </code></pre>