I have found many question about this issue but i am not clear at all. I want to develop and app so that it can record any outgoing call and Incoming call. I wrote this code below but its not working. Please help details explanation would be preferred.Thanks in advance.
import java.io.File;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class serviceBroadcast extends BroadcastReceiver {
Context mycontext;
final MediaRecorder recorder = new MediaRecorder();
public void onReceive(Context context, Intent intent) {
mycontext=context;
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
TeleListener PhoneListener = new TeleListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
StopRecording();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
startRecording();
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
default:
break;
}
}
}
public void startRecording(){
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory()+ "/RecordMyVoice.mp4");
try {
recorder.prepare();
Log.i(this.getClass().getName(), "Prepared");
} catch (IllegalStateException e) {
e.printStackTrace();
Log.i(this.getClass().getName(), "ERR 1");
} catch (IOException e) {
e.printStackTrace();
Log.i(this.getClass().getName(), "ERR 2");
}
recorder.start();
}
public void StopRecording(){
recorder.stop();
recorder.release();
Log.i(this.getClass().getName(), "Stop Recording");
}
}
}
Maybe this answer doesn´t fix Your problem, but it´s to long for a comment. Without seeing Logcat it´s hard to say, why You get an exception, but one thing I see is, that it maybe could give errors of Your release() call. So, if You call release() on a MediaRecorder object, it is in the null-state and can not used after that. So the problem is, You called
final MediaRecorder recorder = new MediaRecorder();
at the beginning. It would be a better practise to make the MediaRecorder Global
private MediaRecorder recorder=null;
and create the Object in Your startRecording() method:
private void startRecording(){
recorder = new MediaRecorder();
.
.
.
}
Also, don´t forget to set permissions in manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
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.