I'm using the ExoPlayer Google Cast extension v2.8.4. When playing content using the CastPlayer, the device's physical volume buttons should control the receiver device's volume. This behaves as expected when the CastPlayer is contained within an Activity. However, when the CastPlayer is contained within a Service (such as a MediaBrowserServiceCompat) the physical volume buttons change the sender device's media volume.
I've created 2 sample projects demonstrating the issue:
As seen in the image below, the cast device volume does appear in the full volume controls view, but the phone's media volume is the active control stream when the CastPlayer is in a service.
Is there a way to set the proper volume control stream in the Service implementation?
I've determined the issue, and the cause is not due to a bug in ExoPlayer or the Cast extension. This commit in the CastVolumeControlServiceTest fixes the issue.
After debugging and looking through the device's logs, I discovered that the volume key events were directed to the active MediaSession. By setting the MediaSession to inactive, the volume control stream will control the receiver device, so the solution is the toggle MediaSession to active/inactive depending on which Player is current.
This didn't seem intuitive, but it works.