title: Media Recorder Start Failed-22 inside a thread
body: <p>Hi i am trying to make a video recorder android app> For some reason i need to get its audio, i have tried some libraries but nothing really extracted me the audio. So now my approach is that instead of using native libraries can i record video and audio separately? Main thread records video and another thread records audio. I am getting error when i call start() method of media recorder class inside thread and i get Start Failed -22. Following is my code.</p>
  
  <pre><code>public class MainActivity extends Activity {
  
   private SurfaceHolder surfaceHolder;
      private SurfaceView surfaceView;
      public MediaRecorder mrec ;
      ImageView recordingBt;
      public MediaRecorder mrec2 = new MediaRecorder();
      File video;
      private Camera mCamera;
      File audiofile = null;
  
      File audiofile2 = null;
      boolean recording=false;
      TextView tv;
      View myview;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      recordingBt= (ImageView) findViewById(R.id.camera);
      surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
      tv=(TextView) findViewById(R.id.textView1);
  
       myview = findViewById(R.id.MyBigView);
  }
  
  @Override
  protected void onStart(){
      super.onStart();
      Log.i("Recording Activity", "we are inside Start function of Recording Activity");
  
  
  
      Log.i("Recording Activity", "Opening camera");
  
  
       mCamera = Camera.open();
  
       Log.i("Recording Activity", "Camera Opened");
          surfaceHolder = surfaceView.getHolder();
          surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          recordingBt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  if(recording==false){
                      try {
                          Log.i("Recording Activity", "Recording Start");
                          startRecording();
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
                  else{
                      stopRecording();
  
                  }
  
              }
          });}
  public void startRecording() throws IOException 
      {
  
          recording=true;
          String path= Environment.getExternalStorageDirectory().toString();
          File sampleDir = new File(path+"/DCIM/Squlium Booth");
          sampleDir.mkdir();
  
  
           Log.i("Setting Path", sampleDir.toString());
  
              try {
                audiofile = File.createTempFile("SqB", ".3gp", sampleDir);
              } catch (IOException e) {
  
                return;}
              Log.i("Recording Activity", "Seting Media Recorder attributes");   
          mrec = new MediaRecorder();  // Works well
  
          mCamera.unlock();
          mrec.setCamera(mCamera);
      mrec.setPreviewDisplay(surfaceHolder.getSurface());
       mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
           mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
           mrec.setMaxDuration(60000);
          mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
          mrec.setOutputFile(audiofile.getAbsolutePath()); 
          mrec.prepare();
          mrec.start();
          startProgress(myview);
  
  
      }
  
   protected void stopRecording() {
       recording=false;
          mrec.stop();
          mrec.release();
          mCamera.lock();
          mCamera.release();
          mrec2.stop();
          mrec.release();
  
      }
   public void startProgress(View view) {
          // Do something long
          Runnable runnable = new Runnable() {
  
            @Override
            public void run() {
  
                  String path= Environment.getExternalStorageDirectory().toString();
                  File sampleDir = new File(path+"/DCIM/Squlium Booth");
                  sampleDir.mkdir();
                  try {
                        audiofile2 = File.createTempFile("myAudio", ".mp3", sampleDir);
                      } catch (IOException e) {
  
                        return;}
  
                  int minBufferSize = AudioRecord.getMinBufferSize(8000, 
                           AudioFormat .CHANNEL_IN_MONO, 
                           AudioFormat.ENCODING_PCM_16BIT);
  
                  mrec2.setAudioSource(MediaRecorder.AudioSource.MIC);
  
                  mrec2.setAudioSamplingRate(16000);
                  mrec2.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                  mrec2.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                  mrec2.setOutputFile(audiofile2.getAbsolutePath()); 
                  try {
                      mrec2.prepare();
                  } catch (IllegalStateException e1) {
                      Log.i("Illegal", "Exception");
                      e1.printStackTrace();
                  } catch (IOException e1) {
                      Log.i("Iinput output", "Exception");
                  }
                  Log.i("Inside Run Method", "Starting Thread soon there will be issue");
                  mrec2.start();
                  Log.i("Inside Run Method", "After start method inside thread");
                Log.i("Inside Thread", "Alive");
  
              }
  
          };
          new Thread(runnable).start();
  
   }
  </code></pre>