【问题标题】:Seekbar not moving with exoplayer androidSeekbar 不与 exoplayer android 一起移动
【发布时间】:2021-05-13 23:53:45
【问题描述】:

我正在构建一个音乐应用程序,使用 exoplayer 和音乐细节片段。 搜索栏不动。

这是当前的行为,(除非我通过触摸手动搜索,否则搜索栏不会移动)

SongViewModel

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()
}

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)
        }
    }
}

SongDetailsFragment

............
private val songViewModel: SongViewModel by viewModels()
.............

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
 binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.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 updatePlayingTime(ms: Long){
    val dateFormat = SimpleDateFormat("mm:ss", Locale.getDefault())
    binding.txtCurrentTime.text = dateFormat.format(ms)
   }

   //observe data here
   songViewModel.currentPlayerPosition.observe(viewLifecycleOwner){

        if (shouldUpdateSeekBar){
            binding.seekBar.progress = it.toInt()
            updatePlayingTime(it)
        }

    }

    songViewModel.currentSongDuration.observe(viewLifecycleOwner) {
        binding.seekBar.max = it.toInt()
        val dateFormat = SimpleDateFormat("mm:ss", 
     Locale.getDefault())
        binding.txtSongDuration.text = dateFormat.format(it)
    }

}

fragmentsongdetails.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" />

可能是什么原因? 让我知道是否需要添加更多代码 - MusicServiceConnection 类或 MusicService

【问题讨论】:

    标签: android android-studio kotlin exoplayer


    【解决方案1】:

    您应该在 xml 中将您的 currentSongDuration LiveData 绑定到 Seekbar max 和 Seekbar progress 中的 currentPlayerPosition。此外,您只需设置一次 _currentSongDuration,而不是在每次玩家位置更新时设置。

    【讨论】:

    • 我已经在我的片段中这样做了。请检查更新的问题。我在 SongDetailsFragment 中添加了代码“//在此处观察数据”。搜索栏不动
    【解决方案2】:

    首先使用 PlayerControlView 并根据您的需要设计您的控件请使用 音乐播放器的默认 exoplayer seekbar/DefaultTimeBar

    <com.google.android.exoplayer2.ui.DefaultTimeBar
       android:id="@id/exo_progress"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:tag="tint_accent_color" />
    

    像这样绑定你的 SimpleExoPlayer 对象

    public class AudioPlayerServiceBinder extends Binder {
        public SimpleExoPlayer getPlayer() {
            return player;
        } 
    }
    

    在您的 Activity 或 Fragment 中绑定您的 SimpleExoPlayer 对象

    private final ServiceConnection connection = new ServiceConnection() {
       @Override
       public void onServiceConnected(ComponentName name, IBinder service) {
       binder = (AudioPlayerService.AudioPlayerServiceBinder) service;
       serviceBound = true;
       player = binder.getPlayer();
    
    
       preview.setPlayer(player); //preview is the object of your custom control object
    
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
       serviceBound = false;
       }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多