如何从媒体播放器检索当前持续时间以显示在歌曲进度条上?

问题描述 投票:0回答:1

我已经尝试了所有可能的步骤,但只给出了一次价值,我希望它应该每秒更新一次。这是我的代码,我哪里做错了?

我想实现音乐进度滑块,有人可以帮助我吗?

这里我使用一个滑块库,您该库接受从当前位置到总持续时间的范围。

data class MusicPlayerStates(
    var playingSongCurrentPosition: MutableState<Int> = mutableIntStateOf(0),
    var playingSongDuration: MutableState<Int> = mutableIntStateOf(0),
    
    //other code
)

fun onEvent(event: MusicPlayerUiEvents) {
        when (event) {

            is MusicPlayerUiEvents.PlaySong -> {
                mediaPlayer?.let {
                    if (it.isPlaying) {
                        mediaPlayer?.stop()
                        mediaPlayer?.reset()
                        _musicPlayerState.update { state ->
                            state.copy(
                                playingSongCurrentPosition = state.playingSongCurrentPosition.apply {
                                    this.value = 0
                                },
                                playingSongDuration = state.playingSongDuration.apply {
                                    this.value = 0
                                }
                            )
                        }
                    }
                }
                _musicPlayerState.update {
                    it.copy(
                        isSongPlaying = it.isSongPlaying.apply {
                            this.value = true
                        }
                    )
                }
                mediaPlayer?.release()
                mediaPlayer = MediaPlayer().apply {
                    setDataSource(event.url)
                    prepareAsync()
                }
                mediaPlayer?.setOnPreparedListener { mediaPlayer ->
                    mediaPlayer.seekTo(state.value.playingSongCurrentPosition.value)
                    mediaPlayer.start()
                    setSongDuration(mediaPlayer.duration)
                    updatePlaybackState(mediaPlayer.currentPosition)
                    
               Log.d("check for currentD_VM","${state.value.playingSongCurrentPosition.value}")
                }

                mediaPlayer?.setOnCompletionListener { mediaPlayer ->
                    // Use for precise updates
                    mediaPlayer?.stop()
                    _musicPlayerState.update { state ->
                        state.copy(
                            playingSongCurrentPosition = state.playingSongCurrentPosition.apply {
                                this.value = 0
                            },
                            playingSongDuration = state.playingSongDuration.apply {
                                this.value = 0
                            },
                            isSongPlaying = state.isSongPlaying.apply {
                                this.value = false
                            },
                        )
                    }
                }

            }
            }
         }
     }
    private fun updatePlaybackState(currentPosition: Int) {
        _musicPlayerState.update {
            it.copy(
                playingSongCurrentPosition = it.playingSongCurrentPosition.apply {
                    this.value = currentPosition
                }
            )
        }
    }

    private fun setSongDuration(duration: Int) {
        _musicPlayerState.update {
            it.copy(
                playingSongDuration = it.playingSongDuration.apply {
                    this.value = duration
                }
            )
        }
    }
 Box(
            modifier =
            Modifier
                .padding(vertical = 80.dp, horizontal = 20.dp)
                .fillMaxWidth()
                .height(20.dp)
        ) {
            var fraction by remember { mutableFloatStateOf(1f) }
            WavySlider(
                valueRange = 1000f..state.playingSongDuration.value.toFloat(),
                value = 1000f,
                onValueChange = { },
                waveLength = 25.dp,     // Set this to 0.dp to get a regular Slider
                waveHeight = 10.dp,     // Set this to 0.dp to get a regular Slider
                waveVelocity = 15.dp to WaveDirection.HEAD, // Speed per second and its direction
                waveThickness = 4.dp,   // Defaults to the specified trackThickness
                trackThickness = 4.dp,  // Defaults to 4.dp, same as regular Slider
                incremental = false,    // Whether to gradually increase waveHeight
                // animationSpecs = ... // Customize various animations of the wave
            )
        }

android kotlin mvvm android-jetpack-compose android-mediaplayer
1个回答
0
投票

看起来您正在将滑块的值设置为固定值 1000f,这将使滑块值固定在 1000f。

在你的 WavySlider 中将值设置为,

WavySlider(
    valueRange = 0f..state.playingSongDuration.value.toFloat(),
    value = state.playingSongCurrentPosition.value.toFloat(),

这里您的滑块值会根据音乐播放的当前位置动态更新。

© www.soinside.com 2019 - 2024. All rights reserved.