Search code examples
javaandroidexoplayerexoplayer2.xandroid-media3

'com.google.android.exoplayer2.ui.StyledPlayerView' is deprecated


StyledPlayerView' is deprecated and is showing in android studio in Exoplayer 2.19 and google advised me to use AndroidX Media3 but I don't get anything on how to migrate to AndroidX Media3. can anyone write the sample code for this one?

here is my sample XML and Java code.

<com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:show_buffering="when_playing"
        app:show_shuffle_button="true"
        app:surface_type="texture_view"
        app:use_controller="false" />

Java

        playerView = findViewById(R.id.player_view);

//        get path from raw folder ok.MOV video
        String path = "android.resource://" + getPackageName() + "/" + R.raw.ok;
        File file = new File(path);

        playerView.setPlayer(player);

        player.setMediaItem(MediaItem.fromUri(path));
        player.prepare();
        player.play();

how to do same in AndroidX Media3


Solution

  • exoplayer told that r2.19.0 will be the last release and ask people to migrate to new library called AndroidX Media3 Steps to use new library as far as i tried: Implementation:

         // media3 formally exo Player
        implementation 'androidx.media3:media3-exoplayer:1.1.0'
        implementation 'androidx.media3:media3-exoplayer-dash:1.1.0'
        implementation 'androidx.media3:media3-ui:1.1.0'
    

    Now add this xml code in your playerview:

     <androidx.media3.ui.PlayerView
            android:id="@+id/playerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:show_buffering="always"
            app:show_subtitle_button="true"
            app:use_controller="true" />
    

    Remember this uses the styled controllerview in the name of playerview so you dont have to add customcontroller but if you add you will get more control over the player now coding mostly it will be same :

     //remember to check the import correctly 
     //(import androidx.media3.ui.PlayerView;)
         PlayerView playerView;  
     //(importandroidx.media3.exoplayer.ExoPlayer;)
         ExoPlayer exoPlayer;
    

    initlization of player sample snippet from official library doc (untested):

    MediaItem mediaItem = MediaItem.fromUri(videoUri);
    // Set the media item to be played.
    player.setMediaItem(mediaItem);
    // Prepare the player.
    player.prepare();
    // Start the playback.
    player.play();
    

    Reference : media 3 Androidx doc

    As far as i checked they just changed name they did not change anything in core just we need to find and replace it correctly you can take a look at this open source thirdparty library called justplayer which also upgraded their exoplayer to media3 you can get more ideas in realtime Added that google has given a easy to use scrpt that migrate your project automatically from exo to media3 : guidehere.