接口IEventDispatcher
IEventDispatcher是一个事件分发接口。
public interface IEventDispatcher {
// 分发播放器相关事件
void dispatchPlayEvent(int eventCode, Bundle bundle);
void dispatchErrorEvent(int eventCode, Bundle bundle);
void dispatchReceiverEvent(int eventCode, Bundle bundle);
void dispatchReceiverEvent(int eventCode, Bundle bundle, IReceiverGroup.OnReceiverFilter onReceiverFilter);
void dispatchProducerEvent(int eventCode, Bundle bundle, IReceiverGroup.OnReceiverFilter onReceiverFilter);
void dispatchProducerData(String key, Object data, IReceiverGroup.OnReceiverFilter onReceiverFilter);
// 分发触摸相关事件
void dispatchTouchEventOnSingleTabUp(MotionEvent event);
void dispatchTouchEventOnDoubleTabUp(MotionEvent event);
void dispatchTouchEventOnDown(MotionEvent event);
void dispatchTouchEventOnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
void dispatchTouchEventOnEndGesture();
}
接口OnPlayerEventListener
public interface OnPlayerEventListener {
// 定义了错误代码-99000到-99052
void onPlayerEvent(int eventCode, Bundle bundle);
}
接口OnErrorEventListener
public interface OnErrorEventListener {
// 定义了错误代码-88000到-88018
void onErrorEvent(int eventCode, Bundle bundle);
}
EventDispatcher类
EventDispatcher类实现了接口IEventDispatcher。
EventDispatcher类内部含有一个事件接受器组IReceiverGroup。
IReceiverGroup循环访问组内的Receiver,通过onEach()方法进行响应。
public final class EventDispatcher implements IEventDispatcher{
// 事件接收器组
private IReceiverGroup mReceiverGroup;
// 构造函数
public EventDispatcher(IReceiverGroup receiverGroup){
this.mReceiverGroup = receiverGroup;
}
// 播放事件发生器
@Override
public void dispatchPlayEvent(final int eventCode, final Bundle bundle){
DebugLog.onPlayEventLog(eventCode, bundle);
switch (eventCode){
case OnPlayerEventListener.PLAYER_EVENT_ON_TIMER_UPDATE:
mReceiverGroup.forEach(new IReceiverGroup.OnLoopListener() {
@Override
public void onEach(IReceiver receiver) {
if(receiver instanceof OnTimerUpdateListener && bundle!=null)
((OnTimerUpdateListener)receiver).onTimerUpdate(
bundle.getInt(EventKey.INT_ARG1),
bundle.getInt(EventKey.INT_ARG2),
bundle.getInt(EventKey.INT_ARG3));
receiver.onPlayerEvent(eventCode, bundle);
}
});
break;
default:
mReceiverGroup.forEach(new IReceiverGroup.OnLoopListener() {
@Override
public void onEach(IReceiver receiver) {
receiver.onPlayerEvent(eventCode, bundle);
}
});
break;
}
recycleBundle(bundle);
}
}
接口IEventDispatcher的使用
SuperContainer类的setReceiverGroup()方法
mEventDispatcher = new EventDispatcher(receiverGroup);新建了一个EventDispatcher对象。
public class SuperContainer extends FrameLayout implements OnTouchGestureListener {
private IReceiverGroup mReceiverGroup;
private IEventDispatcher mEventDispatcher;
public final void dispatchPlayEvent(int eventCode, Bundle bundle){
if(mEventDispatcher !=null)
mEventDispatcher.dispatchPlayEvent(eventCode, bundle);
}
public final void dispatchErrorEvent(int eventCode, Bundle bundle){
if(mEventDispatcher !=null)
mEventDispatcher.dispatchErrorEvent(eventCode, bundle);
}
private DelegateReceiverEventSender mDelegateReceiverEventSender =
new DelegateReceiverEventSender() {
@Override
public void sendEvent(int eventCode, Bundle bundle,
IReceiverGroup.OnReceiverFilter receiverFilter) {
if(mEventDispatcher!=null)
mEventDispatcher.dispatchProducerEvent(eventCode, bundle, receiverFilter);
}
@Override
public void sendObject(String key, Object value, IReceiverGroup.OnReceiverFilter receiverFilter) {
if(mEventDispatcher!=null)
mEventDispatcher.dispatchProducerData(key, value, receiverFilter);
}
};
public final void setReceiverGroup(IReceiverGroup receiverGroup){
// ...
this.mReceiverGroup = receiverGroup;
//init event dispatcher.
mEventDispatcher = new EventDispatcher(receiverGroup);
// ...
}
//receiver event listener, a bridge for some receivers communication.
private OnReceiverEventListener mInternalReceiverEventListener =
new OnReceiverEventListener() {
@Override
public void onReceiverEvent(int eventCode, Bundle bundle) {
if(mOnReceiverEventListener!=null)
mOnReceiverEventListener.onReceiverEvent(eventCode, bundle);
if(mEventDispatcher !=null)
mEventDispatcher.dispatchReceiverEvent(eventCode, bundle);
}
};
}
BaseVideoView的中的setReceiverGroup()方法
public class BaseVideoView extends FrameLayout implements IVideoView, IStyleSetter {
private SuperContainer mSuperContainer;
public void setReceiverGroup(IReceiverGroup receiverGroup){
mSuperContainer.setReceiverGroup(receiverGroup);
}
}
我们在使用时mReceiverGroup可加可不加,不加就只有播放功能。
private BaseVideoView mVideoView;
private ReceiverGroup mReceiverGroup;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_video_view);
mVideoView = findViewById(R.id.myBaseVideoView);
mReceiverGroup = ReceiverGroupManager.get().getReceiverGroup(this);
mReceiverGroup.getGroupValue().putBoolean(DataInter.Key.KEY_CONTROLLER_TOP_ENABLE, true);
mVideoView.setReceiverGroup(mReceiverGroup);
DataSource dataSource = new DataSource(DataUtils.VIDEO_URL_09);
dataSource.setTitle("音乐和艺术如何改变世界");
mVideoView.setDataSource(dataSource);
mVideoView.start();
}
网友评论