views:

81

answers:

2

I am trying out the record outgoing call using mic written this code but not working, i tested the code for simple audio record it works fine, I am not sure when to start media record i putted start in broadcast receiver may be problem is there.

Here Audiorecoder is another class is created where i have implemented MediaRecoder

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        audrec = new AudioRecorder("newcall");
        this.context = context;
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_DIAL)) 
        {
            try {
                audrec.start();
                recordstarted = 1;
                telManager= (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        final PhoneStateListener phoneListener = new PhoneStateListener()
        {
            @Override
            public void onCallStateChanged(final int state, final String incomingNumber)
            {
                getTelephonyOverview(telManager);
            }
        };

        telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    }
    public void getTelephonyOverview(final TelephonyManager telManager)
    {
        int callState = telManager.getCallState();
        switch (callState)
        {
        case TelephonyManager.CALL_STATE_IDLE:
        {
            if (recordstarted==1)
            {
                try {
                    audrec.stop();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                recordstarted =0;
            }
            break;
        }
        case TelephonyManager.CALL_STATE_OFFHOOK:
        {
            try {
                audrec.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            recordstarted =1;
            break;
        }
        case TelephonyManager.CALL_STATE_RINGING:       
        {
            try {
                audrec.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            recordstarted =1;
            break;
        }
        }
    }   

Another Code that i am trying out that create 3Gp file but not playing

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

}
+1  A: 

Hi ud_an ,

You cannot record calls because the firmware doesn't support it . There is a better answer at xda-devs which I got from android's open issues list :

The voice streams were handled by the baseband processor baseband processor, it's that the baseband firmware aren't setup to offer the streams to the application processor that's limiting the ability to truly record a call. The Android system long has the API implemented, but it can do nothing in this case.

Since the baseband firmware is close sourced and available in binary only, I doubt if the brilliant hackers here can do anything about this.

someone found a "cure" for HD2 , just by editing the registry - xda-developers.com/windows-mobile/two-way-in-call-recording-on-hd2-fixed/wo-way-in-call-recording-on-hd2-fixed/

100rabh
There's also a HTC Hero app that can record calls, but generally this doesn't seem to work on the Android platform as a whole.
David Caunt
there is android apps on market tht records both way but there is some issue there but it works. i tested my app now after several changes recording outgoing call not incoming on HTC G1
ud_an
100rabh
+1  A: 

Call recording is not yet possible on Android. See this feature request.

You can record your voice from microphone, but you can not record the sound of the other party. If you only want to record your voice use android.media.MediaRecorder.AudioSource.MIC

Peter Knego