加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

android 通话录音功能

发布时间:2020-12-15 03:17:19 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 package com.newland.teleinterface; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.util.Calend

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    package com.newland.teleinterface;  
      
    import java.io.File;  
    import java.io.IOException;  
    import java.lang.reflect.Method;  
    import java.util.Calendar;  
      
    import com.android.internal.telephony.ITelephony;  
      
    import android.content.Context;  
    import android.content.Intent;  
    import android.media.MediaRecorder;  
    import android.net.Uri;  
    import android.os.Environment;  
    import android.os.Handler;  
    import android.os.Message;  
    import android.telephony.PhoneStateListener;  
    import android.telephony.TelephonyManager;  
    import android.util.Log;  
    import android.widget.Toast;  
      
    /** 
     *  
     * @author JD 功能:打电话,录音,通话时间 
     *  
     */  
    public class TeleInterface {  
      
      private String TAG = "TeleInterface";  
      private Context activity;  
      private Handler handler;  
      private Calendar calendar;  
      private String teleStartTime;  
      private String teleEndTime;  
      private TelephonyManager telephonyManager;  
      public static int TELE_START_TIME = 5;  
      public static int TELE_END_TIME = 6;  
      
      public String getTeleStartTime() {  
        return teleStartTime;  
      }  
      
      public String getTeleEndTime() {  
        return teleEndTime;  
      }  
      
      /** 
       * 构造函数 
       *  
       * @param activity 
       * @param handler 自定义handler接收消息 msg.what 5:电话拨通时间 6:电话挂断时间 
       */  
      public TeleInterface(Context activity,Handler handler) {  
        this.activity = activity;  
        this.handler = handler;  
      }  
      
      /** 
       * 拨打电话 
       *  
       * @param phoneNum 需要拨打号码 
       */  
      public void Call(String phoneNum) {  
        if (phoneNum.length() != 0) {  
          Intent phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + phoneNum));  
          activity.startActivity(phoneIntent);  
        } else {  
          Toast.makeText(activity,"不能输入为空",Toast.LENGTH_LONG).show();  
        }  
      }  
      
      /** 
       * 来电监听注册 
       */  
      public void teleListen() {  
        telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);// 注册监听器  
      
        telephonyManager.listen(new PhoneListener(),PhoneStateListener.LISTEN_CALL_STATE);// 监听电话状态  
      }  
      
      /** 
       * 挂断电话 
       *  
       * @throws Exception 
       */  
      public void endCall() throws Exception {  
        ITelephony iTelephony = PhoneUtils.getITelephony(telephonyManager);  
        iTelephony.endCall();// 自动挂断电话  
      }  
      
      
      private final class PhoneListener extends PhoneStateListener {  
        private String incomeNumber; // 来电号码  
        private MediaRecorder mediaRecorder;  
        private File root_file,file;  
      
        @Override  
        public void onCallStateChanged(int state,String incomingNumber) {  
          try {  
            switch (state) {  
              case TelephonyManager.CALL_STATE_RINGING: // 来电  
                this.incomeNumber = incomingNumber;  
                break;  
              case TelephonyManager.CALL_STATE_OFFHOOK: // 接通电话  
                calendar = Calendar.getInstance();  
                teleStartTime = calendar.getTime().toString();  
      
                String root_directory = Environment.getExternalStorageDirectory() + "/recorded_calls";  
                root_file = new File(root_directory);  
                if (!root_file.exists()) {  
                  root_file.mkdir();  
                }  
                if(this.incomeNumber==null){  
                    this.incomeNumber = "call";  
                }  
                String record_call =  
                    root_directory + "/" + this.incomeNumber + "_" + System.currentTimeMillis()  
                        + ".amr";  
                file = new File(record_call);  
                if (!file.exists()) {  
                  file.createNewFile();  
                }  
      
                mediaRecorder = new MediaRecorder();  
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); // 获得声音数据源  
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // 格式输出  
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  
                mediaRecorder.setOutputFile(file.getAbsolutePath()); // 输出文件  
                mediaRecorder.prepare(); // 准备  
      
                mediaRecorder.start();  
      
                Log.d(TAG,"开始录音!");  
      
                Message msg_start = new Message();  
                msg_start.what = TELE_START_TIME;  
                msg_start.obj = teleStartTime;  
      
                Log.d(TAG,"StartTime=====" + teleStartTime);  
      
                handler.sendMessage(msg_start);  
                break;  
      
              case TelephonyManager.CALL_STATE_IDLE: // 挂掉电话  
                if (mediaRecorder != null) {  
                  mediaRecorder.stop();  
                  mediaRecorder.release();  
                  mediaRecorder = null;  
      
                  calendar = Calendar.getInstance();  
                  teleEndTime = calendar.getTime().toString();  
                  Message msg_end = new Message();  
                  msg_end.what = TELE_END_TIME;  
                  msg_end.obj = teleEndTime;  
      
                  Log.d(TAG,"EndTime=====" + teleEndTime);  
      
                  handler.sendMessage(msg_end);  
                }  
                Log.d(TAG,"结束录音!");  
                break;  
      
            }  
          } catch (IllegalStateException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
          } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
          }  
        }  
      }   
    }  

  1. 二、activity使用?
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
  
public class MainActivity extends Activity implements OnClickListener {  
  
  private TextView teleStartTime,teleEndTime;  
  private Button teleButton;  
  private TeleInterface teleInterface;  
  private TeleHandler teleHandler;  
  private EditText telNo;  
  
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    initView();  
    teleHandler = new TeleHandler(this,teleStartTime,teleEndTime);  
    teleInterface = new TeleInterface(this,teleHandler);  
    teleInterface.teleListen();  
  }  
  
  
  private void initView() {  
    teleStartTime = (TextView) findViewById(R.id.teleStartTime);  
    teleEndTime = (TextView) findViewById(R.id.teleEndTime);  
    teleButton = (Button) findViewById(R.id.teleButton);  
    telNo = (EditText) findViewById(R.id.telno);  
    teleButton.setOnClickListener(this);  
  }  
  
  
  @Override  
  public boolean onCreateOptionsMenu(Menu menu) {  
    // Inflate the menu; this adds items to the action bar if it is present.  
    getMenuInflater().inflate(R.menu.main,menu);  
    return true;  
  }  
  
  
  @Override  
  public void onClick(View v) {  
    switch (v.getId()) {  
      case R.id.teleButton:  
            if(!telNo.getText().toString().trim().equals("")){  
                 teleInterface.Call(telNo.getText().toString().trim());  
            }else{  
                 teleInterface.Call("10010");  
            }  
             
              
            try {  
//              Thread.sleep(5000);  
//              teleInterface.sendDTMF("1".toCharArray()[0]);  
              teleInterface.endCall();  
            } catch (Exception e) {  
              // TODO Auto-generated catch block  
              e.printStackTrace();  
            }  
            break;  
  
      default:  
        break;  
    }  
  }  
  
}  

三、布局配置文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:orientation="vertical"  
    tools:context=".MainActivity" >  
      
    <EditText   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/telno"  
        android:numeric="integer"  
        android:hint="电话号码"/>  
      
    <Button  
        android:id="@+id/teleButton"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="拨号" />  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="通话时间:" />  
  
    <TextView  
        android:id="@+id/teleStartTime"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" />  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="结束时间:" />  
  
    <TextView  
        android:id="@+id/teleEndTime"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" />  
  
</LinearLayout>

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读