gpt4 book ai didi

android 10 在通知上搜索栏

转载 作者:行者123 更新时间:2023-11-30 04:54:02 30 4
gpt4 key购买 nike

有谁知道android 10中通知的seekbar功能吗?

我似乎无法让位置指示器显示当前位置。该栏只是空白,但我可以通过单击通知中的栏来查找媒体。

我有正常的通知生成器代码,我添加了这个代码,使搜索栏在 libvlc 的“MediaPlayer.Event.Playing”事件中可点击

MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();


metadataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, mMediaPlayer.getLength());

PlaybackStateCompat.Builder mStateBuilder = new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING, 1, 1.0f)
.setBufferedPosition(mMediaPlayer.getLength())
.setActions(
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_SEEK_TO |
PlaybackStateCompat.ACTION_PLAY_PAUSE);


mediaSession.setMetadata(metadataBuilder.build());
mediaSession.setPlaybackState(mStateBuilder.build());

媒体播放大约一半,但没有指示其位置。

notification

最佳答案

找了半天终于找到解决办法了。请按照以下步骤在 Android 10 或更高版本的通知中显示 SeekBar。

  • 第 1 步:将文件的持续时间(以毫秒为单位)作为 Long 值,并在 中使用键 MediaMetadataCompat.METADATA_KEY_DURATION MediaSession.setMetadata
mediaSession.setMetadata(MediaMetadataCompat.Builder()
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, resource)
.putString(
MediaMetadataCompat.METADATA_KEY_ARTIST,
activeBookFile.title
)
.putString(
MediaMetadataCompat.METADATA_KEY_ALBUM,
AudioPlayerService.activeBook.title
)
.putString(
MediaMetadataCompat.METADATA_KEY_TITLE,
AudioPlayerService.activeBook.title
)
.putLong(
MediaMetadataCompat.METADATA_KEY_DURATION,
activeBookFile.duration
)
.build()
)
  • 第 2 步:将 ACTION_SEEK_TO 添加到所有 PlaybackStateCompat.setAction() 方法
  • 第 3 步:提供以 毫秒 为单位的有效 currentPosition 和播放项目的当前状态,然后将它们传递给PlaybackStateCompat.setState() 方法。
  • 第 4 步:使用 setPlaybackState()
  • 将 playBackState 对象设置为您的 mediaSession
fun updateMediaPlaybackState(currentPos: Long) {
mediaSession.setPlaybackState(
PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY
or PlaybackStateCompat.ACTION_PLAY_PAUSE
or PlaybackStateCompat.ACTION_PAUSE
or PlaybackStateCompat.ACTION_SKIP_TO_NEXT
or PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
or PlaybackStateCompat.ACTION_SEEK_TO
)
.setState(
getPlaybackState(),
currentPos,
1f,
SystemClock.elapsedRealtime()
)
.build()
)
}
@PlaybackStateCompat.State
private fun getPlaybackState(): Int {
return when (currentState) {
is PlayerStoppedState -> PlaybackStateCompat.STATE_STOPPED
is PlayerPausedState -> PlaybackStateCompat.STATE_PAUSED
is PlayerResumingState -> PlaybackStateCompat.STATE_PLAYING
is PlayerLoadingState, is PlayerBufferingState -> PlaybackStateCompat.STATE_BUFFERING
is PlayerErrorState -> PlaybackStateCompat.STATE_ERROR
else -> PlaybackStateCompat.STATE_NONE
}
}
  • 第 5 步:支持用户触摸 SeekBar 覆盖 MediaSessionCompat.Callback() 中的 onSeekTo(pos: Long) 方法
class PlayerMediaSessionCallback(mediaPlayer: MediaPlayer?) : MediaSessionCompat.Callback() {

// other callback methods

override fun onSeekTo(pos: Long) {
mediaPlayer?.seekTo(pos)
}
}
  • 注意:每当您的播放器(播放、暂停、跳过下一步)发生状态变化时,请记住调用updateMediaPlaybackState()showNotification() , skipPrevious, seek 等)

关于android 10 在通知上搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59516981/

30 4 0