我有一个适用于 Android 手机/平板电脑以及 Amazon Fire TV 的 Android 应用程序。屏幕中央有一个播放/暂停按钮,按下时可在彼此之间切换。使用此按钮时,视频流播放和暂停正常。这是我的 ExoPlayerActivity 类的第一部分代码:
public class ExoPlayerActivity extends ActivityBase implements ExoPlayerView, PlaybackControlView.VisibilityListener,
ExoPlayer.EventListener, View.OnTouchListener {
private final static String TAG = " [ExoPlayerActivity] ";
private ExoPlayerPresenter exoplayerPresenter;
protected String userAgent;
private DataSource.Factory mediaDataSourceFactory;
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private DefaultTrackSelector trackSelector;
private boolean shouldAutoPlay;
public static String playlink;
private LinearLayout debugRootView;
private TextView debugTextView;
private Button retryButton;
private Handler mainHandler;
private boolean playerNeedsSource;
// private ImageView channelPoster;
private TextView channelTitle;
private ImageView play, pause;
private ProgressBar progressBar;
private RelativeLayout channelDetail;
private RecyclerView channelRecyclerView;
private float x1,x2;
private View rootView;
private Timer timer;
private TimerTask timerTask;
private int delayTime;
private boolean isPaused;
@Inject
private Logger logger;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
InjectFactory.injectMembers(this);
exoplayerPresenter = new ExoPlayerPresenter(ExoPlayerActivity.this, this);
shouldAutoPlay = true;
mediaDataSourceFactory = buildDataSourceFactory(true);
userAgent = Util.getUserAgent(this, "ExoPlayer");
mainHandler = new Handler();
playlink = getIntent().getExtras().getString("playLink");
logger.info(TAG+"Play Link is: " + playlink);
setContentView(R.layout.activity_exoplayer);
rootView = findViewById(R.id.root);
play = (ImageView) findViewById(R.id.play);
pause = (ImageView) findViewById(R.id.pause);
play.setVisibility(View.INVISIBLE);
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);
simpleExoPlayerView.requestFocus();
simpleExoPlayerView.setOnTouchListener(this);
simpleExoPlayerView.setControllerVisibilityListener(this);
channelDetail = (RelativeLayout) findViewById(R.id.player_details);
channelTitle = (TextView) findViewById(R.id.airing_title);
channelRecyclerView = (RecyclerView) findViewById(R.id.airingRecyclerView);
progressBar = (ProgressBar) findViewById(R.id.player_loading);
progressBar.setVisibility(View.INVISIBLE);
isPaused = false;
// Button Listener
setListener();
// keep screen on
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Timer Task
setTimerTask();
// setting up channel titles, poster, and airing list at bottom
setFullScreen();
setChannelRecyclerView();
updatePlayerViewDetails();
}
private void setListener() {
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.setPlayWhenReady(true);
play.setVisibility(View.INVISIBLE);
pause.setVisibility(View.VISIBLE);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.setPlayWhenReady(false);
play.setVisibility(View.VISIBLE);
pause.setVisibility(View.INVISIBLE);
}
});
simpleExoPlayerView.setControllerVisibilityListener(new PlaybackControlView.VisibilityListener() {
@Override
public void onVisibilityChange(int visibility) {
simpleExoPlayerView.hideController();
}
});
}
请注意,在 setListener() 方法中,当单击暂停按钮时,会调用 player.setPlayWhenReady(false) 来暂停流,并将按钮切换为播放符号。相反,当按下播放按钮时调用 player.setPlayWhenReady(true),再次将按钮切换为暂停符号。
现在我的 onKeyUp 方法用于注册 Amazon Fire TV 输入。在我的 OnCreate 中,我最初设置 isPaused = false。这是我的 onKeyUp 方法:
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
boolean handled = false;
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
//TODO: fix play pause logic
if(isPaused=false){
player.setPlayWhenReady(true);
toggleControlsVisibility();
isPaused=true;
handled=true;
break;
}
else if (isPaused=true){
player.setPlayWhenReady(false);
toggleControlsVisibility();
isPaused=false;
handled=true;
break;
}
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
//go forward one channel
exoplayerPresenter.onSwipeLeft();
handled=true;
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
//go back one channel
exoplayerPresenter.onSwipeRight();
handled=true;
break;
case KeyEvent.KEYCODE_MENU:
toggleControlsVisibility();
handled=true;
break;
}
return handled || super.onKeyUp(keyCode, event);
}
假设第一个 if 语句是 case#1,else if 是 case #2。当我第一次按下遥控器上的播放/暂停按钮时,它会暂停流。这部分对我来说意义不大,因为我在 onCreate 中将 isPaused 设置为 false。案例#1不应该是被执行的案例吗?在这种情况下,我调用 setPlayWhenReady(true) 应该只是保持流媒体播放。但不知何故,它暂停了流。然后,一旦流暂停,我再次按下遥控器上的播放/暂停按钮,但它不会恢复流。
我一直在研究我的逻辑,似乎无法弄清楚发生了什么。我尝试使用 onKeyDown 而不是 onKeyUp 但结果没有区别。Android Studio 还警告我 isPaused = true 总是正确的。这只会增加我的困惑,因为在 onCreate 中,isPaused 是错误的。只有当我按下按钮时它才会变为真(我不会在代码中的其他任何地方更改 isPaused 的值)
编辑:抱歉,当我将鼠标悬停在案例 #1 上时,Android Studio 警告我 isPaused 始终为假。当我将鼠标悬停在案例 #2 上时,Android Studio 会警告我 isPaused 始终为真。仍然不确定这有什么帮助。