call recorder help needed

All your problems with Audio, Video and Images.

call recorder help needed

Postby ud-an » Mon Oct 18, 2010 8:07 am

After googling a lot i am unable to find proper solution i am trying to record calls for android. I just got little source and tried little my self . It creating 3gp file but not playing. I know their is problem recording incoming calls but its not playing file so its not recording outgoing call need help.
Code: Select all
[code]


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class androidrec extends Activity
{
   Button btn_start;
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState); setContentView(R.layout.main);
      btn_start = (Button) findViewById(R.id.btn_start);
      UpdateRecorderState();
      btn_start.setOnClickListener(new View.OnClickListener()
      {
         public void onClick(View v)
         {             
            //          Toast.makeText(getBaseContext(),"Please enter both phone number and message.",
            //          Toast.LENGTH_SHORT).show();
            if(!SharedData._Started) { StartServicesAtStartUp.Start_CallRec(getBaseContext()); }
            else { StartServicesAtStartUp.Stop_CallRec(getBaseContext()); }
            UpdateRecorderState();
         }
      });       


   }

   private void UpdateRecorderState()
   {
      if(SharedData._Started)
      {btn_start.setText("Stop Recording");}
      else
      {btn_start.setText("Start Recording");}
   }
}[/code]


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.media.MediaRecorder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

//import com.lumitrend.netlogger.Logger;

public class CallStateListener extends PhoneStateListener {
   public void onCallStateChanged(int state, String incomingNumber)
   {
      super.onCallStateChanged(state, incomingNumber);
      switch(state)
      {
      case TelephonyManager.CALL_STATE_IDLE:
         if(SharedData._Recording)
         { Recorders_Stop(); }
         break;
      case TelephonyManager.CALL_STATE_RINGING:
         break;
      case TelephonyManager.CALL_STATE_OFFHOOK:
         String CallDate = SanityDate();
         String CallNum = SanityNum(incomingNumber);
         String RootDir = SharedData._Path ; 
         String CallDir = SharedData._Path + CallNum + "/" ;
         String CallFile = SharedData._Path +  CallNum + "/" + CallNum + CallDate ;
         if(!SharedData._Recording)
         {
            SharedData._Recording = true;
            String med_state = android.os.Environment.getExternalStorageState();
            if(!med_state.equals(android.os.Environment.MEDIA_MOUNTED))
            { break; }

            File directory = null;
            directory = new File(RootDir + "text.txt" ).getParentFile();
            if (!directory.exists() && !directory.mkdirs())
            { break; }

            directory = new File(CallDir + "text.txt" ).getParentFile();
            if (!directory.exists() && !directory.mkdirs())
            { break; }

            Recoders_Init(CallFile);
            Recorder_Prepare();
         }
         Log.v("DEBUG", TelephonyManager.CALL_STATE_OFFHOOK + " ITS.CallRecorder - Recording Started " + state);
         break;
      }
   }

   private String SanityDate() {
      SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd-HHmmss");
      Date currentTime_1 = new Date();
      return formatter.format(currentTime_1);
   }

   private void Recorders_Stop() {
      try {
         SharedData._recorder.stop(); SharedData._recorder.reset();
         //SharedData._recorder_down.stop(); SharedData._recorder_down.reset();
         //SharedData._recorder_up.stop();       SharedData._recorder_up.reset();
      }
      catch (IllegalStateException e) {}
      SharedData._Recording = false;
   }

   private void Recorder_Prepare() {
      try {
         SharedData._recorder.prepare(); SharedData._recorder.start();
         //SharedData._recorder_down.prepare(); SharedData._recorder_down.start();
         //SharedData._recorder_up.prepare(); SharedData._recorder_up.start();
      }
      catch (IllegalStateException e) { e.printStackTrace(); }
      catch (IOException e) { e.printStackTrace(); }
   }

