在Android上录制电话

时间:2010-10-14 04:01:19

标签: android

我正在尝试使用麦克风编写此代码而不能正常工作的记录传出呼叫,我测试了简单音频记录的代码它工作正常,我不确定何时启动媒体记录我推迟启动广播接收器可能是问题在那里。

在这里,Audiorecoder是我已经实现了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;
        }
        }
    }   

我正在尝试创建3Gp文件而不是播放的另一个代码

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

}

2 个答案:

答案 0 :(得分:3)

您无法录制通话,因为固件不支持。 xda-devs有一个更好的答案,我从android的开放问题列表中得到了:

  

语音流由基带处理器处理   基带处理器就是这样的   基带固件未设置为   为应用程序提供流   处理器限制了这种能力   真正记录一个电话。 Android   system long实现了API,   但在这种情况下它无能为力。

     

由于基带固件已关闭   仅以二进制方式获取和提供,   我怀疑这里是不是那些出色的黑客   可以做任何事情。

     有人为HD2找到了“治愈”,只是   通过编辑注册表 -   xda-developers.com/windows-mobile/two-way-in-call-recording-on-hd2-fixed/wo-way-in-call-recording-on-hd2-fixed /

答案 1 :(得分:2)

Android上无法进行通话录音。请参阅此feature request

您可以从麦克风录制语音,但无法录制对方的声音。如果你只想录制你的语音,请使用android.media.MediaRecorder.AudioSource.MIC