AudioExoPlayer a simple player with great potential

Yes...using the standard Player component running a normal UI/foreground process, I have used the PhoneActivity component to detect phone call start/end. It works, but within the event, the player dies not respond to Player.Start to continue playing - instead it remains paused until the call ends.

Seeing what @Taifun has provided explains it...what I want is mute instead of pause, which seems to be in keeping with the idea behind the phone call priority ... be nice if there were an option to select whether to pause or mute other audio (or a particluar app's audio) during a call.

For my application, if I am unable to somehow substitute mute for pause, I can use the PhoneActivity or, in the case of the AudioExoPlayer, the focuschange event to capture the delay being currently applied to the active live stream and then have the end of the call (or restored focus) event to restart the player with the recorded delay...but it would likely add additional delay - but maybe not enough to matter???

However, as I mentioned in an earlier message, the Player in my app runs in an Itoo process and so I must now test your AudioExoPlayer.FocusChanged event to see if it works there :blush:.... PhoneActivity does not work with the current version of Itoo (Open Source • Background Tasks: Itoo :rocket: - #1165 by Randal_Andress)

If your AudioExoPlayer.FocusChanged event works from an Itoo process/service, then I may have my solution.

Thanks, and I'll get back to let you know whether or not it works for me.

Kind regards,
Randal

@Taifun, thanks for digging this out! It certainly explains what I am seeing...

Do you see any way that the default pause could be replaced with mute or continue playing OR allow app to be able to restart player or adjust its volume during the call? Is this accessible to the developer so that it could be placed in an extension?

What about this?
Pausing/Playing while playing music and the phone rings.

Thanks again for providing the information that explains my failures :frowning:

Kind regards,
Randal

Let me ask Gemini again for you
Taifun


The programmatic solution for muting music instead of pausing it during a phone call involves detecting the phone state and then using your media player's volume control to set the volume to zero, ensuring the player remains active.

On modern Android (SDK 35), you generally manage media playback through a MediaSessionService and an underlying player (like ExoPlayer).

  1. Mute via PhoneStateListener and Player Volume Control
    You must implement a PhoneStateListener to detect when a call is incoming or active. Once detected, you use your player's setVolume() method to achieve the mute effect.

  2. Using Audio Focus to 'Duck' (Alternative)
    Android's built-in mechanism for temporary audio interruptions is called Audio Focus. The standard response to a transient focus loss (like a phone ringer) is to "duck" (reduce volume) or pause.
    If you request a "duckable" focus, the system may automatically reduce your volume instead of causing a full pause on newer devices. While this is not guaranteed to be a full mute, it is the platform's standard way to keep playback running while lowering volume.

When requesting audio focus, you must handle the AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK event:

// Inside your AudioFocusChangeListener implementation:
@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        // This is the ideal callback for a mute/duck scenario
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            // Set volume to 0.0f (mute) or a very low value (duck)
            if (mPlayer.isPlaying()) {
                mPlayer.setVolume(0.0f, 0.0f);
            }
            break;
            
        case AudioManager.AUDIOFOCUS_GAIN:
            // Restore original volume
            mPlayer.setVolume(mOriginalVolume, mOriginalVolume);
            break;

        // If you receive this, you must pause/stop to be a good app citizen
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        case AudioManager.AUDIOFOCUS_LOSS:
            mPlayer.pause(); 
            break;
    }
}

Key Takeaway: The most reliable way to enforce a mute (volume \approx 0) instead of a pause, while still respecting the Android ecosystem, is to monitor the phone call state directly using a PhoneStateListener and manually adjust your player's volume (setVolume(0.0f, 0.0f)).

If you're having trouble with the audio focus API, the video below explains how to mute media volume in Android's Silent Mode, which is a conceptual parallel to muting during a call. How to Mute Media Sound in Silent Mode on Android Phone.

@Taifun, Thanks for coaxing Gemini to put this together :slight_smile: - it is EXACTLY what I was wanting to know!!

Looks like both methods might be possible as additional features of @Patryk_F 's AudioExoPlayer extension (or possibly some other Player extension?):

Either:

  1. a MuteDuringPhoneCall boolean property using PhoneStateListener to determine the presence and duration of a call, or
  2. a DuckDuringLossOfFocus property, possibly boolean, possibly a volume value to use while "ducking" (default=0) something like in the @Override method shown...

In either case the volume should be returned to the value before the call began.

@Patryk_F, what do you think? Does this make sense to you? Would these be reasonable additions to your AudioExoPlayer?

-Randal

When we read Taifun's two answers from Gemini, we can conclude that one Gemini answer excludes the other. But I can disable the internal focus management in Exo Player and replace it with my own and test it.

I makes sense that an app would not want to (or be able to) activate/use both. But I got the impression that both methods are available and I would hope both could be made available in an AI2 extension - perhaps with a lock within the extenson to prevent a app from using both of them...But, of course, you would understand things better than me.

@Patryk_F, I would very much appreciate an implementation of either method. However the other option (the one I called "MuteDuringPhoneCall boolean property using PhoneStateListener") might be be the preferred method if only one were provided.

However: "Begars can not be choosers" (BEGGARS CAN'T BE CHOOSERS Definition & Meaning - Merriam-Webster). Anything you are able to do will be greatly appreciated!

:slight_smile: -Randal

I've done some testing, and it's possible to continue playback during an incoming call. Although the system pauses playback automatically, we can resume playback. I just need to implement this in the code.

@Patryk_F, If I understand your question, for my application, when a call in started, I want the player to be muted (set saveVol=Vol and set Vol=0) and to continue "playing" the streaming source, so that when the call is ended, the player may be un-paused (Vol=savedVol) and be playing the same audio that would have been playing if the call had not happened.

Does that answer you question?

Thanks,
Randal

You can do the volume reduction and other logic in blocks. I'll just enable replay. This is too specific and non-standard for me to permanently add it to the extension.

Sure!
-Randal