今日任务
- IPlayerController 具体方法实现
Que 0x01 梳理思路
昨天分析了 IPlayerController 中接口的实现,今天找到了其具体实现部分的代码,进而深入分析。
Que 0x02 play
实现代码如下:
private void play() {
play(false);
}
private void play(boolean applyFadeUp) {
iLog.d(TAG, "play() - mPlayerState : " + this.mPlayerState + ", applyFadeUp : " + applyFadeUp);
if (!CscFeatures.SUPPORT_MUSIC_PLAYBACK_DURING_CALL && !CallStateChecker.isCallIdle(this.mContext)) {
iLog.d(TAG, "play() - Can't play during call.");
this.mOnSoundPlayerChangedListener.onError(1);
} else if (this.mAudioManager.requestAudioFocus(this.mAudioFocusChangeListener, 3, 1) == 0) {
iLog.d(TAG, "play() - Can't play because audio focus request is failed.");
} else if (!isPlaying()) {
if (canPlayState()) {
if (applyFadeUp) {
this.mPlayerHandler.sendEmptyMessageDelayed(0, 20);
} else {
this.mPlayer.setVolume(this.mMaxVolume, this.mMaxVolume);
}
this.mPlayer.start();
this.mPlayerState = 4;
this.mOnSoundPlayerStateListener.onPlayStateChanged(this);
setPlaybackState(getPosition());
setBatteryTemperatureCheck(true);
if (USAFeatures.REGIONAL_USA_GATE_ENABLED) {
GateMessageUtils.printMessage("AUDI_PLAYING", this.mUri.getPath());
return;
}
return;
}
setDataSource(this.mUri, true);
this.mOnSoundPlayerStateListener.onMetaChanged(this);
}
}
- 判断是否支持打电话的时候播放音乐,并且判断是否正在打电话,如果二者同时满足则打印 log 并且返回一个错误
- 获取音频焦点,获取失败则退出
- 判断是否正在播放,如果正在播放则退出
- 通过 mPlayerState 来判断是否满足播放,如果不满足则退出
- applyFadeUp 暂时没能搞清楚是判断什么,如果为真则延迟发送一些消息,否则设置音量
- 开始播放并且设置播放状态
- 更新播放按钮的状态
- 获取当前播放点
- 监控电池状态
-
setDataSource(this.mUri, true)
用于设置音频文件路径 -
this.mOnSoundPlayerStateListener.onMetaChanged(this)
暂且意义不明
Que 0x03 pause
实现代码如下:
public void pause() {
iLog.d(TAG, "pause() - mPlayerState : " + this.mPlayerState);
if (this.mPlayerState == 4) {
this.mPlayerHandler.removeMessages(0);
this.mPlayer.pause();
this.mPlayerState = 5;
this.mOnSoundPlayerStateListener.onPlayStateChanged(this);
setPlaybackState(getPosition());
setBatteryTemperatureCheck(false);
}
}
- 判断播放状态,如果正在播放则状态值为 4
- 取消
sendEmptyMessageDelayed
- 暂停,设置暂停状态,暂停状态值为 5
- 更新播放按钮状态
- 获取当前播放点
- 监控电池状态
Que 0x04 stop
实现代码如下:
public void stop() {
iLog.d(TAG, "stop() - mPlayerState : " + this.mPlayerState);
this.mPlayerHandler.removeMessages(0);
if (isPlaying()) {
this.mPlayer.pause();
this.mPlayerState = 5;
}
seek(0);
this.mPlayer.stop();
this.mPlayerState = 6;
this.mOnSoundPlayerStateListener.onPlayStateChanged(this);
this.mOnSoundPlayerStateListener.onSeekComplete(this);
reset();
}
- 取消
sendEmptyMessageDelayed
- 如果正在播放,则先暂停播放,并且设置播放状态为暂停
-
seek
暂且意义不明 - 停止,设置播放状态为停止,停止状态值为 6
- 更新播放按钮状态
-
this.mOnSoundPlayerStateListener.onSeekComplete(this)
暂且意义不明 - 重置 MediaPlayer 对象
Que 0x05 reset
实现代码如下:
private void reset() {
iLog.d(TAG, "reset()");
this.mPlayer.reset();
this.mPlayerState = 0;
}
- 重置 MediaPlayer 对象
- 设置播放状态为初始状态,初始状态值为 0
Que 0x06 seek
实现代码如下:
public void seek(long position) {
if (this.mPlayerState > 2) {
setPlaybackState(position);
this.mPlayer.seekTo((int) position);
}
}
- 更新播放状态