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:
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
MIC
→MediaRecorder.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.