2

Thanks in advance for spent Ur time on my app...

I have an application in which I require to create an audio file after clicking ona button named "RECORD" and send this file to server. The audio file must be send in any android supported audio format. How can I achieve this...

Maddy Sharma
  • 4,870
  • 2
  • 34
  • 40
  • What you have tried yet ... where is the code ?? What are you looking for or you want to write your application by someone here !! – Daud Arfin Aug 09 '12 at 11:32
  • I want to create an application in which I need to create a recording file and the send it to the server. What are the key points, which must be keep in mind at the time of development? – Maddy Sharma Aug 21 '12 at 09:54
  • go through this link you will get all the details related recording .. http://developer.android.com/reference/android/media/MediaRecorder.html – Daud Arfin Aug 21 '12 at 11:51
  • Thanks for this...! Is this possible to save recording with the help of emulator.I mean Can We save the recording files on the system HDD. – Maddy Sharma Aug 23 '12 at 05:17
  • try this http://stackoverflow.com/questions/5254994/can-the-android-emulator-record-and-play-back-audio-using-pc-hardware – Daud Arfin Aug 23 '12 at 05:28
  • 1
    did you find a solution for this? Can you please share, how are you sending this file to server? – ggsrivas Apr 24 '18 at 18:58

1 Answers1

3

Code For Recording in Android

main.java:

package com.example.audiosend;

import android.os.Bundle;
import android.app.Activity;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity 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;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      text = (TextView) findViewById(R.id.text1);

      // store it to sd card

outputFile = Environment.getExternalStorageDirectory().
              getAbsolutePath() + "/MyAppRecording.3gpp"; //this is the folder in which your Audio file willl save

      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);
        }
      });
   }

   public void start(View view){
       try {
          myRecorder.prepare();
          myRecorder.start();
       } 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.release();
          myRecorder  = null;

          stopBtn.setEnabled(false);
          playBtn.setEnabled(true);
          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();
        }
   }
LittleGirl
  • 658
  • 6
  • 26