title: How can I record voice in android as long as hold a button?
body: <p>I want to record voice as long as holding the record button and save that voice into the raw folder in my project. I used the code below. Altough there appears no errors, i couldn't get any output. What can be the problem ? Do you have any suggestions? Thanks,</p>
  
  <pre><code>public boolean onTouch(View v, MotionEvent event) {
      // TODO Auto-generated method stub
  
  
      Runnable mAction = new Runnable() {
          public void run() {
              System.out.println("Performing action...");
  
              int frequency=11025;
              int channelConfiguration=AudioFormat.CHANNEL_CONFIGURATION_MONO;
              int audioEncoding= AudioFormat.ENCODING_PCM_16BIT;
              File file=new File(Environment.getExternalStorageDirectory(),"raw.pcm");
  
              try{
                  file.createNewFile();
              }catch(IOException e){}
  
              try{
                  OutputStream os=new FileOutputStream(file);
                  BufferedOutputStream bos=new BufferedOutputStream(os);
                  DataOutputStream dos=new DataOutputStream(bos);
  
                  int bufferSize=AudioRecord.getMinBufferSize(frequency, channelConfiguration,
                          audioEncoding);
  
                  short[] buffer=new short[bufferSize];
                  audioRecorder=new AudioRecord(MediaRecorder.AudioSource.MIC,
                          frequency, channelConfiguration, audioEncoding, bufferSize);
  
                  audioRecorder.startRecording();
  
                  isRecording=true;
  
                  while(isRecording){
  
                      int                                    bufferReadResult=audioRecorder.read(buffer, 0,bufferSize);
  
                      for(int i=0;i&lt;bufferReadResult;i++){
  
                          dos.writeShort(buffer[i]);
  
                      }
  
                  }
  
                  audioRecorder.stop();
                  dos.close();
  
              }catch(Throwable t){}
  
          }
      };
  
      switch(event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          if (myHandler != null) return true;
          myHandler = new Handler();
          myHandler.postDelayed(mAction, 500);
          break;
      case MotionEvent.ACTION_UP:
  
          if (myHandler == null) return true;
  
  
          isRecording=false;
  
          myHandler.removeCallbacks(mAction);
          myHandler = null;
          break;
      }
  
      return false;
  }
  </code></pre>