Stopping & Starting Music On Incoming Calls - Stack Overflow PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

41

Stopping & Starting music on incoming calls


android

android-activity

media-player

incoming-call

I have implemented an Activity that plays media from a URL in android


In order to add pause functionality when the incoming call is incoming I created a receiver that sets a a variable when the call is coming. The activity
reads this variable and then pauses the music in its onPause() method and resets is when the call is done and the activity resumes the music in its
onResume() method
This works fine as long the activity has the focus. If I go back to home screen while the music is playing, and then the call comes, the activity's onpause is
not called & hence i can' stop & start the music
What is the way out for this? Anybody implemented a media player so that it intercepts incoming & outgoing calls at all the times & stops and starts the
music correctly?
share

improve this question


user669231
381 3 10 22

Asked
Apr 10 '11 at 7:58

Nishanthi Grashia
7,060 5 21 38

Edited
Oct 14 '14 at 8:32

6 Answers

66

Order By

There are a few things you can do:


First of all, you can listen for changes in the call state using a PhoneStateListener. You can register the listener in the TelephonyManager:

Votes

PhoneStateListener phoneStateListener = new PhoneStateListener() {


@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//Incoming call: Pause music
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

Remember to unregister the listener when it's no longer needed using the PhoneStateListener.LISTEN_NONE:
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}

For more information read the documentation.


Another thing you can do is listening for the broadcast android.intent.action.PHONE_STATE. It will contain the extra TelephonyManager.EXTRA_STATE
which will give you information about the call. Take a look at the documentation here.
Please note that you'll need the android.permission.READ_PHONE_STATE-permission in both cases.
share

improve this answer


Kaloer
2,970 1 17 30

Answered
Apr 10 '11 at 9:53

Edited
May 3 '12 at 16:21

Do I need to register the PhoneStateListener in my Activity that plays music? What if that activity has lost focus? Will the listener still work? user669231 Apr 10 '11 at 12:20

Yes the listener needs to be registered in the Activity which plays music, and it will be called if the activity has lost focus (as long as it has not been killed). However, it may be more
appropriate to use a Service to play the music instead of an Activity as the Activity may be killed. Kaloer Apr 11 '11 at 7:14

This worked for me. I created a service & registered the phonestatelistener in it. Now even if my activity loses focus(like user goes to the home screen by pressing the home button),
the incoming calls get detected & I am able to stop & start music user669231 Apr 12 '11 at 18:40

You should also request AudioManager::requestAudioFocus(), so when the focus has been granted you can play the stream;
AudioManager.OnAudioFocusChangeListener::onAudioFocusChange(int ) depending on the change application should pause/play, refer to AudioManager and
OnAudioFocusChangeListener documentation. Vamsi Jan 9 '12 at 11:14

@kaloer we need the use the phonestatelistener where we are using audio player activity or it has to be in sepereate one Karthick M Sep 13 '13 at 13:05
show 1 more comment

11

I think requestAudioFocus() should be able to handle this case automatically. You don't need to check call state explicitly.
Audio Focus is cooperative in nature. That is, applications are expected (and highly encouraged) to comply with the audio focus guidelines, but
the rules are not enforced by the system. If an application wants to play loud music even after losing audio focus, nothing in the system will
prevent that. However, the user is more likely to have a bad experience and will be more likely to uninstall the misbehaving application.
To request audio focus, you must call requestAudioFocus() from the AudioManager, as the example below demonstrates:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// could not get audio focus.
}

share

improve this answer


spanjeta
1,103 8 19

Answered
Nov 1 '12 at 11:50

This will not handle incoming calls nicely. - They seem like they duck the audio automatically, but incoming calls do not trigger onAudioFocusChange. Peter Ajtai May 8 '13 at
0:14

This is the recommended way according to docs: developer.android.com/intl/ru/guide/topics/media/ david72 Jan 12 at 23:02

add a comment

10

I think that AudioManager is the best and fast solution. Here there is my implementation example:
public class MyActivity extends Activity implements OnAudioFocusChangeListener {
private AudioManager mAudioManager;
@Override
public void onCreate(Bundle savedInstanceState) {
...
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
...
}
@Override
public void onDestroy(){
super.onDestroy();
...
mAudioManager.abandonAudioFocus(this);
...
}

}
I hope it's helpful for you :-)
share

improve this answer


mickesource
111 1 2

OR - You can try a Receiver Application.


Create a Receiver named CallRecord.java

Answered
Jul 25 '14 at 15:11

package com.example.callreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class CallReceiver extends BroadcastReceiver{
TelephonyManager telManager;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context=context;
telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

Then Add this line in manifest.xml file to register it on the App


<receiver android:name="CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>

share

improve this answer


kulakesh
89 1 3

Answered
Nov 5 '12 at 14:00

I don't like the name of that receiver :) Lo-Tan Apr 7 '15 at 22:57
add a comment

IMHO better way is described in off docs: http://developer.android.com/training/managing-audio/audio-focus.html

share

improve this answer


Anton Balashov
148 2 12

Answered
Dec 1 '14 at 20:58

For me idle state was coming while there was incoming call, the quick fix is to check in the broadcast receiver
BroadcastReceiver phonestatereceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//pause here
}
else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
//pause here
}
else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
//play here
}
}
}
};

share

improve this answer


Inder Kumar Rathore
22.8k 8 67 115

Answered
Jun 19 '14 at 18:31

Edited
Jun 19 '14 at 19:23

Your Answer

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

You might also like