不使用remoteMediaPlayer时如何使用Chromecast图标显示音量滑块

时间:2014-12-06 21:55:37

标签: android chromecast google-cast

我正在尝试在投射时实现音量滑块,并在滑块中显示投射图标,就像在Youtube应用中一样:http://imgur.com/kVxoKbM

到目前为止,我已经覆盖了dispatchKeyEvent函数我的活动,如演员开发者文档中所述(如下面的代码所示),这使我可以完美地控制连接的Chromecast的音量。但在设备上,它根本不显示滑块。

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
  int action = event.getAction();
  int keyCode = event.getKeyCode();
  switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
      if (action == KeyEvent.ACTION_DOWN) {
        if (mApiClient != null) {
          double currentVolume = Cast.CastApi.getVolume(mApiClient);
          if (currentVolume < 1.0) {
            try {
              Cast.CastApi.setVolume(mApiClient,
            Math.min(currentVolume + VOLUME_INCREMENT, 1.0));
            } catch (Exception e) {
              Log.e(TAG, "unable to set volume", e);
            }
          }
        } else {
          Log.e(TAG, "dispatchKeyEvent - volume up");
        }
      }
      return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
      if (action == KeyEvent.ACTION_DOWN) {
        if (mApiClient != null) {
          double currentVolume = Cast.CastApi.getVolume(mApiClient);
          if (currentVolume > 0.0) {
            try {
              Cast.CastApi.setVolume(mApiClient,
            Math.max(currentVolume - VOLUME_INCREMENT, 0.0));
            } catch (Exception e) {
              Log.e(TAG, "unable to set volume", e);
            }
          }
        } else {
          Log.e(TAG, "dispatchKeyEvent - volume down");
        }
      }
      return true;
    default:
      return super.dispatchKeyEvent(event);
   }
  }

我没有使用remoteMediaPlayer。那么如何在控制连接的Chromecast音量的同时实现音量滑块呢?

0 个答案:

没有答案