AudioRecorder blocks microphone - android

I'm working on an app which allows user to take voice notes. For this I'm using the AudioRecorder to constantly obtain audio data from microphone and process it into Mp3.
It all worked great until recently I found out that if I receive/make a call while my app is running in background the other party can't hear me. Clicking on mute/unmute button on dialer screen doesn't do anything - the microphone seems to be disabled until I stop my app.
Now the question is how can I handle this situation? Is my only option is to release AudioRecorder once a voice call is started? I'm perfectly ok if AudioRecorder would just skip the audio data from microphone during the call, but the situation when user can communicate because of the app is unacceptable.
Thanks,

Only 1 app can use the mic at any given time. You might want to use PhoneStateListener so that you can release the recorder when the phone rings or is off the hook.

You can intercept incoming calls using PHONE_STATE action as part of a BroadcastReceiver. Then, in your onReceive function of your receiver, you would check states of the phone:
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
//Phone is ringing
}else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){
//Call received
}else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
//Call Dropped or rejected
}
}
Do not forget to add the neccessary permissions in your manifest.
Likewise, you can intercept outgoing calls as well. Here is a nice tutorial explaining how to do both.
The rest is about stopping and resuming recorder depending on what you intercept.

Related

android code to pick the voice through call

Is there is any android code to pick the voice from the other side in the call?
meaning while I'm speaking with another person in my Phone I can Play a speaker that picks the voice, can I direct this voice to my application to record, recognize it, or convert to text?
Since my target is to convert the voice in the call to text.
Thanks in advance.
Use Broadcast Receiver to handle calls
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
}else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){
//apply recording here
}else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
//stop recording
}
}
To convert Voice to Text Follow this Link
You can record from AudioSource.VOICE_DOWNLINK. Keep in mind though that this might not work on every single Android phone out there, and that voice call audio is quite heavily compressed and therefore might not give you any good results from a Speech-To-Text engine.

How to turn speaker ON making call on android Jellybean

