public interface MusicPlayer
关于如何实现自定义的音乐播放器,请参考 MusicPlayer
各方法的文档。文档对如何实现各方法的要求与限制进行了详细介绍。
MediaMusicPlayer
限定符和类型 | 接口和说明 |
---|---|
static interface |
MusicPlayer.OnBufferingUpdateListener
用于监听音乐播放器的缓存进度。
|
static interface |
MusicPlayer.OnCompletionListener
用于监听音乐播放器是否播放完毕。
|
static interface |
MusicPlayer.OnErrorListener
用于监听器音乐播放的错误状态。
|
static interface |
MusicPlayer.OnPreparedListener
用于监听音乐播放器是否准备完毕。
|
static interface |
MusicPlayer.OnRepeatListener
用于监听播放的循环播放事件。
|
static interface |
MusicPlayer.OnSeekCompleteListener
用于监听音乐播放器的播放进度是否调整完毕。
|
static interface |
MusicPlayer.OnStalledListener
用于监听播放器的 stalled 状态。
|
void prepare() throws java.lang.Exception
该方法会在主线程上执行,如果准备操作是个耗时操作,你应该在异步线程中执行它。
在实现该方法时,建议先检查 isInvalid()
状态,如果返回 true
,说明当前
MusicPlayer
已失效,此时因立即从该方法中返回,不应该再调用任何方法。
java.lang.Exception
void setLooping(boolean looping)
如果播放器处于循环播放状态,则应返回 true,否则返回 false。对于新创建的 MusicPlayer
对象来说,
该方法默认返回 false。
looping
- 是否循环播放(默认返回 false)。boolean isLooping()
boolean isStalled()
stalled 状态用于表示当前缓冲区是否有足够的数据继续播放,如果缓冲区没有足够的数据支撑继续播放, 则该方法会返回 true,如果缓冲区有足够的数据可以继续播放,则返回 false。
boolean isPlaying()
该方法的返回值只受 start()
、pause()
、stop()
、发生错误或者 release()
的影响。
调用 start()
方法后,该方法应该返回 true,并且只在调用 pause()
、
stop()
、播放器发生错误或者调用了 release()
方法时才返回 false。
调用了 start()
方法后,即使缓冲区没有足够的数据支持继续播放,只要没有没有调用
pause()
、stop()
、发生错误或者调用 release()
方法,
该方法就应该返回 true,即使当前缓冲区没有足够的数据,播放器正在缓冲。
int getDuration()
int getProgress()
void start()
void pause()
void stop()
void seekTo(int pos)
pos
- 要调整到的播放位置(单位:毫秒)void setVolume(float leftVolume, float rightVolume)
如果你的播放器实现不打算支持单独分别设置左右声道的音量,则全部使用 leftVolume 参数的值即可。
leftVolume
- 左声道的音量百分比(范围为 [0.0 ~ 1.0] 的闭区间)rightVolume
- 右声道的音量百分比(范围为 [0.0 ~ 1.0] 的闭区间)void setSpeed(float speed)
speed
- 播放速度,最小值为 0.1。void quiet()
音量应该降低到不足以影响到其他应用的音频清晰度,通常为当前音量的 0.2。
dismissQuiet()
void dismissQuiet()
quiet()
void release()
注意!一旦调用该方法,就不能再调用 MusicPlayer
对象的任何方法,否则会发生不可预见的错误。
调用该方法后应该立即释放你的音乐播放器(释放占用的内存,断开网络连接)。此时 isInvalid()
方法应该返回 true,isPlaying()
方法与 isStalled()
方法应该返回 false。
boolean isInvalid()
当播放器发生错误,或者调用 release()
方法后,该方法应该返回 true。如果该方法返回 true,则不
应再调用除 release()
方法以外的任何其他方法。
release()
方法以外的任何其他方法int getAudioSessionId()
void setOnPreparedListener(@Nullable MusicPlayer.OnPreparedListener listener)
MusicPlayer.OnPreparedListener
监听器。listener
- 监听器对象,可为 null。为 null 时将清除上次设置的监听器。void setOnCompletionListener(@Nullable MusicPlayer.OnCompletionListener listener)
MusicPlayer.OnCompletionListener
监听器。listener
- 监听器对象,可为 null。为 null 时将清除上次设置的监听器。void setOnRepeatListener(@Nullable MusicPlayer.OnRepeatListener listener)
MusicPlayer.OnRepeatListener
监听器。listener
- 监听器对象,可为 null。为 null 时将清除上次设置的监听器。void setOnSeekCompleteListener(@Nullable MusicPlayer.OnSeekCompleteListener listener)
MusicPlayer.OnSeekCompleteListener
监听器。listener
- 监听器对象,可为 null。为 null 时将清除上次设置的监听器。void setOnStalledListener(@Nullable MusicPlayer.OnStalledListener listener)
MusicPlayer.OnStalledListener
监听器。listener
- 监听器对象,可为 null。为 null 时将清除上次设置的监听器。void setOnBufferingUpdateListener(@Nullable MusicPlayer.OnBufferingUpdateListener listener)
MusicPlayer.OnBufferingUpdateListener
监听器。listener
- 监听器对象,可为 null。为 null 时将清除上次设置的监听器。void setOnErrorListener(@Nullable MusicPlayer.OnErrorListener listener)
MusicPlayer.OnErrorListener
监听器。listener
- 监听器对象,可为 null。为 null 时将清除上次设置的监听器。