How to use the new AndroidX Media2?

2.9k views Asked by At

I am using Media App Architecture as a guide for building a music player app. But it uses the classes from support media-compat / Androidx Media.

But now AndroidX Media2 is available in stable channels and I don't see any word of it. What is it?

  • is AndroidX Media2 supposed to deprecate AndroidX Media?
  • is there a developer guide or other sources of documentation for AndroidX Media2?

Please, no links to JavaDoc, thanks.

3

There are 3 answers

0
Hack5 On

All the documentation I was able to find was useless and outdated. androidx.media and the appcompat media libraries are both superseded by androidx.media2 (but not deprecated for some reason). The most high-level API for media apps appears to be MediaLibraryService for the service and MediaBrowser for the frontend. Just make absolutely sure you declare the right intent filters on the service (which are in the javadoc :P)

2
Paul Lammertsma On

Jetpack Media3 has launched!
https://developer.android.com/jetpack/androidx/releases/media3

This blog post gives a great explanation about how the media libraries evolved. I strongly recommend integrating with androidx.media3.

If for whatever reason you cannot use androidx.media3, my recommendation is to stick with androidx.media instead of androidx.media2 due to the latter not being supported by other media integrations, such as Cast Connect. Integrating Media2 with ExoPlayer is also quite a bit more complex.

From my perspective, the key benefit of switching from Media1 to Media2 is the ability to provide more fine-grained permission controls. See Jaewan Kim's blog post that goes in depth about the more complex SessionPlayerConnector API and permissions to accept or reject connections from a controller in media2.

If you have an existing Media1 implementation using MediaSession (preferably using ExoPlayer with MediaSessionConnector), and have no need for the permission controls in Media2, you can either stick with Media1 or upgrade to Media3.

The “What's next for AndroidX Media and ExoPlayer” talk from Android Dev Summit 2021 goes much more in depth on Media3.

0
dkero On

Here is my working solution using Media2 libs (AndroidX):

  1. Add 3 dependencies in Build.gradle
implementation "androidx.media2:media2-session:1.2.0"
implementation "androidx.media2:media2-widget:1.2.0"
implementation "androidx.media2:media2-player:1.2.0"
  1. Create layout activity_video_player.xml and put this code into it:

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/accompany_agent_details"
         style="@style/Layout.Default">
         <LinearLayout
             android:id="@+id/videoLayout"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_alignParentStart="true"
             android:layout_alignParentLeft="true"
             android:layout_alignParentTop="true"
             android:orientation="vertical">
    
             <android.widget.VideoView android:id="@+id/simpleVideoView"
                 android:layout_width="match_parent"
                 android:layout_height="300dp" />
         </LinearLayout>
     </RelativeLayout>
    
     <style name="Layout.Default">
         <item name="android:layout_width">match_parent</item>
         <item name="android:layout_height">match_parent</item>
     </style>
    
  2. Create activity class SimpleVideoActivity:

    public class VideoPlayerActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_video_player_android_x);
            VideoView simpleVideoView = findViewById(R.id.simpleVideoView);
            MediaMetadata mediaMetaData = new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_TITLE, "Put your video text here").build();
            UriMediaItem item = new UriMediaItem.Builder(Uri.parse("Put your video URL here")).setMetadata(mediaMetaData).build();
            MediaPlayer mediaPlayer = new MediaPlayer(this);
            simpleVideoView.setPlayer(mediaPlayer);
            mediaPlayer.setMediaItem(item);
            mediaPlayer.prepare();
            mediaPlayer.play();
        }
    }