0

I have a simple video player app in Android that plays two videos given a URL. One a smaller HEVC-coded mp4 file and the other one a bigger size H264 encoded file. Basically I want to show that the HEVC file buffers and plays faster simply because of it's smaller size. But I have problems showing this concept and what I did doesn't really show that. I initialize the second player after the first one, and get the time to buffer 100% of content and show it below each player. Apparently it doesn't show what I'm intending to prove.

What am I doing wrong here? What are ways to make it prove the concept?

enter image description here

Here is my simple code:

package com.example.videoplayer;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
    //https://dl5.webmfiles.org/video-h264.mkv
    //https://s3.amazonaws.com/x265.org/video/BigBuckBunny_2000hevc.mp4
    private static final String VIDEO_SAMPLE =
            "https://dl5.webmfiles.org/video-h264.mkv";
    private static final String VIDEO_SAMPLE2 =
            "https://dl5.webmfiles.org/video-h265.mkv";
    private VideoView vw1, vw2;
    private TextView mBufferingTextView,mBufferingTextView2;
    // Current playback position (in milliseconds).
    private int mCurrentPosition = 0;
    // Tag for the instance state bundle.
    private static final String PLAYBACK_TIME = "play_time";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vw1 = findViewById(R.id.videoView1);
        vw2 = findViewById(R.id.videoView2);
        mBufferingTextView = findViewById(R.id.buffering_textview);
        mBufferingTextView2 = findViewById(R.id.buffering_textview2);

        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(PLAYBACK_TIME);
        }

        // Set up the media controller widget and attach it to the video view.
//        MediaController controller = new MediaController(vw1.getContext());
//        controller.setMediaPlayer(vw1);
//        vw1.setMediaController(controller);
    }


    @Override
    protected void onStart() {
        super.onStart();

        // Load the media each time onStart() is called.
        initializePlayer();
        initializePlayer2();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();

        // Media playback takes a lot of resources, so everything should be
        // stopped and released at this time.
//        releasePlayer();
    }

    private void initializePlayer() {
        // Show the "Buffering..." message while the video loads.
        mBufferingTextView.setVisibility(VideoView.VISIBLE);
        // Buffer and decode the video sample.
        long t0 = System.currentTimeMillis();
        Uri videoUri = Uri.parse(VIDEO_SAMPLE);
        vw1.setVideoURI(videoUri);

        // Listener for onPrepared() event (runs after the media is prepared).
        vw1.setOnPreparedListener(
                new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        // Hide buffering message.
//                        mBufferingTextView.setVisibility(VideoView.INVISIBLE);

                        // Restore saved position, if available.
//                        if (mCurrentPosition > 0) {
//                            vw1.seekTo(mCurrentPosition);
//                        } else {
//                            // Skipping to 1 shows the first frame of the video.
//                            vw1.seekTo(1);
//                        }

                        // Start playing!
                        vw1.start();


                        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                            boolean buffered = false;
                            @Override
                            public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {

                                if(i==100 && !buffered) {
                                    long t1 = System.currentTimeMillis();
                                    mBufferingTextView.setText((t1 - t0) + ", " + i + " ");
                                    buffered = true;
                                }
                            }
                    });
                    }
        });

    // Listener for onCompletion() event (runs after media has finished
    // playing).
        vw1.setOnCompletionListener(
                new MediaPlayer.OnCompletionListener()

    {
        @Override
        public void onCompletion (MediaPlayer mediaPlayer){
        Toast.makeText(MainActivity.this,
                "Completed",
                Toast.LENGTH_SHORT).show();

        // Return the video position to the start.
        vw1.seekTo(0);
    }
    });
}

    private void initializePlayer2() {
        // Show the "Buffering..." message while the video loads.
        mBufferingTextView2.setVisibility(VideoView.VISIBLE);
        // Buffer and decode the video sample.
        long t0 = System.currentTimeMillis();
        Uri videoUri = Uri.parse(VIDEO_SAMPLE2);
        vw2.setVideoURI(videoUri);

        // Listener for onPrepared() event (runs after the media is prepared).
        vw2.setOnPreparedListener(
                new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        // Hide buffering message.
//                        mBufferingTextView.setVisibility(VideoView.INVISIBLE);

                        // Restore saved position, if available.
//                        if (mCurrentPosition > 0) {
//                            vw2.seekTo(mCurrentPosition);
//                        } else {
//                            // Skipping to 1 shows the first frame of the video.
//                            vw2.seekTo(1);
//                        }

                        // Start playing!
                        vw2.start();


                        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                            boolean buffered = false;
                            @Override
                            public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {

                                if(i==100 && !buffered) {
                                    long t1 = System.currentTimeMillis();
                                    mBufferingTextView2.setText((t1 - t0) + ", " + i + " ");
                                    buffered = true;
                                }
                            }
                        });
                    }
                });

        // Listener for onCompletion() event (runs after media has finished
        // playing).
        vw2.setOnCompletionListener(
                new MediaPlayer.OnCompletionListener()

                {
                    @Override
                    public void onCompletion (MediaPlayer mediaPlayer){
                        Toast.makeText(MainActivity.this,
                                "Completed",
                                Toast.LENGTH_SHORT).show();

                        // Return the video position to the start.
                        vw2.seekTo(0);
                    }
                });
    }


    // Release all media-related resources. In a more complicated app this
    // might involve unregistering listeners or releasing audio focus.
    private void releasePlayer() {
        vw1.stopPlayback();
    }
}

And the layout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <!-- adding VideoView to the layout -->
    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/buffering_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Buffering..."
        android:layout_margin="8dp"
        android:textSize="18sp"
        android:textStyle="bold"
        />

    <VideoView
        android:id="@+id/videoView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



    <TextView
        android:id="@+id/buffering_textview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Buffering..."
        android:layout_margin="8dp"
        android:textSize="18sp"
        android:textStyle="bold"
        />

</LinearLayout>