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

Android Media3: The Next Generation of Media Playback on Android

Android Media3: The Next Generation of Media Playback on Android

In this talk, you will learn how to use Android Media3 to build powerful and efficient media apps. This topic will cover the basics of the framework, including how to play back different types of media content.

Whether you are a beginner or an experienced media developer, this talk will give you the knowledge and skills you need to build high-quality media apps with Android Media3.

Transcript

  1. Android Media Usages 1. Local Media playbacks (audios, videos) 2.

    Streaming (audios, videos) 3. Transformers (Video processing like Transcoding, HRD Video, Video Editing)
  2. MediaPlayer vs ExoPlayer • Simple and lightweight • Low battery

    consumption • Limited format support • Limited customization • Platform dependent MediaPlayer • Wide format support • Better performance for streaming • More controls and customizations • Higher battery consumption • Platform independent (mostly) ExoPlayer
  3. Media2 + Exoplayer = Media3 media2-player media2-widget media2-session media2-common Media2

    exoplayer-core exoplayer-ui exoplayer- mediasession exoplayer- common ExoPlayer exoplayer- common exoplayer- dash media3- exoplayer media3-ui media3-session media3-common Media3 exoplayer- common media3- exoplayer- dash
  4. Attach the player to a view // Bind the player

    to the view. playerView.player = player
  5. Prepare & Play // Build the media item. val mediaItem

    = MediaItem.fromUri(videoUri) // Set the media item to be played. player.setMediaItem(mediaItem) // Prepare the player. player.prepare() // Start the playback. player.play()
  6. Playlists // Build the media items. val firstItem = MediaItem.fromUri(firstVideoUri)

    val secondItem = MediaItem.fromUri(secondVideoUri) // Add the media items to be played. player.addMediaItem(firstItem) player.addMediaItem(secondItem) // Prepare the player. player.prepare() // Start the playback. player.play()
  7. What is Media Session? 1. a universal way of interacting

    with a video/audio player 2. playback control can be delegated to external sources (buttons from remote controls, voice commands from Google Assistant)
  8. MediaSessionService class PlaybackService : MediaSessionService() { private var mediaSession: MediaSession?

    = null // Create your Player and MediaSession in the onCreate lifecycle event override fun onCreate() { super.onCreate() val player = ExoPlayer.Builder(this).build() mediaSession = MediaSession.Builder(this, player).build() } }
  9. MediaSessionService class PlaybackService : MediaSessionService() { private var mediaSession: MediaSession?

    = null //. // Remember to release the player and media session in onDestroy override fun onDestroy() { mediaSession/.run { player.release() release() mediaSession = null } super.onDestroy() } }
  10. MediaSessionService // This example always accepts the connection request override

    fun onGetSession( controllerInfo: MediaSession.ControllerInfo ): MediaSession? = mediaSession
  11. Connecting with UI override fun onStart() { val sessionToken =

    SessionToken(this, ComponentName(this, PlaybackService/:class.java)) val controllerFuture = MediaController.Builder(this, sessionToken).buildAsync() controllerFuture.addListener( { // Call controllerFuture.get() to retrieve the MediaController. // MediaController implements the Player interface, so it can be // attached to the PlayerView UI component. playerView.setPlayer(controllerFuture.get()) }, MoreExecutors.directExecutor() ) }