AUDIOFOCUS_LOSS called after a phone call in android

后端 未结 2 901
轻奢々
轻奢々 2021-01-02 05:05

I\'m trying to pause media player when the phone rings. I use the sample code from android site. It\'s like this;

public void onAudioFocusChange(int focusCha         


        
相关标签:
2条回答
  • 2021-01-02 05:34

    Two things to check: 1) Do you have a play button for instance, that when clicked, requests focus and starts playback? I had a problem once that they played a "tick" sound to provide feedback of a click. They would take audio focus away from my app when that happened. In your XML set this value to false for all your buttons in that activity:

    android:soundEffectsEnabled = 'false'
    

    2) Check for the installation of other media player apps that also request focus. Are they requesting focus right after losing it? If so there's not much you can do about this other than warn your users not to install this app.

    0 讨论(0)
  • 2021-01-02 05:43

    Probably it will help somebody. I had this issue when in project wasn't using directly MediaPlayer but through VideoView. It has following code while opening the file

    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    

    So as you open the file it gains audio focus and listener that is set before gets fired with focusChange AudioManager.AUDIOFOCUS_LOSS.

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