Created
July 22, 2025 08:08
-
-
Save CodeBoy722/594d6f966c2092c32984eaab29584121 to your computer and use it in GitHub Desktop.
Kotling Compose Equalizer setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun EqualizerUI(equalizer: Equalizer) { | |
val bandCount = equalizer.numberOfBands.toInt() | |
val bandLevels = remember { mutableStateListOf<Short>() } | |
LaunchedEffect(Unit) { | |
bandLevels.clear() | |
repeat(bandCount) { | |
bandLevels.add(equalizer.getBandLevel(it.toShort())) | |
} | |
} | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(16.dp), | |
verticalArrangement = Arrangement.spacedBy(24.dp) | |
) { | |
Text("Equalizer", style = MaterialTheme.typography.headlineMedium) | |
repeat(bandCount) { band -> | |
val bandLabel = "${equalizer.getCenterFreq(band.toShort()) / 1000} Hz" | |
val min = equalizer.bandLevelRange[0] | |
val max = equalizer.bandLevelRange[1] | |
Column { | |
Text(bandLabel) | |
Slider( | |
value = bandLevels[band].toFloat(), | |
valueRange = min.toFloat()..max.toFloat(), | |
onValueChange = { | |
bandLevels[band] = it.toInt().toShort() | |
equalizer.setBandLevel(band.toShort(), it.toInt().toShort()) | |
} | |
) | |
} | |
} | |
val presets = (0 until equalizer.numberOfPresets).map { | |
equalizer.getPresetName(it.toShort()) | |
} | |
var selectedPreset by remember { mutableStateOf("") } | |
DropdownMenuBox( | |
presets = presets, | |
onPresetSelected = { | |
val index = presets.indexOf(it) | |
if (index >= 0) { | |
equalizer.usePreset(index.toShort()) | |
selectedPreset = it | |
} | |
}, | |
selected = selectedPreset | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AndroidEntryPoint | |
class EqualizerFragment : Fragment() { | |
private var equalizer: Equalizer? = null | |
private var audioSessionId: Int = 0 | |
private var _binding: FragmentEqualizerBinding? = null | |
private val binding get() = _binding!! | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View { | |
_binding = FragmentEqualizerBinding.inflate(inflater, container, false) | |
return binding.root | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
// Get audio session ID from ExoPlayer (e.g., set in your player setup) | |
val player = (requireActivity() as YourPlayerHolderActivity).exoPlayer | |
audioSessionId = player.audioSessionId | |
equalizer = Equalizer(0, audioSessionId).apply { | |
enabled = true | |
} | |
binding.composeView.setContent { | |
MaterialTheme { | |
EqualizerUI(equalizer!!) | |
} | |
} | |
} | |
override fun onDestroyView() { | |
super.onDestroyView() | |
equalizer?.release() | |
_binding = null | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/equalizer_container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<androidx.compose.ui.platform.ComposeView | |
android:id="@+id/compose_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment