2013-08-14 11 views
5

Sto usando registerMediaRouteProvider e ti dà una barra del volume per aggiornare il volume della tv. Ho implementato MediaRouteAdapter e quando scrollo la barra del volume, il volume cambia, ma l'ui della barra del volume si reimposta sempre su 0. Come aggiorno l'interfaccia della barra del volume quando il volume cambia?Come aggiornare la barra del volume in 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); 
    } 
} 

EDIT - ha aggiunto dove inizializzo 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

risposta

1

A livello di ricevitore, ci sono due modi per impostare il volume:

  1. cast.Receiver .Platform.setVolume ([0.0,1.0]) Questo imposterà il vo lumi e mostra la barra blu. Questo è ciò che viene solitamente chiamato dal messaggio onVolume.

  2. mediaElement.volume = [0.0, 1.0]; Questo imposterà il volume senza mostrare la barra blu. Puoi usarlo per dissolvenza in apertura/dissolvenza e per equalizzazione.

D'ora in poi il codice Java: non vedo come hai trovato il tuo messageStream in quello che hai postato, ma dal momento che è probabile che sia la cosa giusta. Ecco il lines from the reference docs:

pubblico finale MediaProtocolCommand setVolume (doppio volume)

Imposta il volume dell'audio. Parametri volume Il volume, nell'intervallo da 0.0 (0%) a 1.0 (100%). Restituisce L'oggetto comando per questa richiesta Lancio IOException Se si verifica un errore I/O durante l'invio del messaggio. IllegalStateException Se questo flusso non è collegato a un canale collegato.

+0

Sto creando un oggetto 'MediaProtocolMessageStream' all'interno del' ApplicationSession.Listener onSessionStarted'. Aggiungerò il codice sopra.Ho già 'MediaProcolCommand'' setVolume' dentro 'onSetVolume'. Quello che sto cercando è come aggiornare la barra di ricerca del volume nella finestra di dialogo popup in cui mostra la barra del volume e il pulsante di disconnessione – heero

4

Ho cercato per questo per giorni e improvvisamente ho trovato la soluzione da me: P

In primo luogo, è necessario un MediaRouteStateChangeListener per gestire questa situazione.

MediaRouteStateChangeListener mRouteStateListener; 

secondo luogo, assegnare l'ascoltatore in onDeviceAvailable.

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

scorso, chiamare MediaRouteStateListener.onVolumeChanged() in onSetVolume o 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); 
    } 
} 

Questo dovrebbe aiutare la barra di ricerca del volume ad agire normalmente. :)

+0

La soluzione ha funzionato sul progetto di esempio cast android. Ma quando cambio android.support.v7.app.MediaRouteActionProvider "in action item con MediaRouteButton da utilizzare con ActionBarSherlock, non funziona. Hai usato actionProviderClass o MediaRouteButton? –

Problemi correlati