   private void Recoders_Init(String path) {
      String _ext = ".3gp";
      int out_format = MediaRecorder.OutputFormat.THREE_GPP;

      SharedData._recorder.setAudioSource(SharedData._Rec_Type);
      SharedData._recorder.setOutputFormat(out_format);
      SharedData._recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      SharedData._recorder.setOutputFile(path + "both" + _ext);

      /*
                SharedData._recorder_down.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
                SharedData._recorder_down.setOutputFormat(out_format);
                SharedData._recorder_down.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                SharedData._recorder_down.setOutputFile(path + "-down" + _ext);

                SharedData._recorder_up.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK);
                SharedData._recorder_up.setOutputFormat(out_format);
                SharedData._recorder_up.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                SharedData._recorder_up.setOutputFile(path  + "-up" + _ext);
       */

   }
   private String SanityNum(String numstr)
   {
      String out = "";
      for(char ch : numstr.toCharArray())
      {
         switch(ch)
         {
         case ' ':
            break;
         case '~':
            break;
         case '!':
            break;
         case '@':
            break;
         case '#':
            break;
         case '$':
            break;
         case '%':
            break;
         case '^':
            break;
         case '&':
            break;
         case '*':
            break;
         case '(':
            break;
         case ')':
            break;
         case '-':
            break;
         case '_':
            break;
         case '=':
            break;
         case '|':
            break;
         default:
            out = out + ch;
         }
      }
      return out;
   }
}


import android.media.MediaRecorder;

final public class SharedData
{

   static int _Rec_Type = android.media.MediaRecorder.AudioSource.VOICE_CALL;


   static String _Path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/ITS-CallRecorder/";
   static boolean _Started = false;
   static boolean _AutoStart = true;
   static boolean _Recording = false;

   static MediaRecorder _recorder = new MediaRecorder();
   static MediaRecorder _recorder_down = new MediaRecorder();
   static MediaRecorder _recorder_up = new MediaRecorder();

   SharedData() {    }
}

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;



public class StartServicesAtStartUp extends BroadcastReceiver
{
   public static Intent phoneStateListener;
   public void onReceive(Context context, Intent intent)
   {
      Log.d("DEBUG", "com.its.CallRecorder Initiated ...");
      Start_CallRec(context);
   }

   public static void Start_CallRec(Context context)
   {
      if(!SharedData._Started )
      {
         if(SharedData._AutoStart)
         {
            phoneStateListener = new Intent(context, CallStateListener.class);
            phoneStateListener.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(phoneStateListener);
            Log.d("DEBUG", "com.its.CallRecorder Call Recorder Started ...");
            TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            CallStateListener callStateListener = new CallStateListener();
            tManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
            SharedData._Started = true;
            Toast.makeText(context," Call Recording Started ... ", Toast.LENGTH_SHORT).show();
         }       
      }
      else
      {
         Toast.makeText(context," Call Recording Already Active.. ", Toast.LENGTH_SHORT).show();
      }
   }

   public static void Stop_CallRec(Context context)
   {
      if(SharedData._Started )
      {
         context.stopService(phoneStateListener);
         Toast.makeText(context," Call Recording Stopped  ... ", Toast.LENGTH_SHORT).show();
         SharedData._Started = false;                   
      }
      else
      {
         Toast.makeText(context," Call Recording Already Stopped  ... ",  Toast.LENGTH_SHORT).show();
      }
   }

}
ud-an
Freshman
Freshman
 
Posts: 2
Joined: Sat Sep 04, 2010 5:46 am

Top

Re: call recorder help needed

Postby techkanika » Tue Oct 26, 2010 7:13 am

Hi ud-an..

Please post your code in java code tags from now onwards.
For recording calls you need to attach MediaRecorder with incoming and outgoing calls.
Use PhoneStateListener to find call state.

Thanks
Kanika
Thanks
Kanika Rakheja
User avatar
techkanika
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Fri May 28, 2010 7:49 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 19 guests