Play an alarm during a call - android

I am creating an application that i have to play a sound. If the phone is ringing or in a call, i want to play the sound in the earphone. The code bellow works perfectly only if the screen is on, but in a call, when i put the phone in my ear, the screen gets off and my "alarm" doesn't play. any hint?
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int state = tm.getCallState();
mMediaPlayer.setLooping(true);
if(state == TelephonyManager.CALL_STATE_OFFHOOK || state == TelephonyManager.CALL_STATE_RINGING){
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mMediaPlayer.setScreenOnWhilePlaying(true);
audioManager.setSpeakerphoneOn(false);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
audioManager.setMode(AudioManager.MODE_IN_CALL);
Log.d("GOFC","Tocando ou fora do gancho");
} else {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
vibrator.vibrate(pattern, 1);
Log.d("GOFC","Normal");
}
mMediaPlayer.prepare();
mMediaPlayer.start();

You might need to use WakeLock. You should listen for events when phone is picked up to acquire WakeLock and when phone is hanged to release WakeLock.
This is just a wild guess though.

Related

android stop audio when phone lock after play about 10-20 min

I have a app radio, which use exoplayer
Almost good work, but when phone in background radio play about 10-20 min and stop and I turn phone radio continue play. How can I fix it programatically?
Mediaplayer have a method like setWakeMode(), but I do not find same to Exoplayer. Sorry for my English
I faced the same problem with exoPlayer, when phone screen locked AudioTrack shutdown after few minute, So you can add WakeLock or WifiLock in your code to keep exoplayer running in background,Even if the phone’s screen is locked.
if (intent.getAction().equals(ACTION_PLAY)) {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.requestAudioFocus(this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
wifiLock = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "wifiLock");
wifiLock.acquire();
if(exoPlayer == null){
//Your code Exoplayer her
}
} else if (action.equalsIgnoreCase(ACTION_STOP)) {
if (exoPlayer != null) {
exoPlayer.stop();
}
if (wifiLock != null && wifiLock.isHeld()) {
wifiLock.release();
}
}
}

After Unmute phone when call came only vibrate phone, not able to play ringtone

I'm building an Android app that unmute phone when an Incoming call came in phone.
I use BroadcastReceiver to receive incoming call events. I switch phone from mute mode to Ring Mode when BroadcastReceiver receive incoming call events.
And expect Phone will vibrate and Play ringtone.
But Phone only vibrate, can't play ringtone though phone ring set to max sound.
I found many apps on play store those can unmute before call and play ringtone and vibrate both. Example: One App Link
My code Below:
public void onReceive(Context context, Intent intent) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
if (intent.getAction()!=null && intent.getAction().equals("android.intent.action.PHONE_STATE")){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume/2, AudioManager.FLAG_PLAY_SOUND);
}
}
}
I'm working on something very similar. You actually have to play a ringtone after overriding the audio settings. But you only need to do this on Android 6.0+ Get an instance of the ringtone and play it when phone state is ringing else just stop it.
public void onReceive(Context context, Intent intent) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(context, uri);
if (intent.getAction()!=null && intent.getAction().equals("android.intent.action.PHONE_STATE")){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume/2, AudioManager.FLAG_PLAY_SOUND);
r.play();
}else {
r.stop();
}
}
}
Problems you'll run into with this implementation. You will get multiple ringtones playing because the onReceive method is called many times during Phone State changes. The provides a different context each time onReceive is called and creates a different instance of Ringtone each time. I launched a background service to fix this issue so I could hold a single reference to a context and thus a single reference to Ringtone.
Some devices may not have volume control and may operate at a fixed volume, and may not enable muting or changing the volume of audio streams.
isVolumeFixed()
This method will return true on such devices.

Call_withSpeaker on setting alarm not working on samsung 4.2.2

