当呼叫挂断时,Android Call Recording不会停止

时间:2015-05-12 07:02:55

标签: android audio-recording telephony android-mediarecorder

    public class CallReceiver extends BroadcastReceiver {

        MediaRecorder callrecorder;
        Boolean recording = false;
        private static boolean ring = false;
        private static boolean out = false;
        private static boolean callReceived = false;
        String outNumber;
        Location location;

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
                outNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                Log.d("outNumber", outNumber);
            }

            //Check if Checked In
            SharedPreferences prefs = context.getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
            String check = prefs.getString("isCheckedIn", null);
            if (check.equals("Yes")) {

                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Class c = Class.forName(tm.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    //Get incoming number and phone state
                    Bundle bundle = intent.getExtras();
                    Bundle extras = intent.getExtras();
                    String state = extras.getString(TelephonyManager.EXTRA_STATE);
                    String phoneNumber = bundle.getString("incoming_number");
``                    Log.d("State", state);
                    // Get location
                    try {
                        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //Start switch case to switch between Phone States
                    switch (state) {
                        //If Phone is idle
                        case "IDLE":
                            Toast.makeText(context, state + "", Toast.LENGTH_SHORT).show();
                            //Stop recording
                            if (recording) {
                                stopRecording();
                                Toast.makeText(context, "stopped", Toast.LENGTH_SHORT).show();
                            }

                            break;
                        //If Phone is Answered
                        case "OFFHOOK":
                            out = true;
                            callReceived = true;
                            PreferencesHelper Prefs = new PreferencesHelper(context.getApplicationContext());
                            String checked = Prefs.GetPreferences("isCheckedIn");
                            if (checked.equals("Yes")) {
                                //Start Recording
                                if (!recording) {
                                    startRecording();
                                    Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();
                                }
                         ring = false;
                            }
                            break;
                        //If Phone is Ringing
                        case "RINGING":
                            Toast.makeText(context, state + "" + phoneNumber, Toast.LENGTH_LONG).show();
                            ring = true;
                            callReceived = false;
                            break;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }//End onReceive

        void startRecording() {

            String fname = "/sales/rec" + System.currentTimeMillis() / 1000 + ".amr";
            callrecorder = new MediaRecorder();
            callrecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
            callrecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
            callrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            callrecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + fname);

            try {
                callrecorder.prepare();
                callrecorder.start();
                recording = true;
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        void stopRecording() {

            callrecorder.stop();
            callrecorder.reset();
            callrecorder.release();
            recording = false;
        }
    }

这是我的代码和通话录音很好,但问题是录音在断开连接时没有停止,这也是记录通话的最佳做法,还是有其他稳定/安全的方式来做相同?

0 个答案:

没有答案