public class PlayerViewModel
extends androidx.lifecycle.ViewModel
注意!使用前必须先调用 PlayerViewModel
对象的任意一个 init 方法进行初始化。
构造器和说明 |
---|
PlayerViewModel() |
限定符和类型 | 方法和说明 |
---|---|
void |
cancelProgressClock()
停止实时跟新播放进度。
|
void |
cancelSleepTimer()
取消睡眠定时器。
|
void |
fastForward()
快进。
|
androidx.lifecycle.LiveData<java.lang.String> |
getAlbum()
正在播放的歌曲所属的专辑。
|
androidx.lifecycle.LiveData<java.lang.String> |
getArtist()
正在播放的歌曲的艺术家(歌手)。
|
androidx.lifecycle.LiveData<java.lang.Integer> |
getBufferedProgress()
正在播放歌曲的缓存进度(单位:秒)。
|
androidx.lifecycle.LiveData<java.lang.Boolean> |
getConnected()
获取客户端的连接结果。
|
androidx.lifecycle.LiveData<java.lang.Integer> |
getDuration()
正在播放的歌曲的持续时间(单位:秒)。
|
androidx.lifecycle.LiveData<java.lang.String> |
getErrorMessage()
错误信息,只在发生错误时该值才有意义。
|
androidx.lifecycle.LiveData<java.lang.String> |
getIconUri()
正在播放的歌曲的图标的 Uri
|
androidx.lifecycle.LiveData<PlaybackState> |
getPlaybackState()
播放器的播放状态。
|
PlayerClient |
getPlayerClient()
获取当前
PlayerViewModel 关联到的 PlayerClient 对象。 |
androidx.lifecycle.LiveData<MusicItem> |
getPlayingMusicItem()
获取当前只在播放的音乐。
|
androidx.lifecycle.LiveData<java.lang.Boolean> |
getPlayingNoStalled()
当前播放器状态为
PlaybackState.PLAYING 并且播放器没有处于 stalled 状态。 |
androidx.lifecycle.LiveData<PlayMode> |
getPlayMode()
播放器的播放模式。
|
androidx.lifecycle.LiveData<java.lang.Integer> |
getPlayPosition()
正在播放歌曲在列表中的位置。
|
androidx.lifecycle.MutableLiveData<java.lang.Integer> |
getPlayProgress()
正在播放歌曲的实时播放进度(单位:秒),支持双向绑定。
|
androidx.lifecycle.LiveData<java.lang.Boolean> |
getPreparing()
播放器是否正在准备中。
|
androidx.lifecycle.LiveData<java.lang.Integer> |
getSleepTimerProgress()
获取睡眠定时器进度(单位:秒)。
|
androidx.lifecycle.LiveData<java.lang.Boolean> |
getSleepTimerStarted()
睡眠定时器是否已启动。
|
androidx.lifecycle.LiveData<java.lang.Integer> |
getSleepTimerTime()
获取睡眠定时器的时长(单位:秒)。
|
androidx.lifecycle.LiveData<java.lang.Float> |
getSpeed()
播放器播放速度。
|
androidx.lifecycle.LiveData<java.lang.Boolean> |
getStalled()
播放器是否处于 stalled 状态。
|
androidx.lifecycle.LiveData<java.lang.String> |
getTextDuration()
获取歌曲的持续时间(单位:秒)对应的文本值,例如 82 秒对应的文本值为 "01:22"
|
androidx.lifecycle.LiveData<java.lang.String> |
getTextPlayProgress()
获取歌曲的实时播放进度(单位:秒)对应的文本值,例如 82 秒对应的文本值为 "01:22"
|
androidx.lifecycle.LiveData<java.lang.String> |
getTextSleepTimerProgress()
获取睡眠定时器进度(单位:秒)对应的文本值,例如 82 秒对应的文本值为 "01:22"
睡眠定时器的进度是个倒计时。
|
androidx.lifecycle.LiveData<java.lang.String> |
getTextSleepTimerTime()
获取睡眠定时器的时长(单位:秒)对应的文本值,例如 82 秒对应的文本值为 "01:22"
|
androidx.lifecycle.LiveData<java.lang.String> |
getTitle()
正在播放的歌曲的标题。
|
void |
init(android.content.Context context,
PlayerClient playerClient)
初始化
PlayerViewModel 对象。 |
void |
init(android.content.Context context,
PlayerClient playerClient,
boolean enableProgressClock)
初始化
PlayerViewModel 对象。 |
void |
init(PlayerClient playerClient,
java.lang.String defaultTitle,
java.lang.String defaultArtist,
java.lang.String defaultAlbum)
初始化
PlayerViewModel 对象。 |
void |
init(PlayerClient playerClient,
java.lang.String defaultTitle,
java.lang.String defaultArtist,
java.lang.String defaultAlbum,
boolean enableProgressClock)
初始化
PlayerViewModel 对象。 |
androidx.lifecycle.LiveData<java.lang.Boolean> |
isError()
是否发生了错误。
|
boolean |
isInitialized()
是否已完成初始化。
|
protected void |
onCleared() |
protected void |
onInitialized()
该方法会在
PlayerViewModel 初始化完成后调用。 |
void |
onStartTrackingTouch(android.widget.SeekBar seekBar)
DataBinding 框架与 SeekBar 专用。
|
void |
onStopTrackingTouch(android.widget.SeekBar seekBar)
DataBinding 框架与 SeekBar 专用。
|
void |
pause()
暂停
|
void |
play()
播放
|
void |
playPause()
播放/暂停
|
void |
playPause(int position)
播放/暂停 position 位置处的音乐。
|
void |
rewind()
快退。
|
void |
seekTo(int progress)
调整音乐播放进度(单位:毫秒)。
|
void |
setAutoDisconnect(boolean autoDisconnect)
设置是否在 ViewModel 被清理时自动断开 PlayerClient 的连接。
|
void |
setNextPlay(MusicItem musicItem)
设置指定歌曲 “下一次播放”。
|
void |
setPlaylist(Playlist playlist)
设置播放列表。
|
void |
setPlaylist(Playlist playlist,
boolean play)
设置播放列表,并决定是否立即播放列表的第 1 首音乐。
|
void |
setPlaylist(Playlist playlist,
int position,
boolean play)
设置播放列表,并决定是否立即播放列表的 position 位置处音乐。
|
void |
setPlayMode(PlayMode playMode)
设置播放模式。
|
void |
setSpeed(float speed)
设置播放速度。
|
void |
skipToNext()
下一曲。
|
void |
skipToPosition(int position)
播放 position 处的音乐,如果播放列表中 position 处的音乐是当前正在播放的音乐,则忽略本次调用。
|
void |
skipToPrevious()
上一曲
|
void |
startSleepTimer(long time)
启动睡眠定时器。
|
void |
startSleepTimer(long time,
SleepTimer.TimeoutAction action)
启动睡眠定时器。
|
void |
stop()
停止
|
public void init(@NonNull android.content.Context context, @NonNull PlayerClient playerClient)
PlayerViewModel
对象。
默认启用了进度条时钟(用于实时更新播放进度)。
默认标题为 "未知标题";默认歌手为 "未知歌手";默认专辑为 "未知专辑"。这些默认值会在正在播放的
MusicItem
对应的字段为空时展示。
context
- Context 对象,不能为 nullplayerClient
- PlayerClient 对象,不能为 nullpublic void init(@NonNull android.content.Context context, @NonNull PlayerClient playerClient, boolean enableProgressClock)
PlayerViewModel
对象。
默认标题为 "未知标题";默认歌手为 "未知歌手";默认专辑为 "未知专辑"。这些默认值会在正在播放的
MusicItem
对应的字段为空时展示。
context
- Context 对象,不能为 nullplayerClient
- PlayerClient 对象,不能为 nullenableProgressClock
- 是否启用进度条时钟(用于实时更新播放进度)public void init(@NonNull PlayerClient playerClient, @NonNull java.lang.String defaultTitle, @NonNull java.lang.String defaultArtist, @NonNull java.lang.String defaultAlbum)
PlayerViewModel
对象。
默认启用了进度条时钟(用于实时更新播放进度)。
playerClient
- PlayerClient 对象,不能为 nulldefaultTitle
- 默认标题,会在正在播放的歌曲的标题为空时展示,不能为 nulldefaultArtist
- 默认艺术家,会在正在播放的歌曲的艺术家为空时展示,不能为 nulldefaultAlbum
- 默认专辑,会在正在播放的歌曲的专辑为空时展示,不能为 nullpublic void init(@NonNull PlayerClient playerClient, @NonNull java.lang.String defaultTitle, @NonNull java.lang.String defaultArtist, @NonNull java.lang.String defaultAlbum, boolean enableProgressClock)
PlayerViewModel
对象。playerClient
- PlayerClient 对象,不能为 nulldefaultTitle
- 默认标题,会在正在播放的歌曲的标题为空时展示,不能为 nulldefaultArtist
- 默认艺术家,会在正在播放的歌曲的艺术家为空时展示,不能为 nulldefaultAlbum
- 默认专辑,会在正在播放的歌曲的专辑为空时展示,不能为 nullenableProgressClock
- 是否启用进度条时钟(用于实时更新播放进度)protected void onInitialized()
PlayerViewModel
初始化完成后调用。public void setAutoDisconnect(boolean autoDisconnect)
autoDisconnect
- 如果为 true,则会在 ViewModel 被清理时自动断开 PlayerClient 的连接protected void onCleared()
onCleared
在类中 androidx.lifecycle.ViewModel
public boolean isInitialized()
init(Context, PlayerClient)
方法进行初始化后
才能正常使用当前 PlayerViewModel
对象@NonNull public PlayerClient getPlayerClient() throws java.lang.IllegalStateException
PlayerViewModel
关联到的 PlayerClient
对象。java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getTitle() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getArtist() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getAlbum() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getIconUri() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Integer> getDuration() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.MutableLiveData<java.lang.Integer> getPlayProgress() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Integer> getBufferedProgress() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Integer> getPlayPosition() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<PlayMode> getPlayMode() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)public androidx.lifecycle.LiveData<java.lang.Float> getSpeed() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<PlaybackState> getPlaybackState() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Boolean> getStalled() throws java.lang.IllegalStateException
当缓冲去没有足够的数据支持播放器继续播放时,该值为 true。
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)public androidx.lifecycle.LiveData<java.lang.Boolean> getConnected() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)public androidx.lifecycle.LiveData<java.lang.Boolean> getPreparing() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Boolean> isError() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getErrorMessage() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<MusicItem> getPlayingMusicItem() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Boolean> getPlayingNoStalled()
PlaybackState.PLAYING
并且播放器没有处于 stalled 状态。@NonNull public androidx.lifecycle.LiveData<java.lang.String> getTextDuration() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getTextPlayProgress() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Boolean> getSleepTimerStarted()
@NonNull public androidx.lifecycle.LiveData<java.lang.Integer> getSleepTimerTime() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getTextSleepTimerTime() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.Integer> getSleepTimerProgress() throws java.lang.IllegalStateException
睡眠定时器的进度是个倒计时。
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)@NonNull public androidx.lifecycle.LiveData<java.lang.String> getTextSleepTimerProgress() throws java.lang.IllegalStateException
睡眠定时器的进度是个倒计时。
java.lang.IllegalStateException
- 如果当前 PlayerViewModel
对象还没有被初始化(isInitialized()
返回 false)public void setPlaylist(Playlist playlist)
playlist
- 新的播放列表public void setPlaylist(Playlist playlist, boolean play)
playlist
- 新的播放列表play
- 是否立即播放列表的第 1 首音乐public void setPlaylist(Playlist playlist, int position, boolean play)
playlist
- 新的播放列表position
- 要播放的歌曲的位置play
- 是否立即播放列表的 position 位置处音乐public void play()
public void pause()
public void playPause()
public void playPause(int position)
如果 position 与当前正在播放的音乐的位置是一样的,则暂停播放,否则播放列表中 position 位置处的音乐。
public void stop()
public void skipToPrevious()
public void skipToNext()
public void skipToPosition(int position)
public void fastForward()
public void rewind()
public void setNextPlay(@NonNull MusicItem musicItem)
musicItem
- 要设定为 “下一次播放” 的歌曲,如果歌曲已存在播放列表中,则会移动到 “下一曲播放” 的位
置,如果歌曲不存在,则插入到 “下一曲播放” 位置public void setPlayMode(@NonNull PlayMode playMode)
playMode
- 播放模式public void setSpeed(float speed)
speed
- 要设置的播放速度。public void seekTo(int progress)
注意!seekTo 方法接收的参数的单位是 毫秒,而 PlayProgress 的单位是 秒。如果使用 PlayProgress 值来调整播放进度,则需要乘以 1000。
progress
- 要调整到的播放进度(单位:毫秒)public void cancelProgressClock()
public void startSleepTimer(long time) throws java.lang.IllegalArgumentException
time
- 睡眠时间(单位:毫秒)。播放器会在经过 time 时间后暂停播放。java.lang.IllegalArgumentException
- 如果定时时间小于 0,则抛出该异常public void startSleepTimer(long time, SleepTimer.TimeoutAction action) throws java.lang.IllegalArgumentException
time
- 睡眠时间(单位:毫秒)。播放器会在经过 time 时间后暂停播放。action
- 定时器的的时间到时要执行的操作。java.lang.IllegalArgumentException
- 如果定时时间小于 0,则抛出该异常public void cancelSleepTimer()
public void onStartTrackingTouch(android.widget.SeekBar seekBar)
如果你启用了 DataBinding,并且使用 SeekBar 来显示和调整播放进度,那么请将 SeekBar 的
android:onStartTrackingTouch
事件绑定为当前方法。
例如:
android:onStartTrackingTouch="@{playerViewModel::onStartTrackingTouch}"
public void onStopTrackingTouch(android.widget.SeekBar seekBar)
如果你启用了 DataBinding,并且使用 SeekBar 来显示和调整播放进度,那么请将 SeekBar 的
android:onStopTrackingTouch
事件绑定为当前方法。
例如:
android:onStopTrackingTouch="@{playerViewModel::onStopTrackingTouch}"