My application Call_withSpeaker on setting alarm is working fine in android 4.2.1 micromax , 4.1.2 samsung and 2.3 motorala. Only Problem with Samsung 4.2.2.. every thing fine but the speaker is not enable on call start only in 4.2.2 samsung . I am Using PhoneStateListener and if call start once i use
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
The issue what i am facing here is Turn Speaker Phone on and off is not working properly only for Samsung 4.2.2 and samsung duas 4.1.2
What to do.. Any idea ?? Is it related to version or some setting in mobile ... ??
Thank you ..
This is only because In some mobile (SdkVersion 11 and above ) , its not going through TelephonyManager.CALL_STATE_OFFHOOK and TelephonyManager.CALL_STATE_RINGING and directly call TelephonyManager.CALL_STATE_IDLE so in some case its fails to run your logic. Better if you check each and every callstate than you can get to know the problem.
You Have to exercise your brain little more..:)
check..It will help you
if (TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING start ");
isPhoneCalling = true;
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK..callringing");
isPhoneCalling = true;
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "_callend on start ");
Log.i("start ", "start "+callFromApp + isPhoneCalling );
if (isPhoneCalling) {
Log.i(LOG_TAG, "IDLE_callendafter ring ");
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_NORMAL);
//Deactivate loudspeaker
audioManager.setSpeakerphoneOn(false);
// Remove listener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_NONE);
isPhoneCalling = false;
}
}

Sound played through bluetooth headset while ringing? - android

I'm trying to get a sound (namely a small wav file representing the persons name - not auto generated) to play over a bluetooth headset while the phone is ringing. Essentially it would replace the default ring of the bluetooth device and replace it with the name of the caller!... well, anyway... it's not working.
I've been able to play the wav file directly on the headset within an activity, but when moved to the onReceive function of the broadcast receiver it fails to play the same sound over the headset while the cell phone is ringing...
I feel like I must be missing something. Is there something I can do to revise this code?
Thanks in advance
public void onReceive(Context context, Intent intent) {
mContext = context;
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
System.out.println("Its Ringing");
AudioManager am = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
am.setBluetoothScoOn(true);
am.startBluetoothSco();
am.setMode(AudioManager.MODE_NORMAL);
try{
AssetFileDescriptor afd = mContext.getAssets().openFd("my.wav");
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.setOnPreparedListener(new OnPreparedListener(){
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
player.prepareAsync();
}catch(IOException ioe){
System.err.println(ioe.getLocalizedMessage());
}}}
You should be preparing/setting up your media player before you receive the phone call because this can take some time. Also, remove am.setMode(AudioManager.MODE_NORMAL);, you could replace it with am.setMode(AudioManager.STREAM_VOICE_CALL); but it is probably not necessary because you should already be in that mode if your BT device is paired/setup.

setRingerMode during incoming call - RINGING

I have a task - change the ringer volume immediately when phone ringing. For example:
After detecting, that there is incoming call I need to set ringer volume to 0 (mute) and vibrator also should be disabled (if it not disabled already). Then there is delay when I need to perform another code (startComputing();). After that ringer volume should be changed to certain value (f.e.7) and vibrator should be activated. Here is my code:
public class IncomingCallReceiver extends BroadcastReceiver {
private AudioManager amanager;
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
amanager.setRingerMode(0x00000000); // no sound and no vibration
startComputing();
amanager.setRingerMode(0x00000002); // normal
amanager.setStreamVolume(AudioManager.STREAM_RING, 7,
AudioManager.FLAG_SHOW_UI
+ AudioManager.FLAG_PLAY_SOUND);
}
}
private void startComputing() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1)The main problem is that after this: amanager.setStreamVolume(AudioManager.STREAM_RING, 7, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
ringer does not ring at all, only toast message appears that sound level changed but phone does not play any sound. How this can be changed?
2)Also there is problem that amanager.setRingerMode(0x00000000); does not change Volume and vibration immediately but just in about half a sec.
Thank You in Advance.
Jacob
I don't think there is anything you can do to overcome the 1/2 sec delay before you mute the ringer. Sometimes it works (ie very short or no delay), sometimes you will hear the ringer for a short period of time.
I think the problem you are having is with the flags passed to setStreamVolume.
Try this:
amanager.setStreamVolume(AudioManager.STREAM_RING, 7,
AudioManager.FLAG_SHOW_UI|AudioManager.FLAG_PLAY_SOUND);
You should be using the bitwise inclusive or (|), and not simple addition (+).

Categories

Resources