Search in sources :

Example 16 with MediaSession

use of android.media.session.MediaSession in project TicktockMusic by Lauzy.

the class TickNotification method buildNotification.

private Notification buildNotification(MusicService musicService) {
    PendingIntent playIntent = createAction(musicService, MusicService.ACTION_PLAY);
    PendingIntent pauseIntent = createAction(musicService, MusicService.ACTION_PAUSE);
    PendingIntent previousIntent = createAction(musicService, MusicService.ACTION_LAST);
    PendingIntent nextIntent = createAction(musicService, MusicService.ACTION_NEXT);
    PendingIntent contentIntent = PendingIntent.getActivity(musicService, 0, new Intent(musicService, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
    boolean isPlaying = musicService.getPlaybackState().getState() == PlaybackState.STATE_PLAYING;
    MediaSession session = musicService.getMediaSession();
    MediaController controller = session.getController();
    if (controller.getMetadata() != null) {
        MediaDescription description = controller.getMetadata().getDescription();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(musicService);
        builder.setContentTitle(description.getTitle()).setSmallIcon(R.drawable.ic_notification).setShowWhen(false).setOngoing(isPlaying).setContentIntent(contentIntent).setLargeIcon(description.getIconBitmap()).setContentTitle(description.getTitle()).setContentText(description.getSubtitle()).addAction(R.drawable.ic_skip_previous_notify, "", previousIntent).addAction(isPlaying ? R.drawable.ic_pause : R.drawable.ic_play, "", isPlaying ? pauseIntent : playIntent).addAction(R.drawable.ic_skip_next_notify, "", nextIntent).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(0, 1, 2));
        if (description.getIconBitmap() != null) {
            Palette palette = Palette.from(description.getIconBitmap()).generate();
            int color = palette.getDominantColor(ThemeHelper.getThemeColorResId(musicService.getApplicationContext()));
            builder.setColor(color);
        }
        return builder.build();
    }
    return null;
}
Also used : Palette(android.support.v7.graphics.Palette) MediaController(android.media.session.MediaController) MediaDescription(android.media.MediaDescription) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MainActivity(com.freedom.lauzy.ticktockmusic.ui.activity.MainActivity) MediaSession(android.media.session.MediaSession) NotificationCompat(android.support.v7.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Aggregations

MediaSession (android.media.session.MediaSession)16 MediaController (android.media.session.MediaController)4 MediaSessionManager (android.media.session.MediaSessionManager)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 MediaDescription (android.media.MediaDescription)1 MediaMetadata (android.media.MediaMetadata)1 MediaPlayer (android.media.MediaPlayer)1 Rating (android.media.Rating)1 PlaybackState (android.media.session.PlaybackState)1 Handler (android.os.Handler)1 NotificationCompat (android.support.v7.app.NotificationCompat)1 Palette (android.support.v7.graphics.Palette)1 MainActivity (com.freedom.lauzy.ticktockmusic.ui.activity.MainActivity)1 PlayerFragment (com.sdsmdg.harjot.MusicDNA.fragments.PlayerFragment.PlayerFragment)1