亚马逊Fire TV遥控钥匙的播放/暂停逻辑有什么问题?

时间:2017-02-08 21:32:19

标签: android amazon-fire-tv

我有一个Android应用程序,适用于Android手机/平板电脑以及亚马逊Fire TV。屏幕中央有一个播放/暂停按钮,按下时可以相互切换。使用此按钮时,视频流播放和暂停正常。这是我的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)来暂停流,并将按钮切换为Play符号。相反,按下“播放”按钮时会调用pl​​ayer.setPlayWhenReady(true),再次将按钮切换为“暂停”符号。

现在我用于注册亚马逊Fire TV输入的onKeyUp方法。在我的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语句是#1,而else if是#2。当我第一次按遥控器上的播放/暂停按钮时,它会暂停流。这部分对我来说没什么意义,因为我在onCreate中将isPaused设置为false。情况#1不应该是执行的吗?在这种情况下,我调用setPlayWhenReady(true),这应该只是保持流播放。但不知何故,它暂停了流。然后,一旦暂停流,我再次按遥控器上的播放/暂停按钮,但它不会恢复流。

我一直在查看我的逻辑,似乎无法弄清楚发生了什么。我尝试使用onKeyDown而不是onKeyUp,但结果没有区别。 Android工作室也警告我isPaused = true总是如此。这只会增加我的困惑,因为在onCreate中,isPaused是错误的。只有按下按钮才能成为现实(我不会在代码中的任何其他位置更改isPaused的值)

编辑:对不起,当我将鼠标悬停在案例#1上时,Android工作室会警告我,isPaused始终为false。当我将鼠标移到Case#2上时,Android工作室警告我isPaused总是正确的。仍然不确定这有多大帮助。

1 个答案:

答案 0 :(得分:0)

上面代码中的小错误......应该是

if(isPaused==false){
...

(不只是一个=