5

私は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);
        }
    }

これは更新されていない音量バーです

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()またはonSetVolumeを呼び出しますonUpdateVolume

@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

レシーバー レベルでは、音量を設定する方法が 2 つあります。

  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%) の範囲のボリューム。戻り値 このリクエストのコマンド オブジェクト スローされる IOException メッセージの送信中に I/O エラーが発生した場合。IllegalStateException このストリームが接続されたチャネルに接続されていない場合。

于 2013-08-15T21:46:14.393 に答える
0

Google は今年の夏にボリューム コントロールを更新し、接続されたデバイスとアプリ間でボリューム コントロールを共有できるようにしました。ここで私の解決策を見てください:

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

于 2014-10-24T18:29:09.870 に答える