MediaRecorder start is been failing - android

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

Related

Unable to record audio of Incoming and Outgoing phone call in Android

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.

block call recording in android

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;
}

Media Recorder stop() is never called

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.

Trying to record voice in android but getting error

I am trying to record voice in android emulator (android 4.1).
But I when I press the button to start recording then it immediately return from the function and executes next line that logs a message saying "Recorded".
Then, I press the stop button and in Logcat message appears that media recorder went away with unhandled events.
If I again press the button to start recording then I get an error message saying FATAL SIGNAL 11. And I dont know how to access the SD card to see if the file is created or not.
Below is the logCat code that I used from a tutorial:
}
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
try{
recorder.prepare();
}
catch(IOException e){
Log.e("Recorder","Recording failed");
}
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
The logCat is:
08-08 16:09:39.713: D/gralloc_goldfish(743): Emulator without GPU emulation detected.
08-08 16:09:42.674: D/Recorder(743): Recorded
08-08 16:09:48.764: W/MediaRecorder(743): mediarecorder went away with unhandled events
08-08 16:13:01.613: A/libc(743): Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 743 (xample.recorder)
I solved this problem by resting recorder before releasing it.
recorder.stop();//stop recording
recorder.reset();
recorder.release();
Each time create a new instance of recorder and you wont get any error.
recorder.stop();
recorder.release();
recorder=null;
And let the following be the first line in your startRecording() method-
recorder=new MediaRecorder();
For more info-http://developer.android.com/guide/topics/media/audio-capture.html

android: record audio with MediaPlayer on emulator

I am trying to record audio from the microphone on the Android emulator with this code:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/test/test.3gp");
try {
recorder.prepare();
}
catch (IOException io) {
Log.v(LOG_TAG, "Could not prepare the audio " + io.getMessage());
}
recorder.start();
For stopping the audio, this is the code:
recorder.stop();
recorder.reset();
recorder.release();
The recording process works fine but the resulting audio that is distorted. When I record an audio for 60 seconds duration and play it, it's duration is being shown as 120 seconds. The measurement is not exact but the this is just to give you an idea.
Only the AMR_NB encoder is working on my emulator. I have tried different output formats but the result is always the same.
Is it a limitation of the emulator or am I doing something wrong here?
Edit 1:
I have tried the AudioRecord class too and the result is the same dragging audio.
Thanks.
I have been working for the same and found the solution, Try using the following code:
private void startRecording()
{
this.recorder = new MediaRecorder();
this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
MediaRecorder.getAudioSourceMax();
this.recorder.setOutputFile(this.getFilename());
this.recorder.setOnErrorListener(this.errorListener);
this.recorder.setOnInfoListener(this.infoListener);
try
{
this.recorder.prepare();
this.recorder.start();
} catch (final IllegalStateException e)
{
e.printStackTrace();
} catch (final IOException e)
{
e.printStackTrace();
}
}
This is working perfactly. Hope it helps you :)

Categories

Resources