public class PlayerService extends androidx.media.MediaBrowserServiceCompat implements PlayerManager, PlaylistManager, PlaylistEditor, SleepTimer
player service
实现,用于在后台播放音乐。
可以使用 PlayerClient
类建立与 PlayerService
连接,并对播放器进行控制。
MediaSession 框架:
PlayerService
继承了 MediaBrowserServiceCompat
类,因此也可以使用
MediaBrowserCompat
类来建立与 PlayerService
连接,并对播放器进行控制。不过不推荐这么做,
因为本项目大部的功能都依赖于 PlayerClient
类,如果不使用 PlayerClient
类,那么也无法使
用这些功能。
限定符和类型 | 类和说明 |
---|---|
static interface |
PlayerService.CustomAction
自定义动作。
|
static class |
PlayerService.MediaNotificationView
通知栏控制器,使用 Android 系统提供的样式。
|
static class |
PlayerService.MediaSessionCallback
该类继承了 MediaSessionCompat.Callback 类,如果你需要对 MediaSession 框架的
的 MediaSessionCompat.Callback 进行定制,则可以覆盖
onCreateMediaSessionCallback() 方法,
并返回一个自定义的 PlayerService.MediaSessionCallback 实现。 |
static class |
PlayerService.NotificationView
通知栏控制器的基类。
|
androidx.media.MediaBrowserServiceCompat.BrowserRoot, androidx.media.MediaBrowserServiceCompat.Result<T>
PlaylistManager.Callback
SleepTimer.OnStateChangeListener, SleepTimer.TimeoutAction
限定符和类型 | 字段和说明 |
---|---|
static java.lang.String |
CUSTOM_ACTION_SHUTDOWN
如果你直接使用
MediaBrowserCompat 连接 PlayerService, 你的客户端可以发送该
custom action 来关闭 PlayerService。 |
static java.lang.String |
DEFAULT_MEDIA_ROOT_ID
默认的 root id,值为 `"root"`。
|
static java.lang.String |
SESSION_EVENT_ON_SHUTDOWN
如果你直接使用
MediaBrowserCompat 连接 PlayerService, 那么你的客户端应该在接收到该
session event 时主动断开与 PlayerService 的连接。 |
KEY_MEDIA_ITEM, KEY_SEARCH_RESULTS, RESULT_ERROR, RESULT_OK, RESULT_PROGRESS_UPDATE, SERVICE_INTERFACE
START_CONTINUATION_MASK, START_FLAG_REDELIVERY, START_FLAG_RETRY, START_NOT_STICKY, START_REDELIVER_INTENT, START_STICKY, START_STICKY_COMPATIBILITY, STOP_FOREGROUND_DETACH, STOP_FOREGROUND_REMOVE
ACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APP_OPS_SERVICE, APPWIDGET_SERVICE, AUDIO_SERVICE, BATTERY_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_EXTERNAL_SERVICE, BIND_IMPORTANT, BIND_INCLUDE_CAPABILITIES, BIND_NOT_FOREGROUND, BIND_NOT_PERCEPTIBLE, BIND_WAIVE_PRIORITY, BIOMETRIC_SERVICE, BLOB_STORE_SERVICE, BLUETOOTH_SERVICE, CAMERA_SERVICE, CAPTIONING_SERVICE, CARRIER_CONFIG_SERVICE, CLIPBOARD_SERVICE, COMPANION_DEVICE_SERVICE, CONNECTIVITY_DIAGNOSTICS_SERVICE, CONNECTIVITY_SERVICE, CONSUMER_IR_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, CROSS_PROFILE_APPS_SERVICE, DEVICE_POLICY_SERVICE, DISPLAY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, EUICC_SERVICE, FILE_INTEGRITY_SERVICE, FINGERPRINT_SERVICE, HARDWARE_PROPERTIES_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, IPSEC_SERVICE, JOB_SCHEDULER_SERVICE, KEYGUARD_SERVICE, LAUNCHER_APPS_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_PROJECTION_SERVICE, MEDIA_ROUTER_SERVICE, MEDIA_SESSION_SERVICE, MIDI_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_NO_LOCALIZED_COLLATORS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NETWORK_STATS_SERVICE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, POWER_SERVICE, PRINT_SERVICE, RECEIVER_VISIBLE_TO_INSTANT_APPS, RESTRICTIONS_SERVICE, ROLE_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, SHORTCUT_SERVICE, STORAGE_SERVICE, STORAGE_STATS_SERVICE, SYSTEM_HEALTH_SERVICE, TELECOM_SERVICE, TELEPHONY_IMS_SERVICE, TELEPHONY_SERVICE, TELEPHONY_SUBSCRIPTION_SERVICE, TEXT_CLASSIFICATION_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, TV_INPUT_SERVICE, UI_MODE_SERVICE, USAGE_STATS_SERVICE, USB_SERVICE, USER_SERVICE, VIBRATOR_SERVICE, VPN_MANAGEMENT_SERVICE, WALLPAPER_SERVICE, WIFI_AWARE_SERVICE, WIFI_P2P_SERVICE, WIFI_RTT_RANGING_SERVICE, WIFI_SERVICE, WINDOW_SERVICE
构造器和说明 |
---|
PlayerService() |
限定符和类型 | 方法和说明 |
---|---|
protected void |
addCustomAction(java.lang.String action,
PlayerService.CustomAction customAction)
添加一个自定义动作。
|
void |
appendMusicItem(MusicItem musicItem)
往播放列表的末尾添加一首歌曲。
|
protected void |
attachAudioEffect(int audioSessionId)
已过时。
|
android.content.Intent |
buildCustomActionIntent(java.lang.String actionName)
构建一个用于触发当前 PlayerService 的自定义动作的广播 Intent。
|
static android.content.Intent |
buildCustomActionIntent(java.lang.String actionName,
java.lang.Class<? extends PlayerService> service)
构建一个用于触发自定义动作的广播 Intent。
|
void |
cancelSleepTimer()
取消睡眠定时器。
|
protected void |
detachAudioEffect()
已过时。
|
int |
getErrorCode()
获取错误码。
|
java.lang.String |
getErrorMessage()
获取错误信息。
|
long |
getLastModified()
获取最后一次修改播放列表的时间。
|
protected android.support.v4.media.session.MediaSessionCompat |
getMediaSession()
获取当前 Service 中的
MediaSessionCompat 对象。 |
static java.lang.String |
getPersistenceId(java.lang.Class<? extends PlayerService> service)
获取指定
PlayerService 的持久化 ID。 |
PlaybackState |
getPlaybackState()
获取播放器当前的播放状态。
|
Player |
getPlayer()
获取播放器的 Player 对象。
|
MusicItem |
getPlayingMusicItem()
获取当前正在播放的音乐的 MusicItem 对象。
|
void |
getPlaylist(PlaylistManager.Callback callback)
以异步的方式获取播放队列。
|
android.os.Bundle |
getPlaylistExtra()
获取播放队列携带的额外参数(可为 null)。
|
java.lang.String |
getPlaylistName()
获取播放列表的名称。
|
int |
getPlaylistSize()
获取当前播放队列的大小。
|
java.lang.String |
getPlaylistToken()
获取播放列表的 Token
|
PlayMode |
getPlayMode()
获取播放队列的播放模式。
|
void |
insertMusicItem(int position,
MusicItem musicItem)
往列表中插入了一首新的歌曲。
|
protected boolean |
isCached(MusicItem musicItem,
SoundQuality soundQuality)
已过时。
|
protected void |
isCached(MusicItem musicItem,
SoundQuality soundQuality,
AsyncResult<java.lang.Boolean> result)
查询具有 soundQuality 音质的 MusicItem 表示的的音乐是否已被缓存。
|
boolean |
isError()
播放器是否发生了错误。
|
protected boolean |
isForeground()
当前 Service 是否处于前台。
|
boolean |
isIgnoreAudioFocus()
是否忽略音频焦点。
|
boolean |
isPlaylistEditable()
播放列表是否是可编辑的。
|
boolean |
isPrepared()
播放器当前是否处已准备完毕。
|
boolean |
isPreparing()
播放器当前是否处正在准备中。
|
boolean |
isStalled()
当前播放器是否处于
stalled 状态。 |
void |
moveMusicItem(int fromPosition,
int toPosition)
移动播放列表中某首歌曲的位置。
|
void |
onCreate() |
protected AudioEffectManager |
onCreateAudioEffectManager()
创建音频特效引擎。
|
protected android.media.AudioManager.OnAudioFocusChangeListener |
onCreateAudioFocusChangeListener()
可以通过覆盖该方法来提供一个自定义的 AudioManager.OnAudioFocusChangeListener
|
protected HistoryRecorder |
onCreateHistoryRecorder()
创建历史记录器,用于记录播放器的播放历史。
|
protected PlayerService.MediaSessionCallback |
onCreateMediaSessionCallback()
创建一个
PlayerService.MediaSessionCallback 对象。 |
protected MusicPlayer |
onCreateMusicPlayer(android.content.Context context,
MusicItem musicItem,
android.net.Uri uri)
该方法会在创建 MusicPlayer 对象时调用。
|
protected PlayerService.NotificationView |
onCreateNotificationView()
创建一个通知栏控制器,你可以通过覆盖该方法来提供自定义的通知栏控制器。
|
protected void |
onCustomAction(java.lang.String action,
android.os.Bundle extras)
该方法会在 MediaSession 接收到 custom action 时调用。
|
void |
onDestroy() |
androidx.media.MediaBrowserServiceCompat.BrowserRoot |
onGetRoot(java.lang.String clientPackageName,
int clientUid,
android.os.Bundle rootHints) |
protected void |
onHeadsetHookClicked(int clickCount)
当耳机上的按钮被点击时会调用该方法。
|
void |
onLoadChildren(java.lang.String parentId,
androidx.media.MediaBrowserServiceCompat.Result<java.util.List<android.support.v4.media.MediaBrowserCompat.MediaItem>> result) |
protected boolean |
onMediaButtonEvent(android.content.Intent mediaButtonEvent)
该方法会在媒体按钮被触发时调用。
|
protected android.net.Uri |
onRetrieveMusicItemUri(MusicItem musicItem,
SoundQuality soundQuality)
已过时。
|
protected void |
onRetrieveMusicItemUri(MusicItem musicItem,
SoundQuality soundQuality,
AsyncResult<android.net.Uri> result)
获取音乐的播放链接。
|
int |
onStartCommand(android.content.Intent intent,
int flags,
int startId) |
protected void |
removeCustomAction(java.lang.String action)
移除一个自定义动作。
|
void |
removeMusicItem(int position)
移除播放列表中指定位置的歌曲。
|
void |
removeMusicItem(MusicItem musicItem)
从播放列表中移除了指定歌曲。
|
void |
setAudioEffectConfig(android.os.Bundle config)
修改音频特效的配置。
|
void |
setAudioEffectEnabled(boolean enabled)
设置是否启用音频特效(如:均衡器)(默认为 false)。
|
void |
setIgnoreAudioFocus(boolean ignoreAudioFocus)
设置是否忽略音频焦点。
|
void |
setMaxIDLETime(int minutes)
设置
PlayerService 处于空闲状态(暂停或者停止后)的最大存活时间。 |
protected void |
setMediaSessionFlags(int flags)
设置 MediaSessionCompat 的 Flags。
|
void |
setNextPlay(MusicItem musicItem)
设置 “下一次播放” 的歌曲。
|
void |
setOnlyWifiNetwork(boolean onlyWifiNetwork)
设置是否只允许在 WiFi 网络下联网(默认为 false)。
|
void |
setPlaylist(Playlist playlist)
设置一个新的播放列表。
|
void |
setPlaylist(Playlist playlist,
boolean play)
设置一个新的播放列表。
|
void |
setPlaylist(Playlist playlist,
int position,
boolean play)
设置一个新的播放列表。
|
void |
setSoundQuality(SoundQuality soundQuality)
设置播放器的首选音质(默认为
SoundQuality.STANDARD )。 |
void |
shutdown()
关闭播放器。
|
protected void |
startForeground()
启动前台 Service。
|
void |
startSleepTimer(long time,
SleepTimer.TimeoutAction action)
启动睡眠定时器。
|
protected void |
stopForegroundEx(boolean removeNotification)
停止前台 Service。
|
void |
updateNotificationView()
要求 Service 更新 NotificationView,如果没有设置 NotificationView,则忽略本次操作。
|
attachToBaseContext, dump, getBrowserRootHints, getCurrentBrowserInfo, getSessionToken, notifyChildrenChanged, notifyChildrenChanged, notifyChildrenChanged, onBind, onCustomAction, onLoadChildren, onLoadItem, onSearch, onSubscribe, onUnsubscribe, setSessionToken
attachBaseContext, getApplication, getForegroundServiceType, onConfigurationChanged, onLowMemory, onRebind, onStart, onTaskRemoved, onTrimMemory, onUnbind, startForeground, startForeground, stopForeground, stopForeground, stopSelf, stopSelf, stopSelfResult
bindIsolatedService, bindService, bindService, bindServiceAsUser, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkSelfPermission, checkUriPermission, checkUriPermission, clearWallpaper, createAttributionContext, createConfigurationContext, createContextForSplit, createDeviceProtectedStorageContext, createDisplayContext, createPackageContext, createWindowContext, databaseList, deleteDatabase, deleteFile, deleteSharedPreferences, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getAttributionTag, getBaseContext, getCacheDir, getClassLoader, getCodeCacheDir, getContentResolver, getDatabasePath, getDataDir, getDir, getDisplay, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getExternalMediaDirs, getFilesDir, getFileStreamPath, getMainExecutor, getMainLooper, getNoBackupFilesDir, getObbDir, getObbDirs, getOpPackageName, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getSystemService, getSystemServiceName, getTheme, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isDeviceProtectedStorage, isRestricted, moveDatabaseFrom, moveSharedPreferencesFrom, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, registerReceiver, registerReceiver, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setTheme, setWallpaper, setWallpaper, startActivities, startActivities, startActivity, startActivity, startForegroundService, startInstrumentation, startIntentSender, startIntentSender, startService, stopService, unbindService, unregisterReceiver, updateServiceGroup
getColor, getColorStateList, getDrawable, getString, getString, getSystemService, getText, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, registerComponentCallbacks, sendBroadcastWithMultiplePermissions, unregisterComponentCallbacks
public static final java.lang.String DEFAULT_MEDIA_ROOT_ID
public static final java.lang.String CUSTOM_ACTION_SHUTDOWN
MediaBrowserCompat
连接 PlayerService, 你的客户端可以发送该
custom action
来关闭 PlayerService。PlayerService 在接收到该 custom action
后
会发出一个名为 SESSION_EVENT_ON_SHUTDOWN
的 session event
,客户端在接收到该
session event
后应该主动断开与 PlayerService 的连接。当所有客户端断开与 PlayerService 的
连接后,PlayerService 会自动终止。public static final java.lang.String SESSION_EVENT_ON_SHUTDOWN
MediaBrowserCompat
连接 PlayerService, 那么你的客户端应该在接收到该
session event
时主动断开与 PlayerService 的连接。public void onCreate()
onCreate
在类中 androidx.media.MediaBrowserServiceCompat
public int onStartCommand(android.content.Intent intent, int flags, int startId)
onStartCommand
在类中 android.app.Service
@Nullable public androidx.media.MediaBrowserServiceCompat.BrowserRoot onGetRoot(@NonNull java.lang.String clientPackageName, int clientUid, @Nullable android.os.Bundle rootHints)
onGetRoot
在类中 androidx.media.MediaBrowserServiceCompat
public void onLoadChildren(@NonNull java.lang.String parentId, @NonNull androidx.media.MediaBrowserServiceCompat.Result<java.util.List<android.support.v4.media.MediaBrowserCompat.MediaItem>> result)
onLoadChildren
在类中 androidx.media.MediaBrowserServiceCompat
public void onDestroy()
onDestroy
在类中 android.app.Service
protected void setMediaSessionFlags(int flags)
相当于调用 MediaSessionCompat 的 setFlags(int) 方法。
@NonNull protected PlayerService.MediaSessionCallback onCreateMediaSessionCallback()
PlayerService.MediaSessionCallback
对象。
如果你需要对 MediaSession 框架的 MediaSessionCompat.Callback 进行定制,则可以覆盖该方法并返回一个
PlayerService.MediaSessionCallback
对象。PlayerService.MediaSessionCallback
类继承了
MediaSessionCompat.Callback 类。
@Nullable protected PlayerService.NotificationView onCreateNotificationView()
PlayerService.MediaNotificationView
,如果你不需要在通知栏中显示控制器,可以覆盖该方法并返回 null。PlayerService.NotificationView
对象,返回 null 时将隐藏通知栏控制器@Nullable protected AudioEffectManager onCreateAudioEffectManager()
@Nullable protected HistoryRecorder onCreateHistoryRecorder()
public void setSoundQuality(SoundQuality soundQuality)
PlayerManager
SoundQuality.STANDARD
)。setSoundQuality
在接口中 PlayerManager
soundQuality
- 要设置的音质SoundQuality.STANDARD
,
SoundQuality.LOW
,
SoundQuality.HIGH
,
SoundQuality.SUPER
public void setAudioEffectEnabled(boolean enabled)
PlayerManager
setAudioEffectEnabled
在接口中 PlayerManager
enabled
- 是否启用音频特效public void setAudioEffectConfig(android.os.Bundle config)
PlayerManager
setAudioEffectConfig
在接口中 PlayerManager
protected void onHeadsetHookClicked(int clickCount)
该方法的默认行为:
你可以覆盖该方法来自定义耳机按钮被点击时的行为。
clickCount
- 按钮被连续点击的次数。@Deprecated protected void attachAudioEffect(int audioSessionId)
audioSessionId
- 当前正在播放的音乐的 audio session id。如果为 0,则可以忽略。@Deprecated protected void detachAudioEffect()
public void setOnlyWifiNetwork(boolean onlyWifiNetwork)
PlayerManager
setOnlyWifiNetwork
在接口中 PlayerManager
onlyWifiNetwork
- 是否只允许在 WiFi 网络下联网public void setIgnoreAudioFocus(boolean ignoreAudioFocus)
PlayerManager
setIgnoreAudioFocus
在接口中 PlayerManager
ignoreAudioFocus
- 是否忽略音频焦点。如果为 true,则播放器会忽略音频焦点的获取与丢失。public final void shutdown()
调用该方法后 Service 会要求所有已绑定的客户端断开连接,然后终止自己。
shutdown
在接口中 PlayerManager
public boolean isIgnoreAudioFocus()
public final void setMaxIDLETime(int minutes)
PlayerService
处于空闲状态(暂停或者停止后)的最大存活时间。
当播放器处于空闲状态(暂停或者停止)的时间超出 minutes 分钟后将自动终止 PlayerService
。
将 minutes 设置为小于等于 0 时将关闭此功能(即使播放器处于空闲状态,也不会自动终止 PlayerService
)。
默认未启用该功能。
minutes
- 最大的空闲时间,设置为小于等于 0 时将关闭此功能(即使播放器处于空闲状态,也不会自动终止 PlayerService
)。protected final android.support.v4.media.session.MediaSessionCompat getMediaSession()
MediaSessionCompat
对象。MediaSessionCompat
对象protected final void addCustomAction(@NonNull java.lang.String action, @NonNull PlayerService.CustomAction customAction)
自定义动作可通过构造一个具有指定 action 的 Intent 对象并调用 startService 方法触发。
例:
Intent intent = new Intent(context, PlayerService.class); intent.setAction(action); ... context.startService(intent);
action
- 自定义动作的名称customAction
- 自定义动作要执行的任务,不能为 nullpublic android.content.Intent buildCustomActionIntent(@NonNull java.lang.String actionName)
如果需要携带额外的参数,则可以在构建好 Intent 对象后,将这些额外参数存入到 Intent 中即可。
actionName
- 当前 PlayerService 的自定义动作的名称,不能为 null。public static android.content.Intent buildCustomActionIntent(@NonNull java.lang.String actionName, @NonNull java.lang.Class<? extends PlayerService> service)
如果需要携带额外的参数,则可以在构建好 Intent 对象后,将这些额外参数存入到 Intent 中即可。
actionName
- 自定义动作的名称,不能为 null。service
- 自定义动作关联到的那个 PlayerService 的 Class 对象。@NonNull public static java.lang.String getPersistenceId(@NonNull java.lang.Class<? extends PlayerService> service) throws java.lang.IllegalArgumentException
PlayerService
的持久化 ID。
可以使用 PersistenceId
注解设置你的的 PlayerService
的持久化 ID。
该 ID 值将用于状态持久化,请务必保证其唯一性。如果没有设置,则默认返回你的 PlayerService
的完整类名(使用 Class.getName()
方法获取)。
注意!空 ID 值是非法的,如果 ID 值为空,则会抛出 IllegalArgumentException 异常。
service
- 你的 PlayerService
的 Class 对象,不能为 nullPlayerService
的持久化 ID,如果没有设置,则返回使用你的 PlayerService
的完整类名java.lang.IllegalArgumentException
- 如果 ID 值为空,则抛出 IllegalArgumentException 异常protected final void removeCustomAction(@NonNull java.lang.String action)
action
- 自定义动作的名称@Nullable public final android.os.Bundle getPlaylistExtra()
@NonNull public final PlaybackState getPlaybackState()
public final boolean isStalled()
stalled
状态。true
,否则返回 falsepublic final MusicItem getPlayingMusicItem()
public final boolean isError()
public final int getErrorCode()
该方法的返回值仅在发生错误(isError()
方法返回 true)时才有意义。
public final java.lang.String getErrorMessage()
该方法的返回值仅在发生错误(isError()
方法返回 true)时才有意义。
public final void updateNotificationView()
protected final boolean isForeground()
protected final void startForeground()
protected final void stopForegroundEx(boolean removeNotification)
removeNotification
- 是否清除 NotificationViewpublic final void setPlaylist(@NonNull Playlist playlist)
playlist
- 播放列表(不能为 null)public final void setPlaylist(@NonNull Playlist playlist, boolean play)
playlist
- 播放列表(不能为 null)play
- 是否立即播放列表中的第一首音乐public void insertMusicItem(int position, @NonNull MusicItem musicItem) throws java.lang.IllegalArgumentException
如果播放列表中已包含指定歌曲,则会将它移动到 position 位置,如果不存在,则会将歌曲插入到 position 位置。
insertMusicItem
在接口中 PlaylistEditor
position
- 歌曲插入的位置musicItem
- 要插入的歌曲,不能为 nulljava.lang.IllegalArgumentException
- 如果 position 的值小于 0,则抛出该异常public void appendMusicItem(@NonNull MusicItem musicItem)
PlaylistEditor
如果播放列表中已包含指定歌曲,则会将它移动到播放列表的末尾位置,如果不存在,则会将歌曲添加到播放列表的末尾。
appendMusicItem
在接口中 PlaylistEditor
musicItem
- 要添加播放列表末尾的歌曲,不能为 nullpublic void moveMusicItem(int fromPosition, int toPosition) throws java.lang.IllegalArgumentException
moveMusicItem
在接口中 PlaylistEditor
fromPosition
- 歌曲在列表中的位置toPosition
- 歌曲要移动到的位置。如果 toPosition == fromPosition
,则会忽略本次调用java.lang.IllegalArgumentException
- 如果 fromPosition 或者 toPosition 参数小于 0,则抛出该异常public void removeMusicItem(@NonNull MusicItem musicItem)
PlaylistEditor
removeMusicItem
在接口中 PlaylistEditor
musicItem
- 要移除的歌曲。如果播放列表中不包含该歌曲,则忽略本次调用public void removeMusicItem(int position)
PlaylistEditor
removeMusicItem
在接口中 PlaylistEditor
position
- 要移除的歌曲的位置public void setNextPlay(@NonNull MusicItem musicItem)
PlaylistEditor
setNextPlay
在接口中 PlaylistEditor
musicItem
- 要设定为 “下一次播放” 的歌曲,如果歌曲已存在播放列表中,则会移动到 “下一曲播放” 的位
置,如果歌曲不存在,则插入到 “下一曲播放” 位置public final void setPlaylist(@NonNull Playlist playlist, int position, boolean play) throws java.lang.IllegalArgumentException
setPlaylist
在接口中 PlaylistEditor
playlist
- 播放列表(不能为 null)position
- 播放列表中要播放的歌曲的位置play
- 是否立即播放 position
参数指定处的音乐java.lang.IllegalArgumentException
- 如果 position 参数小于 0,则会抛出该异常@Deprecated protected boolean isCached(MusicItem musicItem, SoundQuality soundQuality)
isCached(MusicItem, SoundQuality, AsyncResult)
方法代替。
查询具有 soundQuality 音质的 MusicItem 表示的的音乐是否已被缓存。
该方法会在异步线程中被调用。
musicItem
- 要查询的 MusicItem 对象soundQuality
- 音乐的音质protected void isCached(@NonNull MusicItem musicItem, @NonNull SoundQuality soundQuality, @NonNull AsyncResult<java.lang.Boolean> result)
该方法会在异步线程中被调用。
musicItem
- 要查询的 MusicItem 对象soundQuality
- 音乐的音质result
- 用于接收异步任务的结果值@NonNull protected MusicPlayer onCreateMusicPlayer(@NonNull android.content.Context context, @NonNull MusicItem musicItem, @NonNull android.net.Uri uri)
你可以重写该方法来返回你自己的 MusicPlayer 实现。
context
- Application Context@Nullable protected android.media.AudioManager.OnAudioFocusChangeListener onCreateAudioFocusChangeListener()
@Deprecated protected android.net.Uri onRetrieveMusicItemUri(@NonNull MusicItem musicItem, @NonNull SoundQuality soundQuality) throws java.lang.Exception
onRetrieveMusicItemUri(MusicItem, SoundQuality, AsyncResult)
方法代替。
获取音乐的播放链接。
该方法会在异步线程中执行,因此可以执行各种耗时操作,例如访问网络。
musicItem
- 要播放的音乐soundQuality
- 要播放的音乐的音质java.lang.Exception
- 获取音乐播放链接的过程中发生的任何异常protected void onRetrieveMusicItemUri(@NonNull MusicItem musicItem, @NonNull SoundQuality soundQuality, @NonNull AsyncResult<android.net.Uri> result)
该方法会在异步线程中执行,因此可以执行各种耗时操作,例如访问网络。
musicItem
- 要播放的音乐soundQuality
- 要播放的音乐的音质result
- 用于接收异步任务的结果值@NonNull public final Player getPlayer()
protected boolean onMediaButtonEvent(android.content.Intent mediaButtonEvent)
mediaButtonEvent
- 被触发的媒体按钮protected void onCustomAction(java.lang.String action, android.os.Bundle extras)
action
- 自定义动作的名称extras
- 自定义动作携带的额外数据@NonNull public java.lang.String getPlaylistName()
PlaylistManager
getPlaylistName
在接口中 PlaylistManager
public int getPlaylistSize()
PlaylistManager
这是个轻量级操作,可在 UI 线程上直接运行。
getPlaylistSize
在接口中 PlaylistManager
@NonNull public java.lang.String getPlaylistToken()
PlaylistManager
getPlaylistToken
在接口中 PlaylistManager
public boolean isPlaylistEditable()
PlaylistManager
isPlaylistEditable
在接口中 PlaylistManager
public void getPlaylist(@NonNull PlaylistManager.Callback callback)
PlaylistManager
getPlaylist
在接口中 PlaylistManager
public long getLastModified()
PlaylistManager
这个时间是 System.currentTimeMillis()
getLastModified
在接口中 PlaylistManager
public void startSleepTimer(long time, @NonNull SleepTimer.TimeoutAction action) throws java.lang.IllegalArgumentException
startSleepTimer
在接口中 SleepTimer
time
- 睡眠时间(单位:毫秒)。播放器会在经过 time 时间后暂停播放。action
- 定时器的的时间到时要执行的操作。java.lang.IllegalArgumentException
- 如果 time 小于 0,则抛出该异常。public void cancelSleepTimer()
cancelSleepTimer
在接口中 SleepTimer
public final boolean isPreparing()
public final boolean isPrepared()