2016-10-10 11 views
6

chcę wdrożyć zmianę głośności jak widać na Youtube aplikacji podczas odlewania, jak jeśli aplikacja znajduje się w tle lub na ekranie blokadyAndroid MediaRouter głośności wydarzenia

Like this

private void createSession() { 
    ComponentName receiver = new ComponentName(getPackageName(), RemoteReceiver.class.getName()); 
    mediaSession = new MediaSessionCompat(this, "PlayerService", receiver, null); 
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | 
      MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); 
    mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() 
      .setState(PlaybackStateCompat.STATE_PLAYING, 0, 1f) 
      .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE) 
      .build()); 
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() { 
     @Override 
     public void onAudioFocusChange(int focusChange) { 
      // Ignore 
     } 
    }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); 
    mediaSession.setActive(true); 

    mediaRouter = MediaRouter.getInstance(this); 
    mediaRouter.setMediaSessionCompat(mediaSession); 
} 

Teraz mam suwak tak jak Powyższy obraz odpowiada na przyciski głośności, ale nie otrzymuję zmian w odbiorniku.

Odpowiedz

4

Wszystko, co musisz zrobić, to stworzyć MediaSessionCompat z VolumeProviderCompat.

// Here is a volume provider which sets the volume of a remote route. 
// Extend VolumeProviderCompat with your own implementation. 
public static class RemoteVolume extends VolumeProviderCompat { 
    public RemoteVolume(MediaRouter.RouteInfo routeInfo) { 
     super(VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE, STEPS, 0); 
     this.stepSize = routeInfo.getVolumeMax()/STEPS; 
     this.routeInfo = routeInfo; 
    } 

    @Override 
    public void onSetVolumeTo(int volume) { 
     routeInfo.requestSetVolume(volume * stepSize); 
     setCurrentVolume(volume); 
    } 

    @Override 
    public void onAdjustVolume(int delta) { 
     int newVolume = getCurrentVolume() + delta; 
     routeInfo.requestSetVolume(newVolume * stepSize); 
     setCurrentVolume(newVolume); 
    } 
} 

Następnie podłącz VolumeProviderCompat aby Twój MediaSessionCompat:

MediaSessionCompat session = new MediaSessionCompat(context, TAG); 

// might need some of these flags 
session.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | 
      MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | 
      MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS); 

// the volume buttons are routed to this session when it is 
// active and currently playing 
session.setPlaybackState(new PlaybackStateCompat.Builder() 
     .setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f) 
     .build()); 

session.setActive(true); 

// The media router tries to bind its own VolumeProvider which kinda 
// works. We need to unbind the one provided and put ours in. 
router.setMediaSessionCompat(session); 
session.setPlaybackToRemote(new RemoteVolume(myRemoteRoute)); 
+0

Na używając klawiszy głośności onAdjustVolume jest wywoływana z delty 0 jakiś pomysł dlaczego? podczas zmiany głośności za pomocą okna dialogowego Cast działa to dobrze – ingsaurabh

+0

Może zostać wywołane w celu zaktualizowania bieżącego woluminu. Czy dzieje się to więcej niż raz? – 0xcaff

+0

Również "KROKI" powinny być niezerowe. – 0xcaff

Powiązane problemy