Запись звонков Android
Я делаю заявление, в котором я записываю звонки. Все сделано успешно, кроме голоса другой стороны. Я записываю звонки, используя микрофон в качестве источника звука. Наиболее загруженное приложение в магазине Play, также использующее MIC в качестве источника. Я не знаю, что я делаю не так. Я не могу записать голос другого человека
Ниже код
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.brillinfosystem.voicerecorder.UploadService.recorder;
/**
* Created by DELLANAND on 21/11/2015.
*/
public class SaveFile {
// MediaRecorder recorder = TService.recorder;
File audiofile;
String audio_format;
/* public SaveFile(MediaRecorder recorder)
{
this.recorder=recorder;
}*/
public File startRecording(String method, String filename, String state, Context context) {
Log.d("recording Started", "Yo I have been called");
String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date());
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/SalesLineCallRecordings");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
String file_name = filename;
try {
// audiofile = File.createTempFile(filename, ".amr", sampleDir);
audiofile = new File(sampleDir, file_name + "-" + out + state + ".aac");
} catch (Exception e) {
e.printStackTrace();
Log.d("save exception", "error" + e);
}
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
// recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
/*
try {
MediaRecorder r = new MediaRecorder();
r.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
r.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
r.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
r.setOutputFile(audiofile.getAbsolutePath());
r.setAudioSamplingRate(96000);
r.start();
r.stop();
r.reset();
r.release();
r = null;
TService.recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
catch (Exception e)
{
Log.d("State" , "Your phone does not support VOICE_CALL");
MediaRecorder r = new MediaRecorder();
r.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
r.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
r.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
r.setOutputFile(audiofile.getAbsolutePath());
try {
r.prepare();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
r.start();
r.stop();
r.reset();
r = null;
TService.recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
}
*/
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0);
// TService.recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// TService.recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//TService.recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//TService.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.d("Problem", "recoder.prepare has problem");
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
return audiofile;
}
public void stopRecording() {
recorder.stop();
}
}
Класс приемника вещания
public class TService extends BroadcastReceiver {
File audiofile;
String name, phonenumber;
String audio_format;
public String Audio_Type;
int audioSource;
Context context;
private static String type;
private Handler handler;
Timer timer;
AudioDbHelper audioDbHelper;
Audio audio;
Boolean offHook = false, ringing = false;
Toast toast;
Boolean isOffHook = false;
static private boolean recordstarted = false;
private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL";
public SaveFile SaveRecording= new SaveFile();
static File audioFile;
//Handler updateHandler = new Handler();
Bundle bundle;
String state;
String outCall;
public static boolean wasRinging = false;
public static boolean setUpRecording = false;
@Override
public void onReceive(final Context context, Intent intent) {
audio = new Audio();
audioDbHelper = new AudioDbHelper(context);
SharedPreferences settings =context.getSharedPreferences("AUDIO_SOURCE", 0);
SharedPreferences.Editor editor = settings.edit();
this.context=context;
if (intent.getAction().equals(ACTION_IN)) {
if ((bundle = intent.getExtras()) != null) {
state = bundle.getString(TelephonyManager.EXTRA_STATE);
// Toast.makeText(context, "state changed : " + state, Toast.LENGTH_LONG).show();
Log.d("State ", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
editor.putString("inCall", inCall);
editor.commit();
wasRinging = true;
setUpRecording = true;
//Toast.makeText(context, "INcoming : " + inCall, Toast.LENGTH_LONG).show();
Log.d("Incoming Call", " from : " + inCall + "wasRinging : " + wasRinging);
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
String inCall = settings.getString("inCall", "");
Log.d("message", " Oh ! he picked the call" + inCall);
if (wasRinging == true) {
Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show();
Log.d("Answered the call", "from :" + inCall);
if (setUpRecording) {
recordstarted = true;
setUpRecording = false;
// SaveRecording.startRecording();
try {
String method = settings.getString("AUDIO_SOURCE", "");
Log.d("message", method + " : " + inCall);
audioFile=SaveRecording.startRecording(method, inCall, "-In" , context);
Log.d("recording Stared","File name" + audioFile.getAbsolutePath().toString());
} catch (Exception e) {
Log.d("Recording Exception1 : ", e.toString());
}
}
}
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
wasRinging = false;
Toast.makeText(context, "REJECTED", Toast.LENGTH_LONG).show();
if (recordstarted) {
// recorder.stop();
Log.d("Message", "Stopping recording");
SaveRecording.stopRecording();
// saverecordings.stopRecording();
Thread xx= new Thread(){
@Override
public void run() {
try {
Log.d("Thread" , "initiated" + audioFile.getAbsolutePath().toString());
sleep(1000);
ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
//NetworkInfo wifi = connMgr;
if(networkInfo != null && networkInfo.isConnected())//
{
{
type = "yes";
Log.e("stopped","");
// uploadActivity(true, audioFile.getAbsolutePath());
}
}
else
{
// uploadActivity(false, audioFile.getAbsolutePath());
}
Log.d("Thread" , "Executed");
}
catch (Exception e){
Log.d("thread Exception" , e.toString());
}
}
};
xx.start();
//SaveFile.recorder.stop();
// updateHandler.postDelayed(timerRunnable, 1000);
recordstarted = false;
}
}
}
} else if (intent.getAction().equals(ACTION_OUT)) {
if ((bundle = intent.getExtras()) != null) {
outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show();
// saverecordings.startRecording();
try {
String method = settings.getString("AUDIO_SOURCE", "");
Log.d("message", method);
audioFile=SaveRecording.startRecording(method, outCall, "-Out", context);
Log.d("recording Stared","File name" + audioFile.getAbsolutePath().toString());
} catch (Exception e) {
Log.d("Recording Exception2", e.toString());
}
recordstarted = true;
setUpRecording = false;
}
}
}
private void uploadActivity(boolean isConnected, String fileName) {
if (isConnected) {
Log.e("", "File path paras" + fileName);
//new UploadAsync().execute(mFilePath);
type = "yes";
Intent intent = new Intent(context, UploadService.class);
intent.putExtra("type", type);
intent.putExtra("audioFile", fileName);
context.startService(intent);
} else {
audio.setAudioRecording(mFilePath);
audioDbHelper.addAudio(audio);
}
}
}