2013-08-14 32 views
5

나는 registerMediaRouteProvider을 사용하고 있으며 TV의 볼륨을 업데이트하는 볼륨 바를 제공합니다. MediaRouteAdapter을 구현했으며 볼륨 바를 문질러 떼면 볼륨이 변경되지만 볼륨 바의 UI는 항상 0으로 다시 설정됩니다. 볼륨이 바뀌면 볼륨 바의 UI를 어떻게 업데이트합니까?MinimalMediaRouteProvider에서 볼륨 막대를 업데이트하는 방법

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

This is the volume bar that is not updating

답변

1

를 초기화, 볼륨 설정하는 두 가지 방법이 있습니다 :

  1. cast.Receiver가 .Platform.setVolume ([0.0,1.0]) 이것은 vo를 설정합니다. 푸른 색 막대를 표시하십시오. 이것은 보통 onVolume 메시지에 의해 호출됩니다.

  2. mediaElement.volume = [0.0, 1.0]; 그러면 파란색 막대가 표시되지 않고 볼륨이 설정됩니다. 페이드 인/페이드 아웃 및 이퀄라이제이션에 사용할 수 있습니다. 자바 코드에 지금에

: 난 당신이 게시 한 내용에 messageStream를 얻는 방법을 볼 수 있지만, 그것은 옳은 일이 될 가능성이 높습니다 이후하지 않습니다.

공공 최종 MediaProtocolCommand setVolume (더블 볼륨)

오디오 볼륨을 설정합니다 : 여기 lines from the reference docs입니다. 매개 변수 볼륨 볼륨이 0.0 (0 %)에서 1.0 (100 %) 범위입니다. Returns 이 요청에 대한 명령 개체 오류 메시지를 보내는 동안 I/O 오류가 발생합니다. IllegalStateException이 스트림이 연결된 채널에 연결되어 있지 않은 경우.

+0

. 위의 코드를 추가하겠습니다.onSetVolume 안에 이미'MediaProcolCommand'' setVolume'을 가지고 있습니다. 내가 찾고있는 것은 당신에게 볼륨 바를 보여주는 대화 상자 팝업에서 볼륨 탐색 바를 업데이트하는 방법입니다. – heero

4

나는 일이 찾고 있던 나는 갑자기 나 자신으로 해결책을 발견 :

먼저 P,이 작업을 처리하는 MediaRouteStateChangeListener가 필요합니다.

MediaRouteStateChangeListener mRouteStateListener; 

두 번째로 수신기를 onDeviceAvailable에 할당하십시오.

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

마지막으로, onSetVolume 또는 onUpdateVolumeMediaRouteStateListener.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); 
    } 
} 

이렇게하면 볼륨 검색 기능이 정상적으로 작동하는 데 도움이됩니다. :)

+0

당신의 솔루션은 android-cast-sample 프로젝트에서 작동했습니다. 하지만, android.support.v7.app.MediaRouteActionProvider "를 ActionBarSherlock과 함께 사용할 MediaRouteButton이있는 작업 항목으로 변경하면 작동하지 않습니다. actionProviderClass 또는 MediaRouteButton을 사용 했습니까? –

0

Google은 이번 여름에 볼륨 컨트롤을 업데이트하여 연결된 기기와 앱간에 공유합니다. 여기에 내 솔루션을 참조하십시오 : 나는`ApplicationSession.Listener onSessionStarted` 내부의`MediaProtocolMessageStream` 개체를 만드는거야

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

관련 문제