PlayerControllerView.java 16.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
package com.cnlive.gundam.video.view;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.databinding.ObservableLong;
import android.databinding.ViewDataBinding;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import com.cnlive.gundam.BR;
import com.cnlive.gundam.R;
import com.cnlive.gundam.databinding.LayoutPlayerControllerLiveLBinding;
import com.cnlive.gundam.databinding.LayoutPlayerControllerUgcLiveLBinding;
import com.cnlive.gundam.video.utils.PlayerUtils;
import com.cnlive.gundam.video.utils.VideoRateUtil;
import com.cnlive.libs.util.Config;

/**
 * Created by gujun on 2017/4/26.
 */

public class PlayerControllerView extends RelativeLayout implements SeekBar.OnSeekBarChangeListener {
    public static final int ID_CLICK_PLAY_BUTTON = 0x2001;

    public static final int VOD_L = 0x1001;
    public static final int VOD_P = 0x1002;
    public static final int LIVE_L = 0x1003;
    public static final int LIVE_P = 0x1004;
    public static final int UGC_VOD_L = 0x1005;
    public static final int UGC_VOD_P = 0x1006;
    public static final int UGC_LIVE_L = 0x1007;
    public static final int UGC_LIVE_P = 0x1008;

    public static final int RATE_L = 0x4001;
    public static final int RATE_M = 0x4002;
    public static final int RATE_H = 0x4003;
    public static final int RATE_HH = 0x4004;

    public static final int SWITCH_RATE_START = 0x5001;
    public static final int SWITCH_RATE_FINISH = 0x5002;
    public static final int SWITCH_RATE_ERROR = 0x5003;

    private static final int HIDE_RATE_TOAST = 0x6001;
    private static final int HIDE_RATE_TOAST_DELAY = 2000;

    private Context mContext;
    private VideoControllerListener l;

    private ViewDataBinding binding;

    //视频title
    private ObservableField<String> observableTitle;
    //controller是否显示
    private ObservableBoolean observableShow;
    //当时是否是播放状态
    private ObservableBoolean observableIsPlay;
    //当前播放进度
    private ObservableLong observableCurProgress;
    //总的播放进度
    private ObservableLong observableDurProgress;
    //记录当前视频是否是ugc
    private boolean isUgc = false;

