public PlaybackServiceStatusHelper(PlaybackService playbackService) { mPlaybackService = playbackService; // Get MediaSession objects mMediaSession = new MediaSessionCompat(mPlaybackService, "OdysseyPBS"); // Register the callback for the MediaSession mMediaSession.setCallback(new OdysseyMediaSessionCallback()); mCoverLoader = new CoverBitmapLoader(mPlaybackService, new BitmapCoverReceiver()); // Register the button receiver PendingIntent mediaButtonPendingIntent = PendingIntent.getBroadcast(mPlaybackService, 0, new Intent(mPlaybackService, RemoteControlReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT); mMediaSession.setMediaButtonReceiver(mediaButtonPendingIntent); mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS + MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); // Initialize the notification manager mNotificationManager = new OdysseyNotificationManager(mPlaybackService); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(playbackService); mHideArtwork = sharedPref.getBoolean(playbackService.getString(R.string.pref_hide_artwork_key), playbackService.getResources().getBoolean(R.bool.pref_hide_artwork_default)); hideMediaOnLockscreen(sharedPref.getBoolean(playbackService.getString(R.string.pref_hide_media_on_lockscreen_key), playbackService.getResources().getBoolean(R.bool.pref_hide_media_on_lockscreen_default))); Intent settingChangedIntent = new Intent(MESSAGE_HIDE_ARTWORK_CHANGED); settingChangedIntent.putExtra(MESSAGE_EXTRA_HIDE_ARTWORK_CHANGED_VALUE, mHideArtwork); mPlaybackService.sendBroadcast(settingChangedIntent); }
private void setUpAudioPlayerRemote() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName()); Intent audioIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); audioIntent.setComponent(audioReceiver); PendingIntent audioPendingIntent = PendingIntent.getBroadcast(this, 0, audioIntent, 0); mediaSession = new MediaSessionCompat(this, "PlayerService", audioReceiver, audioPendingIntent); mediaSession.setCallback(new MediaSessionCallback()); mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mediaSession.setMediaButtonReceiver(audioPendingIntent); setSessionToken(mediaSession.getSessionToken()); try { mMediaNotificationManager = new MediaNotificationManager(this); } catch (RemoteException e) { throw new IllegalStateException("Could not create a MediaNotificationManager", e); } mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() .setState(PlaybackStateCompat.STATE_NONE, 0, 0) .setActions(getAvailableActions()) .build()); if (audioManager != null) { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }