1. 程式人生 > >Android 通話錄音功能實現

Android 通話錄音功能實現

  在Android平臺實現實現通話錄音功能

  1.作為後臺執行的程式需要使用service去實現,在錄音的時候雨IO操作需要啟動新執行緒

  2.使用平臺的MediaRecorder類錄音

  3.用PhoneStateListener監聽電話的狀態

  4.新增相應的許可權

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

具體程式碼如下:

package com.mobi.app;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class CallRecorderService extends Service {
    private static final String TAG = "CallRecorderService";

    private ServiceHandler mServiceHandler;
    private Looper mServiceLooper;
    private TelephonyManager mTelmgr;

    public Handler mToastHandler = new Handler();

    public static final String ACTION_LISTEN_CALL_STATE =
            "com.mobi.bootttest.ACTION_LISTEN_CALL_STATE";

    public void onCreate() {
        HandlerThread thread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
        thread.start();
        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        mServiceHandler.sendMessage(msg);
        return Service.START_STICKY;
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onDestory() {
        mServiceLooper.quit();
    }

    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        public void handleMessage(Message msg) {
            Intent intent = (Intent) msg.obj;
            if (intent != null) {
                if(ACTION_LISTEN_CALL_STATE.equals(intent.getAction())) {
                    PhoneListener mPhoneListener = new PhoneListener(getApplicationContext());
                    if(null == mPhoneListener){
                        mPhoneListener = new PhoneListener(getApplicationContext());
                    }
                    mTelmgr =(TelephonyManager)getApplicationContext().getSystemService(
                            Context.TELEPHONY_SERVICE);
                    mTelmgr.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
                }

            }
        }
    }

    private class PhoneListener extends PhoneStateListener {
        private Context mContext;
        private String number;
        private MediaRecorder mRecoder;
        public  PhoneListener(Context context){
            mContext = context;
        }
        public void onCallStateChanged(int state,String incomingNumber){
            Log.d(TAG,"The state of call is "+String.valueOf(state));
            switch (state){
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.d(TAG,"The state is idle");
                    if(null == mRecoder){
                        return;
                    }
                    mRecoder.stop();
                    mRecoder.release();
                    Log.d(TAG,"The state is done");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d(TAG,"The state is offhook");
                    mRecoder = new MediaRecorder();
                    mRecoder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mRecoder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                    mRecoder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                    File file = new File(Environment.getExternalStorageDirectory().getPath(),"XXXX");
                    if(!file.exists()){
                        file.mkdirs();
                    }
                    mRecoder.setOutputFile(file.getAbsolutePath()+File.separator+number+".amr");
                    try{
                        mRecoder.prepare();
                        mRecoder.start();
                        Log.d(TAG,"start recording");
                    }catch (IOException e){
                        Log.d(TAG,"Recording fails");
                        mRecoder.reset();
                        mRecoder.release();
                        mRecoder = null;
                    }
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    if(incomingNumber!=null){
                        number = incomingNumber;
                    }else{
                        number = "unknown";
                    }
                    Log.d(TAG,"The state is ringing "+incomingNumber);
                    break;
                default:
                    break;
            }
        }
    }
}