我在应用程序内部构建了一项服务,当从应用程序内部进行呼叫时,该服务允许进行语音记录,但该应用程序仅记录我的声音,而完全忽略了接收方的声音。
过程:
代码
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;
}
}
答案 0 :(得分:0)
截止到现在,您的录音机的音频源为MIC;仅记录从MIC输入的音频。
您需要更改音频源
MIC
→MediaRecorder.AudioSource.VOICE_COMMUNICATION
rec.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
答案 1 :(得分:0)
我不确定是否可行。
您是否检查过图书馆? 检查CallRecorder