Fix visualizer init

This commit is contained in:
Damontecres 2026-03-06 15:30:12 -05:00
parent e4422375ce
commit 7f75da2493
No known key found for this signature in database
7 changed files with 54 additions and 35 deletions

View file

@ -152,6 +152,15 @@ class AppPreferencesSerializer
slideshowDuration = AppPreference.SlideshowDuration.defaultValue
slideshowPlayVideos = AppPreference.SlideshowPlayVideos.defaultValue
}.build()
musicPreferences =
MusicPreferences
.newBuilder()
.apply {
showBackdrop = true
showLyrics = true
showAlbumArt = true
}.build()
}.build()
override suspend fun readFrom(input: InputStream): AppPreferences {

View file

@ -15,6 +15,7 @@ import com.github.damontecres.wholphin.preferences.updateHomePagePreferences
import com.github.damontecres.wholphin.preferences.updateInterfacePreferences
import com.github.damontecres.wholphin.preferences.updateLiveTvPreferences
import com.github.damontecres.wholphin.preferences.updateMpvOptions
import com.github.damontecres.wholphin.preferences.updateMusicPreferences
import com.github.damontecres.wholphin.preferences.updatePhotoPreferences
import com.github.damontecres.wholphin.preferences.updatePlaybackOverrides
import com.github.damontecres.wholphin.preferences.updateScreensaverPreferences
@ -263,4 +264,14 @@ suspend fun upgradeApp(
}
}
}
if (previous.isEqualOrBefore(Version.fromString("0.5.2-1=g0"))) {
appPreferences.updateData {
it.updateMusicPreferences {
showBackdrop = true
showLyrics = true
showAlbumArt = true
}
}
}
}

View file

@ -36,7 +36,7 @@ import timber.log.Timber
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.hours
@Singleton
class NavDrawerService
@ -101,7 +101,7 @@ class NavDrawerService
}
NowPlayingStatus.PAUSED -> {
delay(2.minutes)
delay(2.hours)
_state.update {
it.copy(
nowPlayingEnabled = false,

View file

@ -49,7 +49,6 @@ fun BoxScope.BarVisualizer(
.width(width)
.padding(start = if (index == 0) 0.dp else padding)
.background(MaterialTheme.colorScheme.border),
// .align(Alignment.Bottom),
)
}
}

View file

@ -168,9 +168,10 @@ fun NowPlayingOverlay(
Modifier
.fillMaxWidth()
.background(
color = MaterialTheme.colorScheme.surface.copy(alpha = .75f),
color = MaterialTheme.colorScheme.surface.copy(alpha = .5f),
shape = RoundedCornerShape(8.dp),
).onFocusChanged {
).padding(end = 8.dp)
.onFocusChanged {
if (it.hasFocus) showButtons = index < 3
controllerViewState.pulseControls()
}.animateItem(),

View file

@ -19,6 +19,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
@ -28,6 +29,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
@ -165,7 +167,8 @@ fun NowPlayingPage(
model = current?.imageUrl,
modifier =
Modifier
.height(320.dp),
.height(320.dp)
.clip(RoundedCornerShape(16.dp)),
)
current?.title?.let {
Text(

View file

@ -1,6 +1,5 @@
package com.github.damontecres.wholphin.ui.detail.music
import android.content.Context
import android.media.audiofx.Visualizer
import androidx.datastore.core.DataStore
import androidx.lifecycle.ViewModel
@ -13,9 +12,9 @@ import com.github.damontecres.wholphin.data.model.BaseItem
import com.github.damontecres.wholphin.preferences.AppPreference
import com.github.damontecres.wholphin.preferences.AppPreferences
import com.github.damontecres.wholphin.services.BackdropService
import com.github.damontecres.wholphin.services.ImageUrlService
import com.github.damontecres.wholphin.services.MusicService
import com.github.damontecres.wholphin.services.NavigationManager
import com.github.damontecres.wholphin.services.NowPlayingStatus
import com.github.damontecres.wholphin.services.UserPreferencesService
import com.github.damontecres.wholphin.ui.launchDefault
import com.github.damontecres.wholphin.ui.main.settings.MoveDirection
@ -25,7 +24,6 @@ import com.mayakapps.kache.InMemoryKache
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
@ -51,12 +49,10 @@ class NowPlayingViewModel
@AssistedInject
constructor(
private val api: ApiClient,
@param:ApplicationContext private val context: Context,
val navigationManager: NavigationManager,
private val musicService: MusicService,
private val imageUrlService: ImageUrlService,
private val backdropService: BackdropService,
private val preferencesDataStore: DataStore<AppPreferences>,
val navigationManager: NavigationManager,
val userPreferencesService: UserPreferencesService,
) : ViewModel(),
Visualizer.OnDataCaptureListener,
@ -66,15 +62,11 @@ class NowPlayingViewModel
fun create(): NowPlayingViewModel
}
private val visualizer by lazy {
Visualizer(player.audioSessionId).apply {
captureSize = Visualizer.getCaptureSizeRange()[1]
}
}
private var visualizer: Visualizer? = null
val controllerViewState =
ControllerViewState(
AppPreference.Companion.ControllerTimeout.defaultValue,
AppPreference.ControllerTimeout.defaultValue,
true,
)
@ -90,20 +82,32 @@ class NowPlayingViewModel
init {
player.addListener(this)
visualizer.setDataCaptureListener(
this,
Visualizer.getMaxCaptureRate() / 2,
true,
false,
)
visualizer.enabled = true
addCloseable {
player.removeListener(this)
visualizer.release()
visualizer?.release()
}
viewModelScope.launchDefault {
musicService.state.collectLatest { musicServiceState ->
if (musicServiceState.status != NowPlayingStatus.IDLE) {
viewModelScope.launchDefault {
if (visualizer == null) {
visualizer =
Visualizer(onMain { player.audioSessionId }).apply {
captureSize = Visualizer.getCaptureSizeRange()[1]
setDataCaptureListener(
this@NowPlayingViewModel,
Visualizer.getMaxCaptureRate() / 2,
true,
false,
)
}
}
visualizer?.enabled =
musicServiceState.status == NowPlayingStatus.PLAYING
}
}
state.update { it.copy(musicServiceState = musicServiceState) }
}
}
@ -265,18 +269,10 @@ class NowPlayingViewModel
val processed = IntArray(resolution)
val captureSize =
Visualizer.getCaptureSizeRange()[1]
// val groupSize = captureSize / resolution
val groupSize = captureSize / resolution.toFloat()
for (i in 0 until resolution) {
// processed[i] =
// waveform
// .map { abs(it.toInt()) }
// .map { it.toInt() }
// .subList(i * groupSize, min((i + 1) * groupSize, waveform.size))
// .average()
// .toInt()
val v = abs(waveform[ceil(i * groupSize).toInt()].toInt())
processed[i] = v * v / (128)
processed[i] = abs(v * v / (128) - 128)
}
viz.update { processed }
}