AdControlView.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.yc.ycvideoplayer.newPlayer.ad;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.animation.Animation;
  8. import android.widget.FrameLayout;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11. import androidx.annotation.NonNull;
  12. import androidx.annotation.Nullable;
  13. import org.yc.ycvideoplayer.R;
  14. import com.yc.video.bridge.ControlWrapper;
  15. import com.yc.video.config.ConstantKeys;
  16. import com.yc.video.ui.view.InterControlView;
  17. import com.yc.video.tool.PlayerUtils;
  18. public class AdControlView extends FrameLayout implements InterControlView, View.OnClickListener {
  19. private Context mContext;
  20. protected TextView mAdTime, mAdDetail;
  21. protected ImageView mBack, mVolume, mFullScreen, mPlayButton;
  22. protected AdControlListener mListener;
  23. private ControlWrapper mControlWrapper;
  24. public AdControlView(@NonNull Context context) {
  25. super(context);
  26. init(context);
  27. }
  28. public AdControlView(@NonNull Context context, @Nullable AttributeSet attrs) {
  29. super(context, attrs);
  30. init(context);
  31. }
  32. public AdControlView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  33. super(context, attrs, defStyleAttr);
  34. init(context);
  35. }
  36. private void init(Context context){
  37. this.mContext = context;
  38. LayoutInflater.from(getContext()).inflate(R.layout.layout_ad_control_view, this, true);
  39. mAdTime = findViewById(R.id.ad_time);
  40. mAdDetail = findViewById(R.id.ad_detail);
  41. mAdDetail.setText("了解详情>");
  42. mBack = findViewById(R.id.back);
  43. mBack.setVisibility(GONE);
  44. mVolume = findViewById(R.id.iv_volume);
  45. mFullScreen = findViewById(R.id.fullscreen);
  46. mPlayButton = findViewById(R.id.iv_play);
  47. mPlayButton.setOnClickListener(this);
  48. mAdTime.setOnClickListener(this);
  49. mAdDetail.setOnClickListener(this);
  50. mBack.setOnClickListener(this);
  51. mVolume.setOnClickListener(this);
  52. mFullScreen.setOnClickListener(this);
  53. this.setOnClickListener(new OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. if (mListener != null) mListener.onAdClick();
  57. }
  58. });
  59. }
  60. @Override
  61. public void attach(@NonNull ControlWrapper controlWrapper) {
  62. mControlWrapper = controlWrapper;
  63. }
  64. @Override
  65. public View getView() {
  66. return this;
  67. }
  68. @Override
  69. public void onVisibilityChanged(boolean isVisible, Animation anim) {
  70. }
  71. @Override
  72. public void onPlayStateChanged(int playState) {
  73. switch (playState) {
  74. case ConstantKeys.CurrentState.STATE_PLAYING:
  75. mControlWrapper.startProgress();
  76. mPlayButton.setSelected(true);
  77. break;
  78. case ConstantKeys.CurrentState.STATE_PAUSED:
  79. mPlayButton.setSelected(false);
  80. break;
  81. }
  82. }
  83. @Override
  84. public void onPlayerStateChanged(int playerState) {
  85. switch (playerState) {
  86. case ConstantKeys.PlayMode.MODE_NORMAL:
  87. mBack.setVisibility(GONE);
  88. mFullScreen.setSelected(false);
  89. break;
  90. case ConstantKeys.PlayMode.MODE_FULL_SCREEN:
  91. mBack.setVisibility(VISIBLE);
  92. mFullScreen.setSelected(true);
  93. break;
  94. }
  95. //暂未实现全面屏适配逻辑,需要你自己补全
  96. }
  97. @Override
  98. public void setProgress(int duration, int position) {
  99. if (mAdTime != null)
  100. mAdTime.setText(String.format("%s | 跳过", (duration - position) / 1000));
  101. }
  102. @Override
  103. public void onLockStateChanged(boolean isLocked) {
  104. }
  105. @Override
  106. public void onClick(View v) {
  107. int id = v.getId();
  108. if (id == R.id.back | id == R.id.fullscreen) {
  109. toggleFullScreen();
  110. } else if (id == R.id.iv_volume) {
  111. doMute();
  112. } else if (id == R.id.ad_detail) {
  113. if (mListener != null) mListener.onAdClick();
  114. } else if (id == R.id.ad_time) {
  115. if (mListener != null) mListener.onSkipAd();
  116. } else if (id == R.id.iv_play) {
  117. mControlWrapper.togglePlay();
  118. }
  119. }
  120. private void doMute() {
  121. mControlWrapper.setMute(!mControlWrapper.isMute());
  122. mVolume.setImageResource(mControlWrapper.isMute() ? R.drawable.ic_player_volume_up : R.drawable.ic_player_volume_off);
  123. }
  124. /**
  125. * 横竖屏切换
  126. */
  127. private void toggleFullScreen() {
  128. Activity activity = PlayerUtils.scanForActivity(getContext());
  129. mControlWrapper.toggleFullScreen(activity);
  130. }
  131. public void setListener(AdControlListener listener) {
  132. this.mListener = listener;
  133. }
  134. public interface AdControlListener {
  135. void onAdClick();
  136. void onSkipAd();
  137. }
  138. }