Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add volume management to CastPlayer #2279

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

MGaetan89
Copy link
Contributor

@MGaetan89 MGaetan89 commented Mar 27, 2025

This PR enables volume management in the CastPlayer. In particular, the following methods are now supported:

  • setVolume()
  • getVolume()
  • isDeviceMuted()
  • setDeviceMuted()

I've also updated the fetchDeviceInfo() method so now the max volume information is also provided.


Note: I didn't do anything for the setDeviceVolume(), increaseDeviceVolume() and decreaseDeviceVolume() methods. Let me know if something should be done there too.

Note 2: I don't have access to b/364580007 (the TODO I modified in CastPlayer). Maybe I'm missing some information or it needs to be updated.

@MGaetan89 MGaetan89 force-pushed the cast_volume_management branch 2 times, most recently from c65bd97 to 94ff5e4 Compare April 2, 2025 17:10
@marcbaechinger marcbaechinger force-pushed the cast_volume_management branch from 94ff5e4 to 8c5766a Compare April 9, 2025 14:57
@marcbaechinger
Copy link
Contributor

Very nice change! Thank you very much!

setDeviceVolume(), increaseDeviceVolume()

I think this is fine. You are calling remoteMediaClient.setStreamVolume() which matches to setVolume(). Given there is no way to change the device volume with remoteMediaClient AFAIK, I think it's fine to not have the command for device volume changes and leave the methods as they are.

I don't have access to b/364580007 (

The bug is about setting the device info which is actually done. You can leave that with me.

I'm going to send this for internal review now. You may see some more commits being added as I make changes in response to review feedback. Please refrain from pushing any more substantive changes as it will complicate the internal review - thanks!

@AquilesCanta
Copy link
Contributor

Very nice change! Thank you very much!

+1. Thanks for your PR.

Coincidentally I've been working on a similar fix for a while. But I hit the same issue as (seemingly) a bunch of other people out there. E.g.: https://stackoverflow.com/questions/39498516/remotemediaclient-setstreamvolume-doesnt-changing-cast-volume

As a result I changed my implementation to not use setStreamVolume, and instead use CastSession#setVolume. I was planning on merging this soon. Did setStreamVolume actually work for you? If so, can you share with me the model of the device that you used to test?

FWIW, my plan was to:

  1. Implement setDeviceVolume using CastSession.setVolume (same for mute and CastSession#mute).
  2. Fix RemoteMediaClient#setStreamVolume, or at least figure out internally what's going on there.
  3. Implement, or figure out what to do with CastPlayer#setVolume() after dealing with 2.

@MGaetan89
Copy link
Contributor Author

Hey @AquilesCanta,

To test my changes, I did a small change to the demo Cast application similar to the patch below/attached. If you think it is useful to include it, I can make it look a bit nicer and include it in this PR.

Basically, I added a slider to change the volume. When sliding ends, I call Player.setVolume() to update the remote volume. On my side, this seems to be working as expected. However, the physical volume buttons don't work.

The Cast device that I used for testing is a Chromecast with Google TV, running Android 14.
The casting device is a Google Pixel 7 Pro running Android 16.

Note that there's no visual feedback on the TV that the volume is changing.

diff --git a/demos/cast/src/main/java/androidx/media3/demo/cast/MainActivity.java b/demos/cast/src/main/java/androidx/media3/demo/cast/MainActivity.java
index ae49a6f45a..ab51c2a87c 100644
--- a/demos/cast/src/main/java/androidx/media3/demo/cast/MainActivity.java
+++ b/demos/cast/src/main/java/androidx/media3/demo/cast/MainActivity.java
@@ -25,6 +25,7 @@ import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
+import android.widget.SeekBar;
 import android.widget.TextView;
 import android.widget.Toast;
 import androidx.annotation.Nullable;
@@ -85,6 +86,22 @@ public class MainActivity extends AppCompatActivity
     playerView = findViewById(R.id.player_view);
     playerView.requestFocus();
 
+    SeekBar volume = findViewById(R.id.volume_bar);
+    volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+      @Override
+      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+      }
+
+      @Override
+      public void onStartTrackingTouch(SeekBar seekBar) {
+      }
+
+      @Override
+      public void onStopTrackingTouch(SeekBar seekBar) {
+        playerView.getPlayer().setVolume(seekBar.getProgress() / 100f);
+      }
+    });
+
     mediaQueueList = findViewById(R.id.sample_list);
     ItemTouchHelper helper = new ItemTouchHelper(new RecyclerViewCallback());
     helper.attachToRecyclerView(mediaQueueList);
diff --git a/demos/cast/src/main/res/layout/main_activity.xml b/demos/cast/src/main/res/layout/main_activity.xml
index 81320bb75b..5778e6a2e4 100644
--- a/demos/cast/src/main/res/layout/main_activity.xml
+++ b/demos/cast/src/main/res/layout/main_activity.xml
@@ -27,6 +27,12 @@
       android:background="@android:color/black"
       app:repeat_toggle_modes="all|one"/>
 
+  <SeekBar android:id="@+id/volume_bar"
+    android:layout_width="match_parent"
+    android:layout_height="48dp"
+    android:min="0"
+    android:max="100"/>
+
   <RelativeLayout android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1">

demo_volume.patch

@AquilesCanta
Copy link
Contributor

I appreciate the diff but it's not necessary. I already have a similar local setup. What I was interested in learning was about the receiver device.

It's strange I also tried with a chromecast running Android TV. Also tried with a Pixel tablet. And in both cases setStreamVolume didn't do anything. Let me dig a bit deeper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants