【发布时间】:2021-06-06 23:38:27
【问题描述】:
我正在使用 ExoPlayer 构建一个音乐应用程序。
我遇到了 seekBar 的问题,它在播放歌曲时不动..
我可以搜索歌曲的特定部分,但搜索栏上不显示进度。
songDetails.xml
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="0dp"
android:layout_height="18dp"
android:layout_marginStart="10dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.35"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ic_like"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/txt_current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:includeFontPadding="false"
android:text="00:00"
android:textColor="@color/app_name_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/seek_bar"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/seek_bar"
app:layout_constraintTop_toBottomOf="@+id/seek_bar"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/txt_song_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:includeFontPadding="false"
android:text="00:00"
android:textColor="@color/app_name_color"
app:layout_constraintBottom_toBottomOf="@+id/txt_current_time"
app:layout_constraintEnd_toEndOf="@+id/seek_bar"
app:layout_constraintHorizontal_bias="0.996"
app:layout_constraintStart_toEndOf="@+id/txt_current_time"
app:layout_constraintTop_toTopOf="@+id/txt_current_time"
app:layout_constraintVertical_bias="1.0" />
SongDetailsViewModel
class SongViewModel @ViewModelInject constructor(musicServiceConnection: MusicServiceConnection) : ViewModel() {
private val playBackState = musicServiceConnection.playbackState
private val _currentSongDuration = MutableLiveData<Long>()
val currentSongDuration: LiveData<Long> = _currentSongDuration
private val _currentPlayerPosition = MutableLiveData<Long>()
val currentPlayerPosition: LiveData<Long> =
_currentPlayerPosition
init {
updateCurrentPlayerPosition()
}
private fun updateCurrentPlayerPosition() {
viewModelScope.launch {
while (true){
val position = playBackState.value?.currentPlaybackPosition
if (currentPlayerPosition.value != position){
_currentPlayerPosition.postValue(position!!)
_currentSongDuration.postValue(MusicService.curSongDuration)
}
delay(UPDATE_PLAYER_POSITION_INTERVAL)
}
}
}
}
歌曲片段
@AndroidEntryPoint
class SongDetailsFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
subscribeToObservers()
binding.seekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
updatePlayingTime(progress.toLong())
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
shouldUpdateSeekBar = false
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
binding.seekBar.let {
mainViewModel.seekTo(it.progress.toLong())
shouldUpdateSeekBar = true
}
}
})
}
private fun subscribeToObservers() {
mainViewModel.currentlyPlayingSong.observe(viewLifecycleOwner) {
if (it == null) return@observe
currentPlayingSong = it.toSong()
val formattedDuration = it.toSong()?.duration?.let { Utility.getSongDuration(true, it) }
binding.txtSongDuration.text = formattedDuration
}
}
mainViewModel.playBackState.observe(viewLifecycleOwner) {
playbackState = it
binding.seekBar.progress = it?.position?.toInt() ?: 0}
songViewModel.currentPlayerPosition.observe(viewLifecycleOwner){
binding.seekBar.progress = it.toInt()
if (shouldUpdateSeekBar){
binding.seekBar.progress = it.toInt()
updatePlayingTime(it)
}
}
songViewModel.currentSongDuration.observe(viewLifecycleOwner) {
binding.seekBar.max = it.toInt()
}
}
private fun updatePlayingTime(ms: Long){
val dateFormat = SimpleDateFormat("mm:ss", Locale.getDefault())
binding.txtCurrentTime.text = dateFormat.format(ms)
}
我已经坚持了好几天了..不知道问题可能出在哪里。除非您寻找,否则在片段中未观察/更新当前歌曲位置。
搜索栏不动!帮助!
【问题讨论】:
标签: android kotlin exoplayer2.x android-seekbar