feat(cast): add volume control to custom cast controller dialog

- Integrated a SeekBar for volume adjustment in the cast connection dialog.
- Implemented a Cast listener to synchronize volume state between the remote device and the UI.
- Added volume icons and updated the dialog layout to include the control bar.
- Managed listener registration and UI updates within the dialog lifecycle.
This commit is contained in:
Michatec committed 2026-08-16 20:31:45 +02:00
1 parent 7519153037
commit ef9a607dbe
4 files changed
+91 -2

No files matched your search

@@ -3,8 +3,11 @@ package com.michatec.radio.ui
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.SeekBar
import androidx.mediarouter.app.MediaRouteControllerDialogFragment
import com.google.android.gms.cast.Cast
import com.google.android.gms.cast.framework.CastContext
import com.google.android.gms.cast.framework.CastSession
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.michatec.radio.R
import com.michatec.radio.databinding.DialogCastConnectedBinding
@@ -17,6 +20,13 @@ class CustomCastControllerDialogFragment : MediaRouteControllerDialogFragment()
private var _binding: DialogCastConnectedBinding? = null
private val binding get() = _binding!!
private var castSession: CastSession? = null
private val castListener = object : Cast.Listener() {
override fun onVolumeChanged() {
updateVolumeSeekBar()
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val context = requireContext()
@@ -24,11 +34,23 @@ class CustomCastControllerDialogFragment : MediaRouteControllerDialogFragment()
_binding = DialogCastConnectedBinding.inflate(LayoutInflater.from(context))
val castContext = CastContext.getSharedInstance(context)
val castSession = castContext.sessionManager.currentCastSession
castSession = castContext.sessionManager.currentCastSession
val deviceName = castSession?.castDevice?.friendlyName ?: context.getString(R.string.media_route_menu_title)
binding.textViewDeviceName.text = deviceName
updateVolumeSeekBar()
binding.seekBarVolume.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
castSession?.volume = progress / 100.0
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})
val dialog = MaterialAlertDialogBuilder(context)
.setView(binding.root)
.create()
@@ -45,6 +67,23 @@ class CustomCastControllerDialogFragment : MediaRouteControllerDialogFragment()
return dialog
}
override fun onStart() {
super.onStart()
castSession?.addCastListener(castListener)
updateVolumeSeekBar()
}
override fun onStop() {
super.onStop()
castSession?.removeCastListener(castListener)
}
private fun updateVolumeSeekBar() {
val volume = castSession?.volume ?: 0.0
binding.seekBarVolume.progress = (volume * 100).toInt()
binding.seekBarVolume.isEnabled = castSession?.isMute == false
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M18.5,12c0,-1.77 -1.02,-3.29 -2.5,-4.03v8.05c1.48,-0.73 2.5,-2.25 2.5,-4.02zM5,9v6h4l5,5V4L9,9H5z"/>
</vector>
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M3,9v6h4l5,5V4L7,9H3zm13.5,3c0,-1.77 -1.02,-3.29 -2.5,-4.03v8.05c1.48,-0.73 2.5,-2.25 2.5,-4.02zM14,3.23v2.06c2.89,0.86 5,3.54 5,6.71s-2.11,5.85 -5,6.71v2.06c4.01,-0.91 7,-4.49 7,-8.77s-2.99,-7.86 -7,-8.77z"/>
</vector>
@@ -29,12 +29,42 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="24dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:textAppearance="?attr/textAppearanceBodyLarge"
android:textColor="?attr/colorOnSurfaceVariant"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@null"
android:src="@drawable/ic_volume_down_24dp"
app:tint="?attr/colorOnSurfaceVariant" />
<SeekBar
android:id="@+id/seekBarVolume"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="100" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@null"
android:src="@drawable/ic_volume_up_24dp"
app:tint="?attr/colorOnSurfaceVariant" />
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonStopCasting"
style="@style/Widget.Material3.Button"