Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Media Hacks

Android Media Hacks

Look into new media playback control API and how it works in Android Auto and Notification. I'm also talking about some hacks to receive playback state and media content information in a non-music application.

Keishin Yokomaku

June 01, 2015
Tweet

More Decks by Keishin Yokomaku

Other Decks in Technology

Transcript

  1. Android Media Hacks
    @KeithYokoma - Drivemode, Inc.
    potatotips #18

    View full-size slide

  2. @KeithYokoma
    Keishin Yokomaku at Drivemode, Inc. as Android Engineer
    Experience
    1.SNS client and photo book application for Android
    2.Driver’s application for Android
    Publications
    1.Android Training
    2.Mixi official smartphone application development guide
    Like
    Motorsport, Bicycle, Photography, Tumblr

    View full-size slide

  3. Media Playback Control
    Put media playback controller on…
    • Notification
    • AppWidgets
    • Android Auto App
    • Lock Screen(KeyGuard)

    View full-size slide

  4. Media Playback Control
    Different implementations for each API level
    • RemoteController (from KitKat)
    • RemoteControlClient(from ICS)
    These system are based on PendingIntent
    From Lollipop…
    • MediaSession and MediaController

    View full-size slide

  5. MediaController
    Standardized media playback control API from Lollipop
    System will use this API in…
    • Notification.MediaStyle
    • Android Auto App

    View full-size slide

  6. How it works on Notification
    Media Application
    MediaSession
    System Notification
    MediaController
    Token
    Transport Control

    View full-size slide

  7. How it works on Android Auto
    Media Application
    MediaSession
    Android Auto
    MediaController
    Token & Media Contents
    Bind
    MediaBrowserService MediaBrowser
    Token Token
    Transport Control

    View full-size slide

  8. Watching Notification…

    View full-size slide

  9. NotificationListenerService

    View full-size slide

  10. NotificationListenerService
    Event receiver for status bar notification available from JB-MR2
    We can access following data in the notification…
    • Title
    • Message
    • ContentView
    • HeadsUpContentView
    • Extras

    View full-size slide

  11. NotificationListenerService
    Event receiver for status bar notification available from JB-MR2
    We can access following data in the notification…
    • Title
    • Message
    • ContentView
    • HeadsUpContentView
    • Extras

    View full-size slide

  12. Notification.MediaStyle

    View full-size slide

  13. Notification.MediaStyle

    View full-size slide

  14. MediaController

    View full-size slide

  15. MediaController

    View full-size slide

  16. –Me
    “Yes, you can steal a media session token
    without invading private APIs”

    View full-size slide

  17. –Me
    “And you can set the token to your own MediaController”

    View full-size slide

  18. –Someone
    “This is more like session hijacking”

    View full-size slide

  19. Take control of music playback
    public class MusicNotificationWatcher extends NotificationListenerService {
    private Bus eventBus;
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
    MediaSession.Token token =
    sbn.getNotification().extras.getParcelable(Notification.EXTRA_MEDIA_SESSION);
    eventBus.post(new NewMusicNotification(token));
    }
    }
    public class MyActivity extends Activity {
    private MediaController controller;
    @Subscribe
    public void onNewMusicNotificationAdded(NewMusicNotification event) {
    controller = new MediaController(this, event.getToken())
    }
    }

    View full-size slide

  20. Take control of music playback
    public class MusicNotificationWatcher extends NotificationListenerService {
    private Bus eventBus;
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
    MediaSession.Token token =
    sbn.getNotification().extras.getParcelable(Notification.EXTRA_MEDIA_SESSION);
    eventBus.post(new NewMusicNotification(token));
    }
    }
    public class MyActivity extends Activity {
    private MediaController controller;
    @Subscribe
    public void onNewMusicNotificationAdded(NewMusicNotification event) {
    controller = new MediaController(this, event.getToken())
    }
    }

    View full-size slide

  21. Take control of music playback
    public class MusicNotificationWatcher extends NotificationListenerService {
    private Bus eventBus;
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
    MediaSession.Token token =
    sbn.getNotification().extras.getParcelable(Notification.EXTRA_MEDIA_SESSION);
    eventBus.post(new NewMusicNotification(token));
    }
    }
    public class MyActivity extends Activity {
    private MediaController controller;
    @Subscribe
    public void onNewMusicNotificationAdded(NewMusicNotification event) {
    controller = new MediaController(this, event.getToken())
    }
    }

    View full-size slide

  22. It only works for Google Play Music

    View full-size slide

  23. Conclusion
    • Do not put PRIVATE things on your notification
    • Crackers are watching you
    • Consider using MediaSession and MediaController
    • For better interaction with Auto, Notification and

    your partners’ applications

    View full-size slide

  24. Conclusion
    • For a OS compatibility reason, you can use
    RemoteControlClient and AudioManager
    • Still you need some dirty hacks…
    • For an application compatibility reason, you should use
    RemoteController
    • Even though it is deprecated now, it works on Lollipop

    View full-size slide

  25. Android Media Hacks
    @KeithYokoma - Drivemode, Inc.
    potatotips #18

    View full-size slide