Support tv background play

pull/587/head
FongMi 10 months ago
parent 3c634f6077
commit f15cde9e30
  1. 40
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/CastActivity.java
  2. 67
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java
  3. 8
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/SettingPlayerActivity.java
  4. 79
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/VideoActivity.java
  5. 29
      app/src/leanback/res/layout/activity_setting_player.xml

@ -35,6 +35,7 @@ import com.fongmi.android.tv.event.PlayerEvent;
import com.fongmi.android.tv.event.RefreshEvent;
import com.fongmi.android.tv.player.Players;
import com.fongmi.android.tv.player.exo.ExoUtil;
import com.fongmi.android.tv.service.PlaybackService;
import com.fongmi.android.tv.ui.base.BaseActivity;
import com.fongmi.android.tv.ui.custom.CustomKeyDownCast;
import com.fongmi.android.tv.ui.dialog.SubtitleDialog;
@ -59,6 +60,7 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
private Runnable mR1;
private Runnable mR2;
private Clock mClock;
private boolean redirect;
private long position;
private long duration;
private int scale;
@ -137,6 +139,7 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
private void setVideoView() {
mPlayers.init(mBinding.exo);
PlaybackService.start(mPlayers);
setScale(scale = Setting.getScale());
ExoUtil.setSubtitleView(mBinding.exo);
findViewById(R.id.timeBar).setNextFocusUpId(R.id.reset);
@ -185,6 +188,7 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
private void onChoose() {
mPlayers.choose(this, mBinding.widget.title.getText());
setRedirect(true);
}
private void onDecode() {
@ -263,6 +267,18 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
App.post(mR1, Constant.INTERVAL_HIDE);
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
private void checkPlayImg() {
ActionEvent.update();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onActionEvent(ActionEvent event) {
if (ActionEvent.PLAY.equals(event.getAction()) || ActionEvent.PAUSE.equals(event.getAction())) {
@ -294,6 +310,7 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
break;
case Player.STATE_READY:
hideProgress();
checkPlayImg();
mPlayers.reset();
setState(RenderState.PLAYING);
break;
@ -337,6 +354,7 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
private void onPaused() {
setState(RenderState.PAUSED);
mPlayers.pause();
checkPlayImg();
showInfo();
}
@ -344,6 +362,7 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
if (mPlayers.isEmpty()) return;
setState(RenderState.PLAYING);
mPlayers.play();
checkPlayImg();
hideCenter();
}
@ -487,18 +506,31 @@ public class CastActivity extends BaseActivity implements CustomKeyDownCast.List
onKeyCenter();
}
@Override
protected void onStart() {
super.onStart();
mClock.stop().start();
onPlay();
}
@Override
protected void onResume() {
super.onResume();
mClock.start();
onPlay();
if (isRedirect()) onPlay();
setRedirect(false);
}
@Override
protected void onPause() {
super.onPause();
mPlayers.pause();
mClock.stop();
if (isRedirect()) onPaused();
}
@Override
protected void onStop() {
super.onStop();
if (Setting.isBackgroundOff()) onPaused();
if (Setting.isBackgroundOff()) mClock.stop();
}
@Override

@ -12,6 +12,7 @@ import androidx.annotation.Nullable;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.ItemBridgeAdapter;
import androidx.leanback.widget.OnChildViewHolderSelectedListener;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.media3.common.C;
import androidx.media3.common.Player;
@ -44,6 +45,7 @@ import com.fongmi.android.tv.model.LiveViewModel;
import com.fongmi.android.tv.player.Players;
import com.fongmi.android.tv.player.exo.ExoUtil;
import com.fongmi.android.tv.server.Server;
import com.fongmi.android.tv.service.PlaybackService;
import com.fongmi.android.tv.ui.base.BaseActivity;
import com.fongmi.android.tv.ui.custom.CustomKeyDownLive;
import com.fongmi.android.tv.ui.custom.CustomLiveListView;
@ -73,7 +75,9 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
private ArrayObjectAdapter mChannelAdapter;
private ArrayObjectAdapter mEpgDataAdapter;
private ArrayObjectAdapter mGroupAdapter;
private Observer<Channel> mObserveUrl;
private CustomKeyDownLive mKeyDown;
private Observer<Epg> mObserveEpg;
private LiveViewModel mViewModel;
private List<Group> mHides;
private Players mPlayers;
@ -87,6 +91,7 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
private Runnable mR4;
private Clock mClock;
private boolean initTrack;
private boolean redirect;
private int count;
public static void start(Context context) {
@ -124,6 +129,8 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
mClock = Clock.create(mBinding.widget.clock);
mKeyDown = CustomKeyDownLive.create(this);
mPlayers = Players.create(this);
mObserveEpg = this::setEpg;
mObserveUrl = this::start;
mHides = new ArrayList<>();
mR0 = this::setActivated;
mR1 = this::hideControl;
@ -180,6 +187,7 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
private void setVideoView() {
mPlayers.init(mBinding.exo);
PlaybackService.start(mPlayers);
setScale(Setting.getLiveScale());
ExoUtil.setSubtitleView(mBinding.exo);
findViewById(R.id.timeBar).setNextFocusUpId(R.id.player);
@ -202,9 +210,9 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
private void setViewModel() {
mViewModel = new ViewModelProvider(this).get(LiveViewModel.class);
mViewModel.url.observe(this, result -> mPlayers.start(result, getTimeout()));
mViewModel.url.observeForever(mObserveUrl);
mViewModel.xml.observe(this, this::setEpg);
mViewModel.epg.observe(this, this::setEpg);
mViewModel.epg.observeForever(mObserveEpg);
mViewModel.live.observe(this, live -> {
mViewModel.getXml(live);
hideProgress();
@ -314,8 +322,8 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
}
private void checkPlay() {
if (mPlayers.isPlaying()) mPlayers.pause();
else mPlayers.play();
if (mPlayers.isPlaying()) onPaused();
else onPlay();
}
private void onTrack(View view) {
@ -373,6 +381,7 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
private void onChoose() {
mPlayers.choose(this, mBinding.widget.title.getText());
setRedirect(true);
}
private void onDecode() {
@ -619,6 +628,14 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
showProgress();
}
private void start(Channel result) {
mPlayers.start(result, getTimeout());
}
private void checkPlayImg() {
ActionEvent.update();
}
private void resetAdapter() {
mBinding.widget.epgData.getLayoutParams().width = 0;
mBinding.channel.getLayoutParams().width = 0;
@ -712,6 +729,7 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
break;
case Player.STATE_READY:
hideProgress();
checkPlayImg();
mPlayers.reset();
break;
case Player.STATE_ENDED:
@ -816,6 +834,16 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
hideCenter();
}
private void onPaused() {
mPlayers.pause();
checkPlayImg();
}
private void onPlay() {
mPlayers.play();
checkPlayImg();
}
private boolean isInitTrack() {
return initTrack;
}
@ -824,6 +852,14 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
this.initTrack = initTrack;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (isVisible(mBinding.control.getRoot())) setR1Callback();
@ -935,18 +971,31 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
else onMenu();
}
@Override
protected void onStart() {
super.onStart();
mClock.stop().start();
onPlay();
}
@Override
protected void onResume() {
super.onResume();
mPlayers.play();
mClock.start();
if (isRedirect()) onPlay();
setRedirect(false);
}
@Override
protected void onPause() {
super.onPause();
mPlayers.pause();
mClock.stop();
if (isRedirect()) onPaused();
}
@Override
protected void onStop() {
super.onStop();
if (Setting.isBackgroundOff()) onPaused();
if (Setting.isBackgroundOff()) mClock.stop();
}
@Override
@ -968,6 +1017,8 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
protected void onDestroy() {
super.onDestroy();
mPlayers.release();
mViewModel.url.removeObserver(mObserveUrl);
mViewModel.epg.removeObserver(mObserveEpg);
App.removeCallbacks(mR0, mR1, mR3, mR3, mR4);
}
}

@ -52,6 +52,7 @@ public class SettingPlayerActivity extends BaseActivity implements UaCallback, B
mBinding.tunnelText.setText(getSwitch(Setting.isTunnel()));
mBinding.speedText.setText(format.format(Setting.getSpeed()));
mBinding.bufferText.setText(String.valueOf(Setting.getBuffer()));
mBinding.backgroundText.setText(getSwitch(Setting.isBackgroundOn()));
mBinding.audioDecodeText.setText(getSwitch(Setting.isAudioPrefer()));
mBinding.rtspText.setText((rtsp = ResUtil.getStringArray(R.array.select_rtsp))[Setting.getRtsp()]);
mBinding.scaleText.setText((scale = ResUtil.getStringArray(R.array.select_scale))[Setting.getScale()]);
@ -70,10 +71,12 @@ public class SettingPlayerActivity extends BaseActivity implements UaCallback, B
mBinding.tunnel.setOnClickListener(this::setTunnel);
mBinding.caption.setOnClickListener(this::setCaption);
mBinding.caption.setOnLongClickListener(this::onCaption);
mBinding.background.setOnClickListener(this::onBackground);
mBinding.audioDecode.setOnClickListener(this::setAudioDecode);
}
private void setVisible() {
if (Setting.getBackground() == 2) Setting.putBackground(1);
mBinding.caption.setVisibility(Setting.hasCaption() ? View.VISIBLE : View.GONE);
}
@ -146,4 +149,9 @@ public class SettingPlayerActivity extends BaseActivity implements UaCallback, B
Setting.putAudioPrefer(!Setting.isAudioPrefer());
mBinding.audioDecodeText.setText(getSwitch(Setting.isAudioPrefer()));
}
private void onBackground(View view) {
Setting.putBackground(Setting.isBackgroundOn() ? 0 : 1);
mBinding.backgroundText.setText(getSwitch(Setting.isBackgroundOn()));
}
}

@ -24,6 +24,7 @@ import androidx.fragment.app.FragmentActivity;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.ItemBridgeAdapter;
import androidx.leanback.widget.OnChildViewHolderSelectedListener;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.media3.common.C;
import androidx.media3.common.Player;
@ -57,6 +58,7 @@ import com.fongmi.android.tv.event.RefreshEvent;
import com.fongmi.android.tv.model.SiteViewModel;
import com.fongmi.android.tv.player.Players;
import com.fongmi.android.tv.player.exo.ExoUtil;
import com.fongmi.android.tv.service.PlaybackService;
import com.fongmi.android.tv.ui.adapter.QualityAdapter;
import com.fongmi.android.tv.ui.base.BaseActivity;
import com.fongmi.android.tv.ui.custom.CustomKeyDownVod;
@ -109,6 +111,9 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
private ArrayObjectAdapter mQuickAdapter;
private ArrayObjectAdapter mFlagAdapter;
private ArrayObjectAdapter mPartAdapter;
private Observer<Result> mObserveDetail;
private Observer<Result> mObservePlayer;
private Observer<Result> mObserveSearch;
private QualityAdapter mQualityAdapter;
private FlagPresenter mFlagPresenter;
private PartPresenter mPartPresenter;
@ -118,12 +123,12 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
private List<String> mBroken;
private History mHistory;
private Players mPlayers;
private boolean background;
private boolean fullscreen;
private boolean initTrack;
private boolean initAuto;
private boolean autoMode;
private boolean useParse;
private boolean redirect;
private Runnable mR1;
private Runnable mR2;
private Runnable mR3;
@ -266,13 +271,15 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
mFrameParams = mBinding.video.getLayoutParams();
mClock = Clock.create(mBinding.widget.clock);
mKeyDown = CustomKeyDownVod.create(this);
mObserveDetail = this::setDetail;
mObservePlayer = this::setPlayer;
mObserveSearch = this::setSearch;
mPlayers = Players.create(this);
mBroken = new ArrayList<>();
mR1 = this::hideControl;
mR2 = this::updateFocus;
mR3 = this::setTraffic;
mR4 = this::showEmpty;
setBackground(false);
setRecyclerView();
setVideoView();
setViewModel();
@ -362,6 +369,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
private void setVideoView() {
mPlayers.init(mBinding.exo);
PlaybackService.start(mPlayers);
ExoUtil.setSubtitleView(mBinding.exo);
mBinding.control.decode.setText(mPlayers.getDecodeText());
mBinding.control.reset.setText(ResUtil.getStringArray(R.array.select_reset)[Setting.getReset()]);
@ -378,9 +386,9 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
private void setViewModel() {
mViewModel = new ViewModelProvider(this).get(SiteViewModel.class);
mViewModel.result.observe(this, this::setDetail);
mViewModel.player.observe(this, this::setPlayer);
mViewModel.search.observe(this, this::setSearch);
mViewModel.result.observeForever(mObserveDetail);
mViewModel.player.observeForever(mObservePlayer);
mViewModel.search.observeForever(mObserveSearch);
}
private void checkCast() {
@ -494,6 +502,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
@Override
public void onClick(@NonNull View view) {
VodActivity.start(getActivity(), getKey(), result);
setRedirect(true);
}
};
}
@ -820,6 +829,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
private void onChoose() {
mPlayers.choose(this, mBinding.widget.title.getText());
setRedirect(true);
}
private void onDecode() {
@ -967,6 +977,10 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
mHistory.setPosition(replay ? C.TIME_UNSET : mHistory.getPosition());
}
private void checkPlayImg() {
ActionEvent.update();
}
private void checkKeep() {
mBinding.keep.setCompoundDrawablesWithIntrinsicBounds(Keep.find(getHistoryKey()) == null ? R.drawable.ic_detail_keep_off : R.drawable.ic_detail_keep_on, 0, 0, 0);
}
@ -1007,7 +1021,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
@Subscribe(threadMode = ThreadMode.MAIN)
public void onActionEvent(ActionEvent event) {
if (isBackground()) return;
if (isRedirect()) return;
if (ActionEvent.PLAY.equals(event.getAction()) || ActionEvent.PAUSE.equals(event.getAction())) {
onKeyCenter();
} else if (ActionEvent.NEXT.equals(event.getAction())) {
@ -1021,7 +1035,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRefreshEvent(RefreshEvent event) {
if (isBackground()) return;
if (isRedirect()) return;
if (event.getType() == RefreshEvent.Type.DETAIL) getDetail();
else if (event.getType() == RefreshEvent.Type.PLAYER) onRefresh();
else if (event.getType() == RefreshEvent.Type.SUBTITLE) mPlayers.setSub(Sub.from(event.getPath()));
@ -1029,7 +1043,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPlayerEvent(PlayerEvent event) {
if (isBackground()) return;
if (isRedirect()) return;
switch (event.getState()) {
case PlayerEvent.PREPARE:
setInitTrack(true);
@ -1041,6 +1055,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
break;
case Player.STATE_READY:
hideProgress();
checkPlayImg();
mPlayers.reset();
break;
case Player.STATE_ENDED:
@ -1068,6 +1083,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
checkNext(notify);
checkPlayImg();
}
}
@ -1093,7 +1109,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
@Subscribe(threadMode = ThreadMode.MAIN)
public void onErrorEvent(ErrorEvent event) {
if (isBackground()) return;
if (isRedirect()) return;
if (mPlayers.retried()) onError(event);
else onRefresh();
}
@ -1225,6 +1241,7 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
if (isFullscreen()) showInfo();
else hideInfo();
mPlayers.pause();
checkPlayImg();
}
private void onPlay() {
@ -1232,17 +1249,10 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (!mPlayers.isEmpty() && mPlayers.isIdle()) mPlayers.prepare();
mPlayers.play();
checkPlayImg();
hideCenter();
}
public boolean isBackground() {
return background;
}
public void setBackground(boolean background) {
this.background = background;
}
private boolean isFullscreen() {
return fullscreen;
}
@ -1283,6 +1293,14 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
this.useParse = useParse;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
private View getFocus1() {
return mFocus1 == null ? mBinding.video : mFocus1;
}
@ -1367,20 +1385,31 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
if (resultCode == RESULT_OK && requestCode == 1001) mPlayers.checkData(data);
}
@Override
protected void onStart() {
super.onStart();
mClock.stop().start();
onPlay();
}
@Override
protected void onResume() {
super.onResume();
setBackground(false);
mClock.start();
onPlay();
if (isRedirect()) onPlay();
setRedirect(false);
}
@Override
protected void onPause() {
super.onPause();
setBackground(true);
mPlayers.pause();
mClock.stop();
if (isRedirect()) onPaused();
}
@Override
protected void onStop() {
super.onStop();
if (Setting.isBackgroundOff()) onPaused();
if (Setting.isBackgroundOff()) mClock.stop();
}
@Override
@ -1404,6 +1433,10 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
mClock.release();
mPlayers.release();
RefreshEvent.history();
PlaybackService.stop();
App.removeCallbacks(mR1, mR2, mR3, mR4);
mViewModel.result.removeObserver(mObserveDetail);
mViewModel.player.removeObserver(mObservePlayer);
mViewModel.search.removeObserver(mObserveSearch);
}
}

@ -234,6 +234,35 @@
</LinearLayout>
<LinearLayout
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/selector_item"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/player_background"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/backgroundText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textColor="@color/white"
android:textSize="18sp"
tools:text="關" />
</LinearLayout>
<LinearLayout
android:id="@+id/rtsp"
android:layout_width="match_parent"

Loading…
Cancel
Save