Search code examples
androidmedia-playerlockscreenandroid-notification-bar

How to build music controller in NotificationBar/LockScreen that supporting from api version 10?


Am building an music player app. I want to implement media controller in notification bar. I have tried one method http://www.binpress.com/tutorial/using-android-media-style-notifications-with-media-session-controls/165. But it only supports lollipop version of android. Does anyone have idea to build media controller in notification bar ?


Solution

  • You have to use NotificationCompat with MediaStyle: https://developer.android.com/reference/android/support/v7/app/NotificationCompat.MediaStyle.html

    Notification noti = new NotificationCompat.Builder()
     .setSmallIcon(R.drawable.ic_stat_player)
     .setContentTitle("Track title")
     .setContentText("Artist - Album")
     .setLargeIcon(albumArtBitmap))
     .setStyle(new NotificationCompat.MediaStyle()
         .setMediaSession(mySession))
     .build();
    

    A full example of implementing mediaSessionCompat with NotificationCompat can be found here: https://bitbucket.org/nonameden/android-universalmusicplayer-compat/src/412465d6605fee26f197cc5615f7da15cc859d4a/mobile/src/main/java/com/example/android/uamp/MediaNotificationManager.java?at=master