Thursday, 29 September 2016

How to record Voice in android

public class AudioActivity extends Activity {

   private MediaRecorder myRecorder;
   private MediaPlayer myPlayer;
   private String outputFile = null;
   private Button startBtn;
   private Button stopBtn;
   private Button playBtn;
   private Button stopPlayBtn;
   private TextView text;
   private ImageView mic_image;
   
   @Override   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.audio);
      
      text = (TextView) findViewById(R.id.text1);
      mic_image=(ImageView)findViewById(R.id.micImage);
      // store it to sd card      String recordedAudioFile=getRecoredAudioFile();
      
      java.util.Date date= new java.util.Date();
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                           .format(date.getTime());
      outputFile = Environment.getExternalStorageDirectory().
            getAbsolutePath() + "/Music/"+"AUD_"+timeStamp+".mp3";

      myRecorder = new MediaRecorder();
      myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
      myRecorder.setOutputFile(outputFile);
      
      startBtn = (Button)findViewById(R.id.start);
      startBtn.setOnClickListener(new OnClickListener() {
      
      @Override      public void onClick(View v) {
         // TODO Auto-generated method stub         start(v);
      }
      });
      
      stopBtn = (Button)findViewById(R.id.stop);
      stopBtn.setOnClickListener(new OnClickListener()
     {
      
      @Override      public void onClick(View v) {
         // TODO Auto-generated method stub         stop(v);
      }
      });
      
      playBtn = (Button)findViewById(R.id.play);
      playBtn.setOnClickListener(new OnClickListener() {
      
      @Override      public void onClick(View v) {
         // TODO Auto-generated method stub            play(v);   
      }
      });
      
      stopPlayBtn = (Button)findViewById(R.id.stopPlay);
      stopPlayBtn.setOnClickListener(new OnClickListener() {
      
      @Override      public void onClick(View v) {
         // TODO Auto-generated method stub         stopPlay(v);
      }
      });
   }

  
private String getRecoredAudioFile() {
   String returnAudio;
   java.util.Date date= new java.util.Date();
   returnAudio = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date.getTime());
   return returnAudio;
}

public void start(View view)
{
      try {
          myRecorder.prepare();
          myRecorder.start();
          
          Animation animation = new AlphaAnimation(1, 0); // Change alpha       // from fully       // visible to       // invisible       animation.setDuration(500); // duration - half a second       animation.setInterpolator(new LinearInterpolator()); // do not alter       // animation       // rate       animation.setRepeatCount(Animation.INFINITE); // Repeat animation       // infinitely       animation.setRepeatMode(Animation.REVERSE); // Reverse animation at
       // the       // end so the layout will       // fade back in       mic_image.startAnimation(animation);
       } catch (IllegalStateException e) {
          // start:it is called before prepare()         // prepare: it is called after start() or before setOutputFormat()           e.printStackTrace();
       } catch (IOException e) {
           // prepare() fails           e.printStackTrace();
        }
      
       text.setText("Recording Point: Recording");
       startBtn.setEnabled(false);
       stopBtn.setEnabled(true);
       
       Toast.makeText(getApplicationContext(), "Start recording...", 
             Toast.LENGTH_SHORT).show();
   }

   public void stop(View view)
   {
      try {
         myRecorder.stop();
         myRecorder.reset();
         myRecorder.release();
         stopBtn.setEnabled(false);
         playBtn.setEnabled(true);
         myRecorder  = null;

         mic_image.clearAnimation();
         text.setText("Recording Point: Stop recording");
         
         Toast.makeText(getApplicationContext(), "Stop recording...",
               Toast.LENGTH_SHORT).show();
      } catch (IllegalStateException e) {
         //  it is called before start()         e.printStackTrace();
      } catch (RuntimeException e) {
         // no valid audio/video data has been received         e.printStackTrace();
      }
   }
  
   public void play(View view) {
      try{
         myPlayer = new MediaPlayer();
         myPlayer.setDataSource(outputFile);
         myPlayer.prepare();
         myPlayer.start();
         
         playBtn.setEnabled(false);
         stopPlayBtn.setEnabled(true);
         text.setText("Recording Point: Playing");
         
         Toast.makeText(getApplicationContext(), "Start play the recording...", 
               Toast.LENGTH_SHORT).show();
      } catch (Exception e) {
         // TODO Auto-generated catch block         e.printStackTrace();
      }
   }
   
   public void stopPlay(View view) {
      try {
          if (myPlayer != null)
         {

             myPlayer.stop();
              myPlayer.release();
              myPlayer = null;
              playBtn.setEnabled(true);
              stopPlayBtn.setEnabled(false);
              text.setText("Recording Point: Stop playing");
              
              Toast.makeText(getApplicationContext(), "Stop playing the recording...", 
                  Toast.LENGTH_SHORT).show();
          }
      } catch (Exception e) {
         // TODO Auto-generated catch block         e.printStackTrace();
      }
   }

audio.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >

    <TextView        android:id="@+id/text1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="Recording Point: -"        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView        android:id="@+id/micImage"        android:layout_width="60dp"        android:layout_height="60dp"        android:layout_below="@+id/text1"        android:layout_centerHorizontal="true"        android:layout_marginTop="20dp"        android:src="@android:drawable/presence_audio_online" />

    <LinearLayout        android:gravity="center_horizontal"        android:id="@+id/linear1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/micImage"        android:layout_marginTop="10dp" android:weightSum="10"        android:orientation="vertical" >

        <Button            android:id="@+id/start" android:layout_weight="5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="start" />

        <Button            android:id="@+id/stop" android:layout_weight="5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:enabled="false"            android:text="stop" />
    </LinearLayout>

    <LinearLayout        android:gravity="center_horizontal"        android:id="@+id/linear2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/linear1"        android:layout_marginTop="10dp" android:weightSum="10"        android:orientation="vertical" >

        <Button            android:id="@+id/play"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:enabled="false" android:layout_weight="5"            android:text="play" />

        <Button            android:id="@+id/stopPlay"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp" android:layout_weight="5"            android:enabled="false"            android:text="stop playing" />
    </LinearLayout>

    

</RelativeLayout>

No comments:

Post a Comment