    //记录当前选择的码率
    private ObservableInt observableRateType;
    //记录当前是否显示码率按钮
    private ObservableBoolean observableHasRate;
    //记录当前码率列表布局是否显示
    private ObservableBoolean observableShowRateListLayout;
    //记录当前码率列表布局是否显示
    private ObservableBoolean observableShowRateToastLayout;
    //记录切换码率视频状态
    private ObservableInt observableSwitchRateType;
    //记录要切换的码率文字
    private ObservableField<String> observableSwitchRateStr;
    //记录后台给的码率字符串"1,2,3,4"格式
    private ObservableField<String> observableRateStr;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HIDE_RATE_TOAST:
                    observableShowRateToastLayout.set(false);
                    break;
            }
        }
    };

    public PlayerControllerView(Context context) {
        this(context, null);
    }

    public PlayerControllerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayerControllerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        observableTitle = new ObservableField<>();
        observableShow = new ObservableBoolean();
        observableIsPlay = new ObservableBoolean();
        observableCurProgress = new ObservableLong();
        observableDurProgress = new ObservableLong();
        observableRateType = new ObservableInt();
        observableShowRateListLayout = new ObservableBoolean();
        observableShowRateToastLayout = new ObservableBoolean();
        observableSwitchRateType = new ObservableInt();
        observableHasRate = new ObservableBoolean();
        observableRateStr = new ObservableField<>();
        observableSwitchRateStr = new ObservableField<>();
        observableShow.set(true);
        observableRateType.set(RATE_M);
    }

    public void setOnControllerListener(VideoControllerListener listener) {
        l = listener;
    }

    public void setControllerLayout(int type) {
        if (binding != null)
            binding.unbind();
        switch (type) {
            case VOD_P:
                binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_vod_p));
                binding.setVariable(BR.playerControllerView, this);
                binding.setVariable(BR.title, observableTitle);
                binding.setVariable(BR.showController, observableShow);
                binding.setVariable(BR.isPlay, observableIsPlay);
                binding.setVariable(BR.durProgress, observableDurProgress);
                binding.setVariable(BR.curProgress, observableCurProgress);
                isUgc = false;
                break;
            case VOD_L:
                binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_vod_l));
                binding.setVariable(BR.playerControllerView, this);
                binding.setVariable(BR.rateType, observableRateType);
                binding.setVariable(BR.rateListStr, observableRateStr);
                binding.setVariable(BR.hasRate, observableHasRate);
                binding.setVariable(BR.showRateListLayout, observableShowRateListLayout);
                binding.setVariable(BR.showRateToastLayout, observableShowRateToastLayout);
                binding.setVariable(BR.curSwitchRateType, observableSwitchRateType);
                binding.setVariable(BR.curSwitchRateStr, observableSwitchRateStr);
                binding.setVariable(BR.title, observableTitle);
                binding.setVariable(BR.showController, observableShow);
                binding.setVariable(BR.isPlay, observableIsPlay);
                binding.setVariable(BR.durProgress, observableDurProgress);
                binding.setVariable(BR.curProgress, observableCurProgress);
                isUgc = false;
                break;
            case LIVE_P:
                binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_live_p));
                binding.setVariable(BR.playerControllerView, this);
                binding.setVariable(BR.title, observableTitle);
                binding.setVariable(BR.showController, observableShow);
                binding.setVariable(BR.isPlay, observableIsPlay);
                isUgc = false;
                break;
            case LIVE_L:
                binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_live_l));
                binding.setVariable(BR.playerControllerView, this);
                binding.setVariable(BR.rateType, observableRateType);
                binding.setVariable(BR.rateListStr, observableRateStr);
                binding.setVariable(BR.hasRate, observableHasRate);
                binding.setVariable(BR.showRateListLayout, observableShowRateListLayout);
                binding.setVariable(BR.showRateToastLayout, observableShowRateToastLayout);
                binding.setVariable(BR.curSwitchRateType, observableSwitchRateType);
                binding.setVariable(BR.curSwitchRateStr, observableSwitchRateStr);
                binding.setVariable(BR.title, observableTitle);
                binding.setVariable(BR.showController, observableShow);
                binding.setVariable(BR.isPlay, observableIsPlay);
                switchCompatChangeListener.onSwitchCompatChange(((LayoutPlayerControllerLiveLBinding) this.binding).barrageTop);
                isUgc = false;
                break;
            case UGC_VOD_P:
                this.binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_ugc_vod_p));
                this.binding.setVariable(BR.playerControllerView, this);
                this.binding.setVariable(BR.title, observableTitle);
                this.binding.setVariable(BR.showController, observableShow);
                this.binding.setVariable(BR.isPlay, observableIsPlay);
                this.binding.setVariable(BR.durProgress, observableDurProgress);
                this.binding.setVariable(BR.curProgress, observableCurProgress);
                isUgc = true;
                break;
            case UGC_VOD_L:
                this.binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_ugc_vod_l));
                this.binding.setVariable(BR.playerControllerView, this);
                this.binding.setVariable(BR.title, observableTitle);
                this.binding.setVariable(BR.showController, observableShow);
                this.binding.setVariable(BR.isPlay, observableIsPlay);
                this.binding.setVariable(BR.durProgress, observableDurProgress);
                this.binding.setVariable(BR.curProgress, observableCurProgress);
                isUgc = true;
                break;
            case UGC_LIVE_P:
                this.binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_ugc_live_p));
                this.binding.setVariable(BR.playerControllerView, this);
                this.binding.setVariable(BR.title, observableTitle);
                this.binding.setVariable(BR.showController, observableShow);
                this.binding.setVariable(BR.isPlay, observableIsPlay);
                isUgc = true;
                break;
            case UGC_LIVE_L:
                this.binding = DataBindingUtil.bind(getView(R.layout.layout_player_controller_ugc_live_l));
                this.binding.setVariable(BR.playerControllerView, this);
                this.binding.setVariable(BR.title, observableTitle);
                this.binding.setVariable(BR.showController, observableShow);
                this.binding.setVariable(BR.isPlay, observableIsPlay);
                switchCompatChangeListener.onSwitchCompatChange(((LayoutPlayerControllerUgcLiveLBinding) this.binding).barrageTop);
                isUgc = true;
                break;
        }
    }

    //获取对应的controller
    private View getView(int layout) {
        View view = LayoutInflater.from(mContext).inflate(layout, null, false);
        if (getChildCount() > 0)
            removeAllViews();
        addView(view);
        return view;
    }

    //设置是否显示码率按钮
    public void setRateInfo(String rateStr) {
        observableRateType.set(RATE_M);
        observableHasRate.set(!TextUtils.isEmpty(rateStr) && !rateStr.equals("2"));
        observableRateStr.set(rateStr);
    }

    //设置当前选中的码率
    public void setCurRate(String curRate) {
        switch (curRate) {
            case Config.TYPE_PLAY_RATE_1:
                observableRateType.set(RATE_L);
                break;
            case Config.TYPE_PLAY_RATE_2:
                observableRateType.set(RATE_M);
                break;
            case Config.TYPE_PLAY_RATE_3:
                observableRateType.set(RATE_H);
                break;
            case Config.TYPE_PLAY_RATE_4:
                observableRateType.set(RATE_HH);
                break;
        }
    }

    //设置title
    public void setTitle(String title) {
        observableTitle.set(title);
    }

    //设置播放状态
    public void setIsPlay(boolean isPlay) {
        observableIsPlay.set(isPlay);
    }

    //设置当前播放进度
    public void setCurProgress(long curProgress) {
        observableCurProgress.set(curProgress);
    }

    //设置总共播放长度
    public void setDurProgress(long durProgress) {
        observableDurProgress.set(durProgress);
    }

    //改变controller的显示隐藏
    public void changeVisible() {
        //切换显示隐藏状态时,当rate布局显示时controller不修改显示状态
        if (!observableShow.get() && observableShowRateListLayout.get()) {
            observableShowRateListLayout.set(false);
        } else {
            observableShow.set(!observableShow.get());
        }
    }

    //设置切换码率提示显示和隐藏
    public void setSwitchRateType(int switchRateType, String curRate) {
        observableSwitchRateStr.set(VideoRateUtil.getRateText(curRate));
        observableSwitchRateType.set(switchRateType);
        if (switchRateType == SWITCH_RATE_START) {
            observableShowRateToastLayout.set(true);
        } else {
            mHandler.sendEmptyMessageDelayed(HIDE_RATE_TOAST, HIDE_RATE_TOAST_DELAY);
        }
    }

    //获取当前controller是否显示
    public boolean isShow() {
        return observableShow.get();
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fullScreen:
                if (PlayerUtils.isPortrait(getContext())) {
                    ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
                break;
            case R.id.back:
                if (!(PlayerUtils.isPortrait(getContext())) && !isUgc) {
                    ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else {
                    ((Activity) mContext).onBackPressed();
                }
                break;
            case R.id.playBtn:
                if (l != null) l.onControllerViewClick(ID_CLICK_PLAY_BUTTON);
                break;
            case R.id.rate:
                if (!observableShowRateListLayout.get()) {
                    observableShowRateListLayout.set(true);
                    observableShow.set(false);
                }
                break;
            case R.id.rateOutClickView:
                if (!observableShow.get())
                    observableShow.set(true);
                observableShowRateListLayout.set(false);
                break;
            case R.id.rateL:
                if (observableRateType.get() != RATE_L) {
                    observableShowRateListLayout.set(false);
                    observableRateType.set(RATE_L);
                    observableShow.set(true);
                    if (l != null) l.onSwitchRate(Config.TYPE_PLAY_RATE_1);
                }
                break;
            case R.id.rateM:
                if (observableRateType.get() != RATE_M) {
                    observableShowRateListLayout.set(false);
                    observableRateType.set(RATE_M);
                    observableShow.set(true);
                    if (l != null) l.onSwitchRate(Config.TYPE_PLAY_RATE_2);
                }
                break;
            case R.id.rateH:
                if (observableRateType.get() != RATE_H) {
                    observableShowRateListLayout.set(false);
                    observableRateType.set(RATE_H);
                    observableShow.set(true);
                    if (l != null) l.onSwitchRate(Config.TYPE_PLAY_RATE_3);
                }
                break;
            case R.id.rateHH:
                if (observableRateType.get() != RATE_HH) {
                    observableShowRateListLayout.set(false);
                    observableRateType.set(RATE_HH);
                    observableShow.set(true);
                    if (l != null) l.onSwitchRate(Config.TYPE_PLAY_RATE_4);
                }
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (l != null && fromUser)
            l.onSeekProgressChange(progress);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (l != null) l.onSeekComplete();
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mHandler != null && mHandler.hasMessages(HIDE_RATE_TOAST)) {
            mHandler.removeMessages(HIDE_RATE_TOAST);
        }
        super.onDetachedFromWindow();
    }

    public interface VideoControllerListener {
        void onControllerViewClick(int id);

        void onSeekProgressChange(int progress);

        void onSeekComplete();

        void onSwitchRate(String rate);
    }

    public interface  SwitchCompatChangeListener{
       void onSwitchCompatChange(SwitchCompat barrageTop);
    }
    private  SwitchCompatChangeListener switchCompatChangeListener;
    public void setonswitchCompatChangeListene(SwitchCompatChangeListener switchCompatChangeListener){
        this.switchCompatChangeListener=switchCompatChangeListener;
    }



}