简体   繁体   中英

Phone call recording service is recording the caller's voice only. (Android)

I built a service inside an app which allows voice recording when calling from within the app, but the app is recording my voice only, completely ignoring the receiver's voice.

The process:

  1. I click on a toggle to turn on phone call recording
  2. Then there is a "Call" button
  3. I click on the "Call" button
  4. The app takes me to the default phone app
  5. I talk to the other person and the other person talks to me
  6. Phone call is finished and we hang up
  7. I go to the recording and play it
  8. Only my voice is recorded

Code

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.format.DateFormat;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class RecordingService extends Service {
private MediaRecorder rec;
private boolean recordstarted;
private File file;
String path="/sdcard/alarms/";
@androidx.annotation.Nullable
@Override
public IBinder onBind(Intent intent) {

    return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
   // return super.onStartCommand(intent, flags, startId);

    file= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS);

    Date date=new Date();
    CharSequence sdf= DateFormat.format("MM-dd-yy-mm-ss",date.getTime());
    rec=new MediaRecorder();

    rec.reset();

    rec.setAudioSource(MediaRecorder.AudioSource.MIC);
    rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    rec.setOutputFile(file.getAbsolutePath()+"/"+sdf+"rec.3gp");



    TelephonyManager manager=(TelephonyManager)getApplicationContext().getSystemService(getApplicationContext().TELEPHONY_SERVICE);
    manager.listen(new PhoneStateListener(){
        @Override
        public void onCallStateChanged(int state, String phoneNumber) {
           // super.onCallStateChanged(state, phoneNumber){

                if (TelephonyManager.CALL_STATE_IDLE == state && rec ==null) {
                    rec.start();
                    rec.reset();
                    rec.release();
                    recordstarted = false;
                    stopSelf();
                }else if (TelephonyManager.CALL_STATE_OFFHOOK==state){

                    try {
                        rec.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    rec.start();
                    recordstarted=true;

                }
                }


        },PhoneStateListener.LISTEN_CALL_STATE
    );
    return START_STICKY;
}
}

As of now your recorder has audio source as MIC; which will only record audio inputted from MIC.

What you need is to change audio source

MICMediaRecorder.AudioSource.VOICE_COMMUNICATION

rec.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)

I'm not sure that works.

have you checked for libraries? check CallRecorder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM