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