【问题标题】:How to update volume bar in MinimalMediaRouteProvider如何更新 MinimalMediaRouteProvider 中的音量条
【发布时间】:2013-08-16 19:48:42
【问题描述】:

我正在使用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);
    }
}

编辑 - 在我初始化 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);
        }
    }

【问题讨论】:

    标签: chromecast google-cast


    【解决方案1】:

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

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

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

    现在开始您的 Java 代码: 我看不出您是如何在您发布的内容中获取 messageStream 的,但因为它可能是正确的。这是lines from the reference docs

    public final MediaProtocolCommand setVolume(双卷)

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

    【讨论】:

    • 我在ApplicationSession.Listener onSessionStarted 中创建了一个MediaProtocolMessageStream 对象。我将添加上面的代码。我已经在onSetVolume 里面有MediaProcolCommand setVolume。我正在寻找的是如何在弹出对话框中更新音量搜索栏,它会向您显示音量栏和断开连接按钮
    【解决方案2】:

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

    首先,您需要MediaRouteStateChangeListener 来处理此问题。

    MediaRouteStateChangeListener mRouteStateListener;
    

    其次,在onDeviceAvailable中分配监听器。

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

    最后,在onSetVolumeonUpdateVolume 中致电MediaRouteStateListener.onVolumeChanged()

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

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

    【讨论】:

    • 您的解决方案适用于 android-cast-sample 项目。但是当我将 android.support.v7.app.MediaRouteActionProvider" 更改为带有 MediaRouteButton 的操作项以与 ActionBarSherlock 一起使用时。它不起作用。你使用 actionProviderClass 还是 MediaRouteButton
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2011-03-30
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      相关资源
      最近更新 更多