如何在MinimalMediaRouteProvider中更新音量条

hee*_*ero 5 google-cast chromecast

我正在使用registerMediaRouteProvider,它为您提供了一个音量条来更新电视的音量.我实现了MediaRouteAdapter,当我擦洗音量条时,音量发生变化,但音量条的ui总是重置为0.当音量改变时,如何更新音量条的ui?

@Override
public void onCreate(Bundle savedInstanceState) {

  mCastContext = new CastContext(getApplicationContext());
  MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this);
  mMediaRouter = MediaRouter.getInstance(getApplicationContext());
  mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST);
  mMetaData = new ContentMetadata();

  mMediaRouterCallback = new MyMediaRouterCallback();
  mMediaRouteButton.setRouteSelector(mMediaRouteSelector);
}

private class MyMediaRouterCallback extends MediaRouter.Callback {
    @Override
    public void onRouteSelected(MediaRouter router, RouteInfo route) {
        MediaRouteHelper.requestCastDeviceForRoute(route);
    }

    @Override
    public void onRouteUnselected(MediaRouter router, RouteInfo route) {
        try {
            if (mSession != null) {
                Log.e(TAG, "Ending session and stopping application");
                mSession.setStopApplicationWhenEnding(true);
                mSession.endSession();
            } else {
                Log.e(TAG, "onRouteUnselected: mSession is null");
            }
        } catch (IllegalStateException e) {
            Log.e(TAG, "onRouteUnselected:");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "onRouteUnselected:");
            e.printStackTrace();
        }
        mSelectedDevice = null;
    }
}

 @Override
public void onDeviceAvailable(CastDevice device, String arg1, MediaRouteStateChangeListener listener) {
    mSelectedDevice = device;
    openSession();
}

@Override
public void onSetVolume(double volume) {
    try {
        if (mMessageStream != null) {
            mMessageStream.setVolume(volume);
        }
    } catch (IllegalStateException e) {
        Log.e(TAG, "Problem sending Set Volume", e);
    } catch (IOException e) {
        Log.e(TAG, "Problem sending Set Volume", e);
    }
}

@Override
public void onUpdateVolume(double volumeChange) {
    try {
        if (mCurrentRoute != null) {
            mCurrentRoute.requestUpdateVolume((int) (volumeChange * 20));
        }
    } catch (IllegalStateException e) {
        Log.e(TAG, "Problem sending Update Volume", e);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 - 添加初始化mMessageStream的位置

private void openSession() {
        mSession = new ApplicationSession(mCastContext, mSelectedDevice);
        int flags = 0;
        flags |= ApplicationSession.FLAG_DISABLE_NOTIFICATION;
        flags |= ApplicationSession.FLAG_DISABLE_LOCK_SCREEN_REMOTE_CONTROL;
        mSession.setApplicationOptions(flags);

        Log.d(TAG, "Beginning session with context: " + mCastContext);
        Log.d(TAG, "The session to begin: " + mSession);
        mSession.setListener(new com.google.cast.ApplicationSession.Listener() {

            @Override
            public void onSessionStarted(ApplicationMetadata appMetadata) {
                Log.d(TAG, "Getting channel after session start");
                ApplicationChannel channel = mSession.getChannel();
                if (channel == null) {
                    Log.e(TAG, "channel = null");
                    return;
                }
                Log.d(TAG, "Creating and attaching Message Stream");
                mMessageStream = new MediaProtocolMessageStream();
                channel.attachMessageStream(mMessageStream);

                if (mMessageStream.getPlayerState() == null) {
                    if (vastVideoView.getPlayingPlaylistItem() != null) {
                        loadMedia();
                    }
                } else {
                    Log.e(TAG, "Found player already running; updating status");
                }
            }

            @Override
            public void onSessionStartFailed(SessionError error) {
                Log.e(TAG, "onStartFailed " + error + " code " + error.getCode());
                nowifi.setVisibility(View.GONE);
            }

            @Override
            public void onSessionEnded(SessionError error) {
                Log.i(TAG, "onEnded " + error);
                controller.removeChromeCastListener();
                controller.setChromeCast(false);
                nowifi.setVisibility(View.GONE);
            }
        });

        try {
            Log.e(TAG, "Starting session with app name " + getString(R.string.app_id));
            mSession.startSession(getString(R.string.app_id));

            vastVideoView.stopPlayback();

            controller = vastVideoView.getMediaController();
            controller.setChromeCast(true);
            controller.setPausePlayListener(pausePlayListener);
            seekBar = controller.getSeekBar();
            seekBar.setProgress(0);

            mPlayButtonShowsPlay = true;
        } catch (IOException e) {
            Log.e(TAG, "Failed to open session", e);
            controller.removeChromeCastListener();
            controller.setChromeCast(false);
            nowifi.setVisibility(View.GONE);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是未更新的音量条

Les*_*Rel 1

在接收器级别,有两种设置音量的方法:

  1. cast.Receiver.Platform.setVolume([0.0,1.0]) 这将设置音量并显示蓝色条。这就是通常所说的 onVolume 消息。

  2. mediaElement.volume = [0.0, 1.0]; 这将设置音量而不显示蓝色条。您可以使用它进行淡入/淡出以及均衡。

现在来看看你的Java代码:我不明白你是如何在你发布的内容中获取messageStream的,但因为它可能是正确的。这是参考文档中的行

public Final MediaProtocolCommand setVolume (双倍音量)

设置音量。参数 体积 体积,范围为 0.0 (0%) 到 1.0 (100%)。返回 此请求的命令对象 如果发送消息时发生 I/O 错误,则抛出 IOException。IllegalStateException 如果此流未附加到已连接的通道。