5

I am using registerMediaRouteProvider and it gives you a volume bar to update the tv's volume. I implemented MediaRouteAdapter and when I scrub the volume bar, the volume changes, but the volume bar's ui always resets back to 0. How do I update the ui of the volume bar when the volume changes?

@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);
    }
}

EDIT - added where I initialize 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);
        }
    }

This is the volume bar that is not updating

4

3 回答 3

4

我一直在寻找这个几天,我突然自己找到了解决方案:P

首先,你需要一个MediaRouteStateChangeListener来处理这个。

MediaRouteStateChangeListener mRouteStateListener;

其次,将监听器分配到onDeviceAvailable.

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

MediaRouteStateListener.onVolumeChanged()最后,呼入onSetVolumeonUpdateVolume

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

这应该有助于您的音量搜索栏正常运行。:)

于 2013-09-18T08:17:38.830 回答
1

在 Receiver 级别,有两种设置音量的方法:

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

  2. mediaElement.volume = [0.0, 1.0]; 这将设置音量而不显示蓝条。您可以将其用于淡入/淡出和均衡。

现在转到您的 Java 代码:我看不出您是如何在您发布的内容中获取 messageStream 的,但因为它可能是正确的。以下是参考文档中的内容

public final MediaProtocolCommand setVolume(双倍音量)

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

于 2013-08-15T21:46:14.393 回答
0

谷歌今年夏天更新了音量控制,以便在连接的设备和应用程序之间共享。请在此处查看我的解决方案:

https://stackoverflow.com/a/26554007/672373

于 2014-10-24T18:29:09.870 回答