-
Notifications
You must be signed in to change notification settings - Fork 437
Receiver Part
贾俊辉 edited this page Jun 18, 2018
·
9 revisions
------------------------------------------------------------
//当Receiver被添加到ReceiverGroup时该方法会被调用。建议视图类中View的初始化在此方法中进行。
void onReceiverBind();
------------------------------------------------------------
//当Receiver被从ReceiverGroup中移除时调用。可以在此方法中进行一些释放、销毁操作等。
void onReceiverUnBind();
------------------------------------------------------------
//获取ReceiverGroup中的共享数据池。关于GroupValue,下文有讲解。
GroupValue getGroupValue();
------------------------------------------------------------
//获取播放状态获取器
PlayerStateGetter getPlayerStateGetter();
------------------------------------------------------------
//发送一个针对Receiver的事件,通过此方法发送的事件,所有Receiver都能收到。
void notifyReceiverEvent(int eventCode, Bundle bundle);
------------------------------------------------------------
//发送一个针对特定Receiver的事件,此方法发送的事件只有指定的Receiver能收到。
notifyReceiverPrivateEvent(String key, int eventCode, Bundle bundle);
------------------------------------------------------------
//收到的私有事件,即通过notifyReceiverPrivateEvent发送的事件。
Bundle onPrivateEvent(int eventCode, Bundle bundle);
------------------------------------------------------------
//获取上下文
Context getContext();
------------------------------------------------------------
//所有的播放器相关事件都会被回调到此方法中
void onPlayerEvent(int eventCode, Bundle bundle);
------------------------------------------------------------
//错误事件接收
void onErrorEvent(int eventCode, Bundle bundle);
------------------------------------------------------------
//非特定的Receiver事件都会被回调到此方法中
void onReceiverEvent(int eventCode, Bundle bundle);
------------------------------------------------------------
视图基类,继承自BaseReceiver,上文中BaseReceiver具备的方法均对BaseCover有效。
------------------------------------------------------------
//请求暂停
void requestPause(Bundle bundle);
//请求恢复播放
void requestResume(Bundle bundle);
//请求seek
void requestSeek(Bundle bundle);
//请求停止
void requestStop(Bundle bundle);
//请求重置
void requestReset(Bundle bundle);
//请求重试
void requestRetry(Bundle bundle);
//请求重播
void requestReplay(Bundle bundle);
------------------------------------------------------------
//必须实现的方法。用于初始化视图布局。
abstract View onCreateCoverView(Context context);
------------------------------------------------------------
//设置视图的显示或隐藏
void setCoverVisibility(int visibility);
------------------------------------------------------------
//获取视图的根View,如果您使用ButterKnife时可调用此方法。
View getView();
------------------------------------------------------------
//当视图依附到window时
void onCoverAttachedToWindow();
------------------------------------------------------------
//当视图从window移除时
void onCoverDetachedToWindow();
------------------------------------------------------------
//获取视图的层级优先级,默认为最低ICover.COVER_LEVEL_LOW。
//视图的优先级决定了视图被放置的位置,优先级高的会盖在优先级低的上面。
//可选值如下:
//int COVER_LEVEL_LOW = 0; 低
//int COVER_LEVEL_MEDIUM = 1; 中
//int COVER_LEVEL_HIGH = 2; 高
int getCoverLevel();
------------------------------------------------------------
Receiver组管理器,有两个构造方法:
//如果您需要指定一个GroupValue时使用第二个构造方法,如果不指定或者为空,框架会默认初始化一个GroupValue。
public ReceiverGroup();
public ReceiverGroup(GroupValue groupValue);
------------------------------------------------------------
//添加一个Receiver,你需要指定一个唯一的key值对应于Receiver。
void addReceiver(String key, IReceiver receiver);
------------------------------------------------------------
//依据您传入的key值,移除对应的Receiver
void removeReceiver(String key);
------------------------------------------------------------
//获取组的共享数据池
GroupValue getGroupValue();
------------------------------------------------------------
//获取key对应的Receiver对象
<T extends IReceiver> T getReceiver(String key);
------------------------------------------------------------
//清除所有Receiver
void clearReceivers();
------------------------------------------------------------
组共享数据池,组内的所有Receiver共享。通过GroupValue您可以写入数据也可以获取数据,您还可以设置监听器去监听某些值的更新。
代码示例:
public class CustomCover extends BaseCover{
//...
@Override
public void onReceiverBind() {
super.onReceiverBind();
getGroupValue().registerOnGroupValueUpdateListener(mOnGroupValueUpdateListener);
}
//...
private IReceiverGroup.OnGroupValueUpdateListener mOnGroupValueUpdateListener =
new IReceiverGroup.OnGroupValueUpdateListener() {
@Override
public String[] filterKeys() {
return new String[]{ DataInter.Key.KEY_COMPLETE_SHOW };
}
@Override
public void onValueUpdate(String key, Object value) {
//...
}
};
//...
@Override
public void onReceiverUnBind() {
super.onReceiverUnBind();
getGroupValue().unregisterOnGroupValueUpdateListener(mOnGroupValueUpdateListener);
}
}
public void putBoolean(String key, boolean value);
public void putInt(String key, int value);
public void putString(String key, String value);
public void putFloat(String key, float value);
public void putLong(String key, long value);
public void putDouble(String key, double value);
public void putObject(String key, Object value);
public boolean getBoolean(String key);
public boolean getBoolean(String key, boolean defaultValue);
public int getInt(String key);
public int getInt(String key, int defaultValue);
public String getString(String key);
public float getFloat(String key);
public float getFloat(String key, float defaultValue);
public long getLong(String key);
public long getLong(String key, long defaultValue);
public double getDouble(String key);
public double getDouble(String key, double defaultValue);
通过StateGetter可以获取到PlayerStateGetter。 PlayerStateGetter可以获取到播放状态
在Receiver的子类中均可以调用到方法getPlayerStateGetter();
PlayerStateGetter playerStateGetter = getPlayerStateGetter();
if(playerStateGetter!=null){
//解码器是否处于缓冲状态
boolean isBuffering = playerStateGetter.isBuffering();
//获取解码器状态,有以下取值
//int STATE_END = -2;
//int STATE_ERROR = -1;
//int STATE_IDLE = 0;
//int STATE_INITIALIZED = 1;
//int STATE_PREPARED = 2;
//int STATE_STARTED = 3;
//int STATE_PAUSED = 4;
//int STATE_STOPPED = 5;
//int STATE_PLAYBACK_COMPLETE = 6;
int state = playerStateGetter.getState();
}