Setup equalizer

This commit is contained in:
Naveen 2023-08-20 20:59:43 +05:30
parent 2c0dad101c
commit 709c2205e5
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
5 changed files with 69 additions and 20 deletions

View file

@ -1,7 +1,6 @@
package com.simplemobiletools.musicplayer.activities package com.simplemobiletools.musicplayer.activities
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.media.MediaPlayer
import android.media.audiofx.Equalizer import android.media.audiofx.Equalizer
import android.os.Bundle import android.os.Bundle
import android.widget.SeekBar import android.widget.SeekBar
@ -16,10 +15,11 @@ import com.simplemobiletools.commons.views.MySeekBar
import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.config import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.helpers.EQUALIZER_PRESET_CUSTOM import com.simplemobiletools.musicplayer.helpers.EQUALIZER_PRESET_CUSTOM
import com.simplemobiletools.musicplayer.services.MusicService import com.simplemobiletools.musicplayer.services.playback.SimpleEqualizer
import kotlinx.android.synthetic.main.activity_equalizer.* import kotlinx.android.synthetic.main.activity_equalizer.*
import kotlinx.android.synthetic.main.equalizer_band.view.* import kotlinx.android.synthetic.main.equalizer_band.view.*
import java.text.DecimalFormat import java.text.DecimalFormat
import kotlin.math.roundToInt
class EqualizerActivity : SimpleActivity() { class EqualizerActivity : SimpleActivity() {
private var bands = HashMap<Short, Int>() private var bands = HashMap<Short, Int>()
@ -42,15 +42,12 @@ class EqualizerActivity : SimpleActivity() {
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun initMediaPlayer() { private fun initMediaPlayer() {
val equalizer = MusicService.mEqualizer ?: run { val equalizer = SimpleEqualizer.instance
val audioSessionId = MusicService.mPlayer?.getAudioSessionId() ?: MediaPlayer().audioSessionId
Equalizer(0, audioSessionId)
}
try { try {
if (!equalizer.enabled) { if (!equalizer.enabled) {
equalizer.enabled = true equalizer.enabled = true
} }
} catch (e: IllegalStateException) { } catch (ignored: IllegalStateException) {
} }
setupBands(equalizer) setupBands(equalizer)
@ -65,6 +62,7 @@ class EqualizerActivity : SimpleActivity() {
equalizer_preset.setTextColor(presetTextColor) equalizer_preset.setTextColor(presetTextColor)
} }
@SuppressLint("SetTextI18n")
private fun setupBands(equalizer: Equalizer) { private fun setupBands(equalizer: Equalizer) {
val minValue = equalizer.bandLevelRange[0] val minValue = equalizer.bandLevelRange[0]
val maxValue = equalizer.bandLevelRange[1] val maxValue = equalizer.bandLevelRange[1]
@ -92,16 +90,16 @@ class EqualizerActivity : SimpleActivity() {
this.equalizer_band_seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { this.equalizer_band_seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
if (fromUser) { if (fromUser) {
val newProgress = Math.round(progress / 100.0) * 100 val newProgress = (progress / 100.0).roundToInt() * 100
this@apply.equalizer_band_seek_bar.progress = newProgress.toInt() this@apply.equalizer_band_seek_bar.progress = newProgress
val newValue = newProgress + minValue val newValue = newProgress + minValue
try { try {
if ((MusicService.mEqualizer ?: equalizer).getBandLevel(band.toShort()) != newValue.toShort()) { if (equalizer.getBandLevel(band.toShort()) != newValue.toShort()) {
(MusicService.mEqualizer ?: equalizer).setBandLevel(band.toShort(), newValue.toShort()) equalizer.setBandLevel(band.toShort(), newValue.toShort())
bands[band.toShort()] = newValue.toInt() bands[band.toShort()] = newValue
} }
} catch (e: Exception) { } catch (ignored: Exception) {
} }
} }
} }
@ -169,10 +167,10 @@ class EqualizerActivity : SimpleActivity() {
bandSeekBars[band].progress = progress!!.toInt() bandSeekBars[band].progress = progress!!.toInt()
val newValue = progress + minValue val newValue = progress + minValue
(MusicService.mEqualizer ?: equalizer).setBandLevel(band.toShort(), newValue.toShort()) equalizer.setBandLevel(band.toShort(), newValue.toShort())
} }
} else { } else {
val presetName = (MusicService.mEqualizer ?: equalizer).getPresetName(presetId.toShort()) val presetName = equalizer.getPresetName(presetId.toShort())
if (presetName.isEmpty()) { if (presetName.isEmpty()) {
config.equalizerPreset = EQUALIZER_PRESET_CUSTOM config.equalizerPreset = EQUALIZER_PRESET_CUSTOM
equalizer_preset.text = getString(R.string.custom) equalizer_preset.text = getString(R.string.custom)
@ -180,11 +178,11 @@ class EqualizerActivity : SimpleActivity() {
equalizer_preset.text = presetName equalizer_preset.text = presetName
} }
(MusicService.mEqualizer ?: equalizer).usePreset(presetId.toShort()) equalizer.usePreset(presetId.toShort())
val lowestBandLevel = (MusicService.mEqualizer ?: equalizer).bandLevelRange?.get(0) val lowestBandLevel = equalizer.bandLevelRange?.get(0)
for (band in 0 until (MusicService.mEqualizer ?: equalizer).numberOfBands) { for (band in 0 until equalizer.numberOfBands) {
val level = (MusicService.mEqualizer ?: equalizer).getBandLevel(band.toShort()).minus(lowestBandLevel!!) val level = equalizer.getBandLevel(band.toShort()).minus(lowestBandLevel!!)
bandSeekBars[band].progress = level bandSeekBars[band].progress = level
} }
} }

View file

@ -42,6 +42,7 @@ class PlaybackService : MediaLibraryService() {
releaseMediaSession() releaseMediaSession()
clearListener() clearListener()
stopSleepTimer() stopSleepTimer()
SimpleEqualizer.release()
} }
private fun initializeLibrary() { private fun initializeLibrary() {

View file

@ -0,0 +1,47 @@
package com.simplemobiletools.musicplayer.services.playback
import android.content.Context
import android.media.audiofx.Equalizer
import androidx.media3.common.util.UnstableApi
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.helpers.EQUALIZER_PRESET_CUSTOM
import com.simplemobiletools.musicplayer.services.playback.player.SimpleMusicPlayer
@UnstableApi
object SimpleEqualizer {
lateinit var instance: Equalizer
internal set
fun setupEqualizer(context: Context, player: SimpleMusicPlayer) {
try {
val preset = context.config.equalizerPreset
instance = Equalizer(0, player.exoPlayer.audioSessionId)
if (!instance.enabled) {
instance.enabled = true
}
if (preset != EQUALIZER_PRESET_CUSTOM) {
instance.usePreset(preset.toShort())
} else {
val minValue = instance.bandLevelRange[0]
val bandType = object : TypeToken<HashMap<Short, Int>>() {}.type
val equalizerBands = Gson().fromJson<HashMap<Short, Int>>(context.config.equalizerBands, bandType) ?: HashMap()
for ((key, value) in equalizerBands) {
val newValue = value + minValue
if (instance.getBandLevel(key) != newValue.toShort()) {
instance.setBandLevel(key, newValue.toShort())
}
}
}
} catch (ignored: Exception) {
context.toast(R.string.unknown_error_occurred)
}
}
fun release() = instance.release()
}

View file

@ -17,6 +17,7 @@ import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.setRepeatMode import com.simplemobiletools.musicplayer.extensions.setRepeatMode
import com.simplemobiletools.musicplayer.helpers.SEEK_INTERVAL_MS import com.simplemobiletools.musicplayer.helpers.SEEK_INTERVAL_MS
import com.simplemobiletools.musicplayer.services.playback.PlaybackService import com.simplemobiletools.musicplayer.services.playback.PlaybackService
import com.simplemobiletools.musicplayer.services.playback.SimpleEqualizer
import com.simplemobiletools.musicplayer.services.playback.getCustomLayout import com.simplemobiletools.musicplayer.services.playback.getCustomLayout
import com.simplemobiletools.musicplayer.services.playback.getMediaSessionCallback import com.simplemobiletools.musicplayer.services.playback.getMediaSessionCallback
@ -37,6 +38,7 @@ internal fun PlaybackService.initializeSessionAndPlayer(handleAudioFocus: Boolea
setPlaybackSpeed(config.playbackSpeed) setPlaybackSpeed(config.playbackSpeed)
shuffleModeEnabled = config.isShuffleEnabled shuffleModeEnabled = config.isShuffleEnabled
mediaSession.setCustomLayout(getCustomLayout()) mediaSession.setCustomLayout(getCustomLayout())
SimpleEqualizer.setupEqualizer(this@initializeSessionAndPlayer, player)
} }
} }

View file

@ -3,9 +3,10 @@ package com.simplemobiletools.musicplayer.services.playback.player
import androidx.media3.common.ForwardingPlayer import androidx.media3.common.ForwardingPlayer
import androidx.media3.common.Player import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
@UnstableApi @UnstableApi
class SimpleMusicPlayer(player: Player) : ForwardingPlayer(player) { class SimpleMusicPlayer(val exoPlayer: ExoPlayer) : ForwardingPlayer(exoPlayer) {
/** /**
* The default implementation only advertises the seek to next and previous item in the case * The default implementation only advertises the seek to next and previous item in the case