Pause song when phone rings

前端 未结 3 987
无人及你
无人及你 2021-01-23 09:17

I am pretty new to android development.

I wish to pause the multimedia whenever the phone rings and then start again when the call ends.

how can that be done?

相关标签:
3条回答
  • 2021-01-23 09:49

    What you want is a BroadcastReceiver, which is notified, when the phone receives a call. Here you can find a Tutorial on this.

    0 讨论(0)
  • 2021-01-23 09:49

    As per this answer: Pause music player on a phone call and again resume it after phone call in android

    It would be easier to use a

    PhoneStateListener

    http://developer.android.com/reference/android/telephony/PhoneStateListener.html

    0 讨论(0)
  • 2021-01-23 09:55

    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);
             ...
         }
    
    
        @Override
        public void onAudioFocusChange(int focusChange) {
            if(focusChange<=0) {
                //LOSS -> PAUSE
            } else {
                //GAIN -> PLAY
            }
        }   
    }
    

    I hope it's helpful for you :-)

    0 讨论(0)
提交回复
热议问题