I am recording an audio file on my broadcast but I am not getting any error but my recorder.stop() is never called heres my code:
String file_name= "recording";
audiofile = File.createTempFile(file_name, ".3gp", sampleDir);
String path=Environment.getExternalStorageDirectory().getAbsolutePath();
//recorder = null;
int audioSource = MediaRecorder.AudioSource.VOICE_CALL;
recorder.setAudioSource(audioSource);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();
and my stop function contains:
public void stopRecord()throws IOException{
if(recorder!=null)
{
recorder.stop();
recorder.release();
recorder.reset();
Log.d(TAG, "recording stopped");
}
else
{
Log.d(TAG, "recording stopped error");
}
i am calling start recorder at offhook stage and stop recorder at idle ,but it goes to idle state and stop is not called at all .
case TelephonyManager.CALL_STATE_IDLE:
prev_state=state;
Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
Log.d(TAG, "recording stopped"); // Executes till here and skips the below part
try {
stopRecord();
Log.d(TAG, "recording stopped");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "recording stopped");
I have give all the permission required pls help me on this
Thanks in Advance
TelephonyManager.EXTRA_STATE_IDLE is called when there is no activity and this is called before TelephonyManager.EXTRA_STATE_OFFHOOK.
Here you are calling stopRecord() function in the TelephonyManager.EXTRA_STATE_IDLE state and thus you are accessing mRecorder.stop();, but at that point Recorder object is null and not initialized. Checdk this post for more clarification Null Exception when recording using mediaRecorder. Also check out the comments.
As far as no error is concerned, you are calling it in try/catch block. So only stack trace will be generated.
Related
Unable to record audio of Incoming and Outgoing phone call in Android
I am using Broadcastreceiver for detecting Phonecalls, It is working fine.
When ever phonecall is started I am using below code for start recording Phonecall and creating a folder of "CALLLOG", in which each call record will be stored.
public void startRecordingStoreFile(){
String out = new SimpleDateFormat("dd-MM-yyyy_hh-mm-ss").format(new Date());
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/CALLLOG");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
String file_name = "Rec_"+out;
try {
audiofile = File.createTempFile(file_name, ".amr", sampleDir);
} catch (IOException e) {
e.printStackTrace();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
recordstarted = true;
}
Below code for stopping the record
public void stopRecording(){
if (recordstarted) {
recorder.stop();
audioManager.setMode(AudioManager.MODE_NORMAL);
recordstarted = false;
}
}
The extension of audio files are ".amr".
Above code is not recording the audio of a phonecall, it is creating a folder of "CALLLOG" and ".amr" files are stored but audio is not recording.I was working on this from 2 days.
For example suppose lets say I am calling to "X" person,
1.MIC is not recording once the "X"(other) person lift the call, until then audio is recording some times,
2.Some times MIC instance is not available as mentioned below solution by Afsar,
I have tried with below code but it doesn't work(Sometimes it works, sometimes not).
I am unable to record audio of Incoming and outgoing calls.Some times it works, sometimes it is not working.
Please help me on this.
Thanks in Advance.
I had the same issue in the past I was trying to record Audio + Video during video call. While device is in call MIC is being used by other processes, so before setting MediaRecorder AudioSource as MIC just check whether MIC instance is available or not. You can test it like that
private boolean validateMicAvailability(){
Boolean available = true;
AudioRecord recorder =
new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_DEFAULT, 44100);
try{
if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
available = false;
}
recorder.startRecording();
if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
recorder.stop();
available = false;
}
recorder.stop();
} finally{
recorder.release();
recorder = null;
}
return available;
}
Simple solution of this problem is to use some CallRecorder Library following is the link.
aykuttasil/CallRecorder check it.
Hey I'm trying to make MediaRecorder record the contents of my screen. It works when I'm making a recording for a first time but when I try to record the screen for a second time it fails. Here is relevant code:
void startRecording(String directory,String filename,MediaProjection mediaProjection) {
this.mediaProjection=mediaProjection;
this.directory=directory;
this.filename=filename;
initRecorder();
prepareRecorder();
virtualDisplay = createVirtualDisplay();
mediaRecorder.start();
}
void stopRecording() {
mediaRecorder.stop();
mediaRecorder.reset();
if (virtualDisplay != null) {
virtualDisplay.release();
}
if (mediaProjection != null) {
mediaProjection.stop();
mediaProjection = null;
}
initRecorder();
prepareRecorder();
}
void setScreen(int screenWidth, int screenHeight, int screenDensity) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.screenDensity = screenDensity;
}
void prepareRecorder() {
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
void initRecorder() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncodingBitRate(512 * 1000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(screenWidth, screenHeight);
mediaRecorder.setOutputFile(directory + "/" + filename + ".mp4");
//mediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath()+"/vitalij.mp4");
}
So in my activity I create a new instance of this class once then after pressing the button the startRecording method get's invoked. Then user can press stop recording which calls stopRecording method. When app is destroyed i release the mediaRecorder object.
This is the error I get:
Caused by: java.lang.IllegalStateException
at android.media.MediaRecorder.setAudioSource(Native Method)
at com.example.xxx.myapplication.VideoRecorder.initRecorder(VideoRecorder.java:77)
at com.example.xxx.myapplication.VideoRecorder.startRecording(VideoRecorder.java:30)
at com.example.xxx.myapplication.MainActivity.onActivityResult(MainActivity.java:134)
I'm sure that I have the correct permissions set and the first video gets created fine. The problem only occurs when making the recording for a second time.
The issue is that you're executing these two lines of code:
initRecorder();
prepareRecorder();
at the end of your stopRecording() function and again in your startRecording() function.
When you try to call mediaRecorder.setAudioSource in initRecorder() after the audio source has already been set, you get an IllegalStateException because it's in the incorrect state.
If you look at the state diagram on the Android MediaRecorder reference page, you'll see that a MediaRecorder must be in the Initial state to call setAudioSource(), but yours is in the Prepared state after stopRecording() has been called and you try to call setAudioSource() again.
I want to create application in android which will block call recording.if someone has secretly install call recording app in my phone like virus or something then this app will restrict/prevent all call recording.
so my question is
Is there any way to block call recording?
Thank you in advance.
I think its possible!!! If one android device is running with two different call recording apps, the call will recorded by first app only, which will use the call recording resources in that device, rest of all apps will fail to record, because the resources can be used by one app at a time, its a computation, who will trigger to start using resources will gonna win..it can be your app!!
I'm just giving Idea only, not the perfect solution!!!
Sample code(Not Full code):
MediaRecorder recorder = new MediaRecorder();
Log.d(TAG, "RecordService will config MediaRecorder with audiosource: " + audiosource + " audioformat: " + audioformat);
try {
// These calls will throw exceptions unless you set the
// android.permission.RECORD_AUDIO permission for your app
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Log.d(TAG, "set encoder default");
recorder.setOutputFile(recording.getAbsolutePath());
Log.d(TAG, "set file: " + recording.getAbsolutePath());
//recorder.setMaxDuration(msDuration); //1000); // 1 seconds
//recorder.setMaxFileSize(bytesMax); //1024*1024); // 1KB
recorder.setOnInfoListener(this);
recorder.setOnErrorListener(this);
try {
recorder.prepare();
} catch (java.io.IOException e) {
Log.e(TAG, "RecordService::onStart() IOException attempting recorder.prepare()\n");
Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to start recording: " + e, Toast.LENGTH_LONG);
t.show();
recorder = null;
return; //return 0; //START_STICKY;
}
Log.d(TAG, "recorder.prepare() returned");
recorder.start();
isRecording = true;
Log.i(TAG, "recorder.start() returned");
//updateNotification(true);
} catch (java.lang.Exception e) {
Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to start recording: " + e, Toast.LENGTH_LONG);
t.show();
Log.e(TAG, "RecordService::onStart caught unexpected exception", e);
recorder = null;
}
I am trying to record the call when phone call is accepted in android.
For that I have created a Receiver, in that I have kept code for MediaRecorder initial setup like this..
onReceive(){
recorder = new MediaRecorder();
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/myrecording"+System.currentTimeMillis()+".3gp";
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputFile);
}
and in the phoneState change listener, for the case OFF_HOOK
case TelephonyManager.CALL_STATE_OFFHOOK: {
Log.v(TAG,"call state is OffHook");
try{
recorder.prepare();
Thread.sleep(1000);
recorder.start();
} catch(Exception e) {
Log.e(TAG,"Error occured here");
}
break;
}
every time when the control comes into this state I am getting error saying that
E/MediaRecorder(23056): start failed: -2147483648
W/System.err(23056): java.lang.RuntimeException: start failed.
I tried this but it is also not working, could you please help me in this.Thanks
I got this issue and cant get my head around it, im recording calls directly from the speaker, so when i get TelephonyManager.CALL_STATE_OFFHOOK I instantly start recording audio from VOICE_CALL. thats part its ok, start recording but if the call is ended and start a new one, I get a java.lang.IllegalStateException
I think this is because the first call it's still being recorded... I've tried to do:
mRecorder.stop();
mRecorder.release();
mRecorder.reset();
but no luck, they all gave me a illegalStateException, I just want to know how to stop first call recording and record a new one without errors.
Here's my code for recording and call handling,
//At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
if (state == TelephonyManager.CALL_STATE_OFFHOOK){
if (record_calls == 1){
record_enviroment();
}
}
//when Idle = No activity.
if (state == TelephonyManager.CALL_STATE_IDLE){
//Check for audio recorded and if exists post audio to server
}
public void record_enviroment(){
path = context.getFilesDir().getAbsolutePath() + "/";
try {
//Random number for file name
Random r = new Random( System.currentTimeMillis() );
i = 10000 + r.nextInt(20000);
// Save file local to app
mFileName = path + i + "_call_" + id_asociado + ".3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("AUDIO_RECORDER", "prepare() failed");
}
mRecorder.start();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}