3

我正在使用以下代码在 android 手机中记录来电和去电对话,但此代码仅在麦克风或扬声器打开且用户禁用扬声器电话时有效,我的代码将无法记录来电。
我想在不激活电话扬声器的情况下记录来电和去电通话。

    class MyPhoneStateListener extends PhoneStateListener implements SensorEventListener {
            Context context;
            AudioManager audioManager;
            MediaRecorder recorder;
            private SensorManager mSensorManager;
            private Sensor myLightSensor;
            private boolean CallState;
            private float sensorState;

            public MyPhoneStateListener(Context context) {
                this.context = context;
                mSensorManager = (SensorManager) this.context.getSystemService(Context.SENSOR_SERVICE);
                myLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
                audioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
                if (myLightSensor == null){
                    Log.i("On Receive", "Not Support");
                }else{
                    mSensorManager.registerListener(this,myLightSensor,SensorManager.SENSOR_DELAY_NORMAL);
                }
            }

            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    System.out.println("My Call IDLE");
                    CallState = false;
                    StartAudioSpeacker();
                    StopRecording();
                    System.out.println("Is phone speaker : "+ audioManager.isSpeakerphoneOn());
                    if (audioManager.isSpeakerphoneOn()) {
                        audioManager.setSpeakerphoneOn(false);
                        mSensorManager.unregisterListener(this);
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    System.out.println("My Call OFFHOOK");
                    CallState = true;
                    StartAudioSpeacker();
                    StartRecording();
                    System.out.println("Is phone speaker : "+ audioManager.isSpeakerphoneOn());
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    System.out.println("My Call RINGING");
                    break;
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                if (sensor.getType() == Sensor.TYPE_PROXIMITY) {
                    Log.i("Sensor Changed", "Accuracy :" + accuracy);
                }
            }

            public void onSensorChanged(SensorEvent event) {
                if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
                    Log.i("Sensor Changed", "onSensor Change :" + event.values[0]);
                    sensorState = event.values[0];
                    StartAudioSpeacker();
                }
            }

            public void StartAudioSpeacker(){
                if (CallState && sensorState == 1.0) {
                    audioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
                    audioManager.setSpeakerphoneOn(true);
                    audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
                    audioManager.setStreamVolume(AudioManager.MODE_IN_CALL, audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL), 1);
                    System.out.println("Is phone speaker : "+ audioManager.isSpeakerphoneOn());
                }else{
                    audioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
                    audioManager.setSpeakerphoneOn(false);
                    audioManager.setStreamVolume(AudioManager.MODE_IN_CALL, audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL), 1);
                    System.out.println("Speaker Volume :"+ audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL));
                    System.out.println("Is phone speaker : "+ audioManager.isSpeakerphoneOn());
                }
            }
            public void StartRecording(){
                 recorder = new MediaRecorder();
                 recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                 recorder.setOutputFile(this.getFullSdPath());
                 try {
                    recorder.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                recorder.start();   // Recording is now started
                Log.i(this.getClass().getName(), "Start Recording");
            }

            public void StopRecording(){
                 recorder.stop();
                 recorder.reset();   
                 recorder.release();
                 Log.i(this.getClass().getName(), "Stop Recording");
            }

            public String getFullSdPath(){
                File sdCard = new File(Environment.getExternalStorageDirectory()    + "/RecordMyVoice");
                if (!sdCard.exists()) {
                    sdCard.mkdir();
                }
                File file = new File(Environment.getExternalStorageDirectory()+"/RecordMyVoice/",new Date().getTime()+".3gp");
                System.out.println("Full path of record sound is : "+file.getAbsolutePath());
                return file.getAbsolutePath();
            }
        }
4

1 回答 1

3

我想在不激活电话扬声器的情况下记录来电和去电通话。

抱歉,Android 通常不支持此功能。

于 2012-09-18T12:43:23.810 回答