Delay when switching video with Videoview

Asked

Viewed 32 times

0

Hi, I’m having a problem with a delay when making the video exchange of a playlist that contains images and videos as soon as the video ends in a Videoview.
The delay is about 200ms in my tvbox.
Running on emulator works normal.
I believe it is pq the processor the emulator uses is much superior to tvbox.
I also use an Imageview for interlink, is an infinite loop image and video player.


Is it possible to cache or something for the video to load faster? Or even other libraries/solutions (but I need to capture the event when a video is finished)

package br.com.usevision.usevisionplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.File;

public class videoPlayerActivity extends AppCompatActivity {

    VideoView videoView;
    ImageView imageView;
    int position = -1;

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

        videoView = (VideoView) findViewById(R.id.myPlayer);
        imageView = (ImageView) findViewById(R.id.myImage);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        position = getIntent().getIntExtra("position", -1);
        getSupportActionBar().hide();

        MediaController mediaController = new MediaController(this);
        mediaController.setVisibility(View.GONE);
        mediaController.setAnchorView(videoView);

        videoView.setMediaController(mediaController);

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                videoView.start();
            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                nextMidia();
            }
        });

        videoView.requestFocus();

        playerVideo();
    }

    private void playerVideo() {

        File media = MainActivity.fileArrayList.get(position);
        String mediaPath = String.valueOf(media);
        initVideo(mediaPath);
    }

    private void nextMidia() {
        if (MainActivity.fileArrayList.size() == 0)
            return;
        File video;
        try {
            if (MainActivity.fileArrayList.size() > position + 1) {
                video = MainActivity.fileArrayList.get(position = position + 1);
            } else {
                video = MainActivity.fileArrayList.get(position = 0);
            }
        } catch (Exception e) {
            video = MainActivity.fileArrayList.get(position = 0);
        }

        String pathMedia = String.valueOf(video);
        setVideo(pathMedia);
        if (!pathMedia.endsWith(".mp4")) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    nextMidia();
                }
            }, 5000);
        }
    }

    private void setVideo(String path) {
        if (path.endsWith(".mp4")) {
            videoView.setVisibility(View.VISIBLE);
            imageView.setVisibility(View.GONE);
            videoView.setVideoPath(path);
            videoView.start();
        } else {
            videoView.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
            File imgFile = new  File(path);
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            imageView.setImageBitmap(myBitmap);
        }
    }

    private void initVideo(String path) {
        setVideo(path);
        if (!path.endsWith(".mp4")) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    nextMidia();
                }
            }, 5000);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        videoView.stopPlayback();
    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.