SessionManagerListener无法正常工作

时间:2016-09-20 08:19:17

标签: android chromecast

我正在使用某个应用而我正在尝试投放视频,但是当我想设置一个监听器来进行演员会话时却无法正常工作。我在一个片段中工作,所以我无法弄清楚问题出在哪里。

private void setupCastListener() {
        mSessionManagerListener = new SessionManagerListener<CastSession>() {

            @Override
            public void onSessionEnded(CastSession session, int error) {
                onApplicationDisconnected();
            }

            @Override
            public void onSessionResumed(CastSession session, boolean wasSuspended) {
                Log.d("fragment tv", "onSessionResumed");

                onApplicationConnected(session);
            }

            @Override
            public void onSessionResumeFailed(CastSession session, int error) {
                onApplicationDisconnected();
            }

            @Override
            public void onSessionStarted(CastSession session, String sessionId){                    Log.d("fragment tv", "onSessionStarted");
                onApplicationConnected(session);
            }

            @Override
            public void onSessionStartFailed(CastSession session, int error) {
                onApplicationDisconnected();
            }

            @Override
            public void onSessionStarting(CastSession session) {
            }

            @Override
            public void onSessionEnding(CastSession session) {
            }

            @Override
            public void onSessionResuming(CastSession session, String sessionId) {
            }

            @Override
            public void onSessionSuspended(CastSession session, int reason) {
            }

            private void onApplicationConnected(CastSession castSession) {
                Log.d("fragment tv", "connected");
                mCastSession = castSession;
                mLocation = PlaybackLocation.REMOTE;}

            private void onApplicationDisconnected() {
                Log.d("fragment tv", "disconnected");

                mLocation = PlaybackLocation.LOCAL;

            }
        };
    }

1 个答案:

答案 0 :(得分:0)

故障1 :-通常人们忘记了使用onResume方法注册监听器

onResume(){
   mCastContext.getSessionManager()
               .addSessionManagerListener(mSessionManagerListener, CastSession.class);
}

故障2 :-有些人在MediaRouteButton上添加home screen,然后在不同的活动中写上SessionManagerListener,却忘记添加{ MediaRouteButton上的{1}}。因此,显然scrren永远不会被调用。

因此,请小心添加SessionManagerListener,然后在相应的活动中添加MediaRouteButton

SessionManagerListener