Unable to play MKV Matroska video with exoPlayer 2.11

前端 未结 2 804
误落风尘
误落风尘 2021-01-17 22:44

In my video player when i try to play MKV Matroska file it stay still the video is not playing.

i followed CodeLabs and ExoPlayer Dev and build player it

相关标签:
2条回答
  • 2021-01-17 23:06

    Try to make MediaSource like this:

    private MediaSource buildMediaSource(Uri uri) {
        DataSource.Factory dataSourceFactory =
            new DefaultDataSourceFactory(this, "exoplayer");
        return new ExtractorMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri);
    }
    
    0 讨论(0)
  • 2021-01-17 23:14

    I made a sample project on Github that works properly with your file. You can check and test it in the link below:

    https://github.com/squti/ExoPlayer-MKV-Sample

    If you just want to use the codes in your project, first uninstall the previously installed app from your device or emulator and then run the new one.

    Here are the essential codes of the project:

    MainActiviy.java

    public class MainActivity extends AppCompatActivity {
    
        private PlayerView playerView;
        private SimpleExoPlayer player;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            playerView = findViewById(R.id.player_view);
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
            playerView.setPlayer(player);
    
            DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(
                    this,
                    Util.getUserAgent(this, getString(R.string.app_name)));
            ProgressiveMediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(Uri.parse("file:///android_asset/jellyfish-3-mbps-hd-h264.mkv"));
    
            player.prepare(mediaSource);
            player.setPlayWhenReady(true);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            playerView.setPlayer(null);
            player.release();
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/player_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            app:resize_mode="fill" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Also, you need to add these dependencies to the app level Gradle file:

    implementation 'com.google.android.exoplayer:exoplayer-core:2.11.7'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.11.7'
    

    I put the video file in the asset folder but you can change it base on your needs.

    0 讨论(0)
提交回复
热议问题