Hitesh
Hitesh

Reputation: 51

How to record a call in android?

Recorder = new MediaRecorder();     
Recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
Recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); 
Recorder.setOutputFile(FileName); 
Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

this is my Code and i am not able to record the voice of other person.so what should i use instead of AudioSorce.MIC. plz help me

Upvotes: 1

Views: 3407

Answers (6)

ifeegoo
ifeegoo

Reputation: 7292

As I known and I have finshed this kind of job.I mean both the user's voice and the other people's voice.We used the customized ROM of one phone provided by the specificated factory and they also have modificated some security mechanism of the ROM.Another way you can try this:

https://github.com/FR13NDS/call-recorder-for-android

Upvotes: 1

Ravdeep Sohi
Ravdeep Sohi

Reputation: 11

Try This code to record call.

AudioManager audioManager;
private MediaRecorder myAudioRecorder;
//Myservice m=new Myservice();
   private String outputFile = null;
   String uid=null;
   String no=null;
   String rname=null;

public void record(Context c,String no, String ser){
    try{
    this.no=no;
    uid=ser;
    rname=String.format("%d.mp3", System.currentTimeMillis());
    //rname="/#"+uid+"#"+rname;
     outputFile = Environment.getExternalStorageDirectory().toString() + "/Android/free"+"/#"+uid+"#"+no+"#"+rname;
      StartAudioSpeacker();
      myAudioRecorder=new MediaRecorder();
      myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
      myAudioRecorder.setOutputFile(outputFile);
      }catch(IllegalStateException e){e.printStackTrace();}
        try {
            myAudioRecorder.prepare();
             myAudioRecorder.start();
        } catch (IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


 }
 public void getc(Context c) {
    // TODO Auto-generated method stub
     this.c=c;

}
public void st()
 { //Toast.makeText(;, "recorded",Toast.LENGTH_LONG).show();
    try{
     myAudioRecorder.stop();
     myAudioRecorder.release();
     myAudioRecorder=null;
    }
    catch(IllegalStateException e)
    {
        e.printStackTrace(); 
    }
     Handler h=new Handler();
     h.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{
            uprecord ur=new uprecord("/#"+uid+"#"+no+"#"+rname);
             ur.tt();
            }catch(IllegalStateException e){e.printStackTrace();}   }
    }, 1000);

 }

 public void StartAudioSpeacker(){



         audioManager = (AudioManager) c.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());
 }

}

Upvotes: 0

Ashok Domadiya
Ashok Domadiya

Reputation: 1122

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

Upvotes: 3

Manikandan
Manikandan

Reputation: 1519

If you are trying to store the audio file to sdcard, try this code. It will work fine.

 protected void startRecording() {

    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }
    mRecorder.start();        
}
}

Upvotes: -1

Imran
Imran

Reputation: 3024

I didnt try it on Phone, but according doc MediaRecorder.AudioSource you should use

MediaRecorder.AudioSource.VOICE_CALL instead of MediaRecorder.AudioSource.MIC

Hopes that works

Upvotes: -1

gby
gby

Reputation: 15218

Sorry to disappoint you, but you can't, not in the general case. Most Android running phones have the wiring in hardware/firmware so that the media of the call does not pass through the application processor at all - it goes from the audio to the DSP and vice versa, so you cannot access it.

You can catch the audio of the person using the phone, but not the other way around, disregarding silly hacks like asking the person to use the speakers and recording the sound from there via the phone mic...

Upvotes: 4

Related Questions