七、Android视频框架PlayerBase#IProduce

2019-09-23  本文已影响0人  YongtaoHuang

上一篇:https://www.jianshu.com/p/53912268ec67

下一篇:
https://www.jianshu.com/p/5de2ee571728

接口IProducerGroup

IProducerGroup用来管理多个事件生成器

public interface IProducerGroup {
    // 增加事件生成器
    void addEventProducer(BaseEventProducer eventProducer);
    // 删除事件生成器
    boolean removeEventProducer(BaseEventProducer eventProducer);
    // 销毁
    void destroy();
}

类ProducerGroup 实现IProducerGroup

public final class ProducerGroup implements IProducerGroup {
    // 接受器事件发送器
    private ReceiverEventSender mEventSender;
    // 事件生成器的List
    private List<BaseEventProducer> mEventProducers;
    // 构造函数
    public ProducerGroup(ReceiverEventSender eventSender){
        this.mEventSender = eventSender;
        mEventProducers = new CopyOnWriteArrayList<>();
    }
    // 增加事件生成器
    @Override
    public void addEventProducer(BaseEventProducer eventProducer) {
        if(!mEventProducers.contains(eventProducer)){
            eventProducer.attachSender(mEventSender);
            mEventProducers.add(eventProducer);
            eventProducer.onAdded();
        }
    }
    // 删除事件生成器
    @Override
    public boolean removeEventProducer(BaseEventProducer eventProducer) {
        boolean remove = mEventProducers.remove(eventProducer);
        if(eventProducer!=null){
            eventProducer.onRemoved();
            eventProducer.attachSender(null);
        }
        return remove;
    }
    // 销毁
    @Override
    public void destroy() {
        for(BaseEventProducer eventProducer : mEventProducers){
            eventProducer.onRemoved();
            eventProducer.destroy();
            eventProducer.attachSender(null);
        }
        mEventProducers.clear();
    }
}

接口EventProducer

EventProducer事件生成器,设计的目的是为了让外部事件引入,服务于视图。
框架内包含了一个网络状态事件生成器,您可以通过配置引入。
如果您视图内有电量的显示或者有弹幕的需求等,您可以考虑使用事件生成器来处理数据和事件分发。

public interface EventProducer {

    void onAdded();

    void onRemoved();

    ReceiverEventSender getSender();

    void destroy();

}

抽象类BaseEventProducer实现接口EventProducer

public abstract class BaseEventProducer implements EventProducer {

    private ReceiverEventSender mReceiverEventSender;

    void attachSender(ReceiverEventSender receiverEventSender){
        this.mReceiverEventSender = receiverEventSender;
    }

    @Override
    public ReceiverEventSender getSender() {
        return mReceiverEventSender;
    }

}

SuperContainer中调用IProducerGroup

源代码中只有SuperContainer中调用了IProducerGroup。

public class SuperContainer extends FrameLayout implements OnTouchGestureListener {

    private IProducerGroup mProducerGroup;

    public SuperContainer(@NonNull Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        initBaseInfo(context);
        // ....
    }

    private void initBaseInfo(Context context) {
        mProducerGroup = new ProducerGroup(new ProducerEventSender(mDelegateReceiverEventSender));
    }

    // 添加事件生成器
    public void addEventProducer(BaseEventProducer eventProducer){
        mProducerGroup.addEventProducer(eventProducer);
    }

    // 删除事件生成器
    public boolean removeEventProducer(BaseEventProducer eventProducer){
        return mProducerGroup.removeEventProducer(eventProducer);
    }

    // 销毁
    public void destroy(){
        //clear ReceiverGroupChangeListener
        if(mReceiverGroup!=null){
            mReceiverGroup.removeOnReceiverGroupChangeListener(mInternalReceiverGroupChangeListener);
        }
        //destroy producer group
        mProducerGroup.destroy();
        //and remove render view.
        removeRender();
        //and remove all covers
        removeAllCovers();
    }   
}

BaseVideoView中的SuperContainer调用addEventProducer方法

superContainer.addEventProducer(new NetworkEventProducer(context));

    protected SuperContainer onCreateSuperContainer(Context context){
        SuperContainer superContainer = new SuperContainer(context);
        if(PlayerConfig.isUseDefaultNetworkEventProducer())
            superContainer.addEventProducer(new NetworkEventProducer(context));
        return superContainer;
    }
// 网络更改事件生成器,用于发送网络状态更改事件。
public class NetworkEventProducer extends BaseEventProducer {
上一篇下一篇

猜你喜欢

热点阅读