I have an android app that is made for both Android phone/tablets as well as Amazon Fire TV. There is a play/pause button in the center of the screen that toggles between each other when pressed. The video streaming plays and pauses fine when using this button. Here is the first part of the code of my ExoPlayerActivity class:
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();
}
});
}
Note that in the setListener() method, when the pause button is clicked, player.setPlayWhenReady(false) is called which pauses the stream, and toggles the button to be a Play symbol. In contrast, player.setPlayWhenReady(true) is called when the Play button is pressed, again toggling the button to be a Pause symbol.
Now for my onKeyUp method that I use to register Amazon Fire TV inputs. In my OnCreate, I initially set isPaused = false. Here is my onKeyUp method:
@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);
}
Let's say the first if statement is case#1 and the else if is case #2. When I first press the Play/Pause button on the remote, it pauses the stream. This part makes little sense to me as I've set isPaused to false in the onCreate. Shouldn't case #1 be the one that is executed? In that case, I'm calling setPlayWhenReady(true) which should just keep the streaming playing. But somehow, it pauses the stream. Then once the stream is paused, I press the play/pause button again on the remote, but it doesn't resume the stream.
I've been looking through my logic and can't seem to figure out what's going on. I tried using onKeyDown instead of onKeyUp but there was no difference in the results. Android studio also warns me that isPaused = true is always true. This just adds onto my confusion because in the onCreate, isPaused is false. It can only become true, when I press the button (I don't change the value of isPaused anywhere else in my code)
Edit: Sorry, when I mouse over Case #1, Android studio warns me that isPaused is always false. When I mouse over Case #2, Android studio warns me that isPaused is always true. Still not sure how that helps.
small typo in above code... should be
if(isPaused==false){
...
(not just a single =
)