Stop and start music on incoming calls

I implemented an Activity that plays media from a url in android

To add pause functionality to an incoming call, I created a receiver that sets a variable when a call arrives. Activity reads this variable and then pauses the music in the onPause () method, and is reset when the call is made, and the action resumes music in the onResume () method

This works just fine since the activity is focused. If I return to the main screen while playing music and then a call comes in, onpause activity will not be triggered and therefore I can β€œstop and start the music

What is the way for this? Has anyone implemented a media player so that it intercepts incoming and outgoing calls all the time and stops and plays music correctly?

+48
android android-activity media-player incoming-call
Apr 10 2018-11-11T00:
source share
6 answers

There are a few things you can do:

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

 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 is no longer needed using PhoneStateListener.LISTEN_NONE :

 TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if(mgr != null) { mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); } 

Read the documentation for more details .

Another thing you can do is listen to the android.intent.action.PHONE_STATE broadcast. It will contain an optional TelephonyManager.EXTRA_STATE that will give you call information. Check out the documentation here .

Please note that in both cases you will need android.permission.READ_PHONE_STATE transmission.

+84
Apr 10 2018-11-11T00:
source share

I believe AudioManager is the best and fastest solution. Here 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); ... } @Override public void onAudioFocusChange(int focusChange) { if(focusChange<=0) { //LOSS -> PAUSE } else { //GAIN -> PLAY } } 

}

Hope this helps you :-)

+31
Jul 25 '14 at 15:11
source share

I think requestAudioFocus () should be able to handle this case automatically. You do not need to explicitly check the status of the call.

Audiofocus is a collaborative one. That is, it is expected that applications will (and strongly recommended) comply with the rules of audio focus, but the rules will not be respected by the system. If an application wants to play loud music even after losing focus of sound, nothing in the system will prevent this. However, the user is more likely to have a bad experience and, most likely, he will delete the wrong application.

To request a sound focus, you must call requestAudioFocus () from the AudioManager, as shown in the example below:

 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. } 
+12
Nov 01 '12 at 11:50
source share

OR - You can try the recipient application.

Create a receiver named CallRecord.java

 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); } private final PhoneStateListener phoneListener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { try { switch (state) { case TelephonyManager.CALL_STATE_RINGING: { //PAUSE break; } case TelephonyManager.CALL_STATE_OFFHOOK: { break; } case TelephonyManager.CALL_STATE_IDLE: { //PLAY break; } default: { } } } catch (Exception ex) { } } }; } 

Then add this line to the manifest.xml file to register it in the application

 <receiver android:name="CallReceiver" > <intent-filter> <action android:name="android.intent.action.PHONE_STATE" > </action> </intent-filter> </receiver> 
+12
Nov 05 '12 at 14:00
source share
+5
Dec 01 '14 at
source share

An unoccupied state was occupied for me when there was an incoming call, a quick fix is ​​to check 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 } } } }; 
+1
Jun 19 '14 at 18:31
source share



All Articles