What I'm trying to do is to make call from my app and turn on speaker in this call.
Everything was okay on android 4.0, I've just set audiomanager.setSpeakerphoneOn(true) before starting call intent.
But on android 4.1 PhoneUtils checkin speaker state before call and turn it off(logcat):
D/PhoneUtils( 1029): about to activate speaker
D/PhoneUtils( 1029): activateSpeakerIfDocked()...
I/PhoneUtils( 1029): Forcing speaker off when initiating a new outgoing call...
I've also tried to listen call state using : mTelephonyManager.listen(receiver, PhoneStateListener.LISTEN_CALL_STATE);
After this receiver will catch 3 states:
IDLE(don't interesting for mee)
CALL_STATE_RINGING(also don't interesting because I need work with outgoing calls)
CALL_STATE_OFFHOOK(i though this one will help)
But last "event" was also send before PhoneUtils check speaker state(logcat):
11:43:48.089 D/KeyguardViewMediator( 665): keyguard receiver action : android.intent.action.PHONE_STATE
11:43:48.089 D/KeyguardViewMediator( 665): keyguard receiver TelephonyManager.EXTRA_STATE : OFFHOOK
11:43:48.099 D/PhoneUtils( 977): setAudioMode()...OFFHOOK
11:43:48.099 D/MyPhoneStateListener:main( 2590): catching call state CALL_STATE_OFFHOOK
11:43:48.099 D/MyPhoneStateListener:main( 2590): turning phone speaker on
...
11:43:48.119 D/PhoneUtils( 977): about to activate speaker
11:43:48.119 I/PhoneUtils( 977): Forcing speaker off when initiating a new outgoing call...
If somebody faced with same problem or have a solutuion?
Thanks!
I've had this problem this week..
Reading the jelly bean code source, i've found this code
// This is not an error but might cause users' confusion. Add log just in case.
Log.i(LOG_TAG, "Forcing speaker off when initiating a new outgoing call...");
PhoneUtils.turnOnSpeaker(app, false, true);
So that's it, in version 4.1 android forces speaker off in incoming and outgoing calls.
My solution is turn on the speaker in my call state broadcast receiver, after start to make a call

Is Android speech recognition service busy?

I use Android speech recognition service from my application.
It turns out that when two clients attempt to send requests at the same time (say, the user started one voice search from an app, then switched to another application while the search is still active, and launched another search), the service stops working.
Is there a way to determine that there's a voice recognition session in progress, so that I can refuse to start another one?
You should probably be trapping ERROR_RECOGNIZER_BUSY in the onError handler:
#Override
public void onError(int error) {
switch (error) {
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
// do something reasonable
…
}
}
You're most likely (only?) going to receive that on a request to start listening; unfortunately, I'm not aware of any way to test this condition without trying (e.g. I don't think you can pre-detect another process's listener being active to remove a microphone button)
(On stopping listening, you'd only get this error if you were trying to kill another process's listener somehow.)
I was getting this error (ERROR_RECOGNIZER_BUSY) and found out that the package was missing in my intent set up. So I just added this line and it works now:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
...
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());

android media player: dealing with incoming phone calls and playing audio in the background

I am fairly new to Android app development and I need some direction.
I have written an app that plays mp3 files from the internet via the Android MediaPlayer either one at a time or from a playlist.
The user can play one mp3 at a time or queue up several mp3's, go to a playlist screen and hear each one after the other.
I have a progress bar, start, stop, pause, and continue buttons on the screen that plays a single mp3.
On the playlist screen there is no progress bar, but there are start, stop, pause, and continue buttons.
I want the following behavior but I am not sure how to implement it correctly:
when an mp3 is playing and an incoming phone call is received, the mp3 is paused; when the user hangs up, the mp3 is resumed automatically
when an mp3 is playing, and the user presses the phone's "home" button, the mp3 continues to play while the user is free to do other things (like check email for example);
Do I need to implement the media player as a service?
Do I need a separate thread to run the media player?
I am doing neither at the moment.
Is there a good tutorial on this?
I have tried the following tutorial in a separate app that implements the media player as a service and it seems to do most of what I want but I haven't been able to figure out how to incorporate a "pause" and "continue" button.
"ServicesDemo - Using Android Services": http://marakana.com/forums/android/examples/60.html
As a followup question, are there canned media players that can be purchased or available as a free download that already have this functionality that can be included in my app?
I'm not posting any code here yet as this is more a general question, but will as a followup.
Thanks in advance,
Dave
Yes, you need service for your media player, and how to handle phone calls read about AUDIO_FOCUS.
Try this Code To Stop and Resume Song in between calling
PhoneStateListener phoneStateListener=new PhoneStateListener()
{
#Override
public void onCallStateChanged(int state, String phoneNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING )
{
MP.Pause();
}
else if(state==TelephonyManager.CALL_STATE_OFFHOOK )
{
MP.Pause();
}else if (state==TelephonyManager.CALL_STATE_IDLE)
{
MP.Start();
}
}
};
TelephonyManager manger = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(manger!= null) {
manger.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

How to automatically answer call and play prerecorded sound out to the caller and DTMF on Android

Could it be possible now, which we can develop an Android application having the following requirement for its working steps
Automatic receive the call, then
Play some sound out to the caller, wait for DTMF response, then
Record the caller sound (in the case that they permit, by pressing 1 as DTMF response)
Callee can play back the recoreded sound later.
Thank for all answers in advance .....
Hey I am also making same application...for Automatic receive call you need to change Source Code of android. I have done it successfully with froyo 2.2. There is method answerCall(Phone phone) in PhoneUtils.java file with that you can automatic receive call.
I am also searching for DTMF....but till now i didn't get anything which can be helpful....for DTMF decoding....
the first point seems possible. check the state of call if it is ringing state the program can auto attend it for you..you can find more information in this link.
http://www.devlper.com/2010/08/detecting-incoming-and-outgoing-calls-in-android/
http://prasanta-paul.blogspot.com/2010/09/call-control-in-android.html

Categories

Resources