Unable to play MKV Matroska video with exoPlayer 2.11

前端 未结 2 836
误落风尘
误落风尘 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: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

    
    
    
        
    
    
    

    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.

提交回复
热议问题