AudioExoPlayer a simple player with great potential

Well, if that will be a set-able/read-able boolean property, it would be quite nice!! :slight_smile:

If set=true,
On incomming calls: AudioExoPlayer does not pause but sound is muted (or Vol=0)

On call end (no answer or hangup or refuse, etc.) : AudioExoPlayer is unmuted (or Vol is set to previous vol)

Is that what you propose? That would be just what I need!!

-Randal

I tested this on my POCO phone with Android 15, and it doesn't work for me. Starting the second player doesn't trigger the OtherPlayerStarted event.

Hmmm... I assume you ran the OtherPlayer_Test.aia app that I posted above...

Let me try on other API/OS versions...

But as far as I am converned if you can implement the OnPhoneCallStart and OnPhoneCallEnd events/actions as discussed above, I would preferr that to the OtherPlayerStarted ... since the Phone call interruption on my phone did NOT fire the IA2 Comp[onent Player.OtherPlayerStarted event anyway. So your suggestion is better for me ... maybe not for other users??

Let me test a bit and get back.

-Randal

Tested on API 26 (OS ver 8) and 35 (OS ver 15) and seems to work for me.

Start Player 1 (Note that Player1 volume is set VERY low...), then start Player 2 which stops player 1 and starts player 2 playing the same audio clip but louder...I also get the momentary display/Alert notification stating one player detects the start of the other.

You can check which player is playing (or not) with the Display Player Status button...

Can you tell me what happens? Do both players play after starting the second one? If you start player2 first (louder one) does player1 start (use button to see which are playing) and player2 keeps playing...

You might remove/delete Notifier1 just to see of that's possibly making the difference. When I deleted it, it took no other code changes and it worked as expected. Again, you can use button to verify which is or is not playing...

-Randal

Yes, both players are playing simultaneously for me. I swapped Player for my AudioExoPlayer and got the same result. I think it might be some internal implementation changes depending on the phone manufacturer. But I still receive a notification whenever a call comes in.

So if you need such an event, I can do something like AudioFocusChanged, which returns a boolean value indicating focus lost. If it returns true, it means focus has been lost and someone is calling, and if it returns false, it means the call has been disconnected.

Yes, an Event. But to clarify, by "returns" do you mean that the event passes a single boolean parameter? That would be good...I just want to be sure you don't mean a function that returns a boolean value when called?

However, FULL DISCLOSURE:

I have to tell you that the AudioExoPlayer will be running in an Itoo service/process (;which I have implemented).

BUT, earlier I reported what appears to be a bug (Open Source • Background Tasks: Itoo 🚀 - #1165 by Randal_Andress ) that prevents the PhoneCall component's events IncomingCallAnswered and PhoneCallEnded from working...

If you are using these PhoneCall events in your extension to provide this new event, I'm concerned that it may not work from within Itoo either. If you build it, I am happy to try it, but I would not want you to go to a lot of trouble building it when it may not work from Itoo :frowning: ... and if it is fixed, then I can simply use the PhoneCall component for these events :slight_smile: ... I'd like to give your new event a try, but it may not solve my problem.

-Randal

The previous event had a "focusLost" parameter, which will only be true if losing focus would result in your player being paused (e.g., while someone is calling on the phone or using an instant messenger). Sometimes, losing focus will cause the player to mute, but this won't trigger the event.

If this event is called with an argument of true, it will mean that my player has lost focus and has been paused, correct?

So, if I set the volume to zero and issue a Play command, that should minimize any delay added to the stream that is playing.

Will the event be called when the call or message has ended with a parameter of false? If so, I could then unmute my player.

-Randal

No, it is not possible to play when the player is not have focus. I tried.

You can try a new event.

Thanks, @Patryk_F, for version 2.0! Just downloaded it and the test app... looking forward to trying it tonight!

Tnx,
Randal

@Patryk_F, just letting you know it will be tomorrow before I can get you any meaningful feedback :frowning:

Kind regards,
Randal

@Patryk_F, I have not been able to get the new event (AudioFocusChanged) to occur.

Perhaps my test app is ill-designed, but I had expected that, if the player was playing, an incoming call would cause the even to be called. But the player pauses and then unpauses on the end of the call without any indication that the event was called.

BTW, the default url is a music data file included with the app....(so long as you do not press the toggle url button).

UIExoPlayer_FocusChange.aia (6.7 MB)

Perhaps I have misunderstood or have made an error in my implementation.

Can you see what is wrong?

==================

I have added a smaller/simpler test:

ExoPlayer_Focus_Test.aia (6.7 MB)

Screen1.Initialize starts the player. The start of a phone call pauses the player - when the call ends, the player resumes with no indication of an error or a focus change.

-Randal

There was a minor error which I have already fixed.

Thanks! I have downloaded version 2.1 and now I am able to get the event. However, I cannot seem to stop the player from pausing for the duration of the call.

I have tried to execute the play command and reset the volume, but it still seems to pause/

Here is the focus cnanged blockl:

The test case blocks:

The ai2 cocde:

ExoPlayer_Focus_Test_v2_1.aia (6.7 MB)

Any ideas on how to keep the phone call from pausing the player?

-Randal

Yes, I think this is forced by the system, and we can't continue playing when the phone rings. Have you done similar tests with the regular Player?

I asked Gemini for you and below is the answer
Taifun


Generally, it is not possible for a music player app to continue playing audio audibly during an active phone call on a standard Android phone.

Android's audio focus system is designed to give exclusive, high-priority access to the phone call app, which by default causes other media apps (like music players) to automatically pause or mute. This is the intended behavior for a good user experience, ensuring the user can hear the call clearly.

Key Technical and User Considerations

  1. Audio Focus
    The primary mechanism that prevents simultaneous audio is Audio Focus.
  • When a phone call starts, the Phone app requests and is granted the highest level of audio focus.
  • Media apps (like your music player) are notified of this loss of focus with a AUDIOFOCUS_LOSS_TRANSIENT event, which signals them to pause their playback.
  • Even if a music app was programmed to ignore this focus loss, the Android system on newer versions (Android 12+) is designed to mute media playback when an incoming call is received to prevent conflicts.
  1. Hardware and Bluetooth Limitations
    The ability to play two different audio streams (a phone call and music) to the same Bluetooth device is often a limitation of the Bluetooth standard (HFP vs. A2DP profiles) or the specific Bluetooth hardware, which typically prioritizes the call audio.

Exceptions and Workarounds
While standard media playback is blocked, there are a few scenarios where multiple audio streams might be possible:

  • System/OEM-Specific Features (e.g., Samsung): Some device manufacturers, like Samsung, offer features that can override the default audio focus behavior.
    • Good Lock/Sound Assistant: Users can sometimes enable a "Multi Sound" or "Separate app sound" feature that allows selected apps to play audio simultaneously with a call, or play one app's audio to a different output device.
  • Developer/Root-Level Tweaks: Advanced users can sometimes use third-party apps like AppOps (often requiring root or ADB access) to revoke the "Take Audio Focus" permission from the phone call app.
    • Caveat: This is highly technical, not recommended for average users, and may result in the music playing at full volume over the call, making it difficult to hear the conversation.
  • App Configuration: Some individual music player apps might have a setting like "Pause During Call" which, if unchecked, attempts to let the music play, but this is usually still overridden by the phone's system-level call prioritization.
1 Like

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.