Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android VideoView starting lots of videos sequentially

I'm testing this for about three days nonstop.

Here's the simple code:

private VideoView videoView;

--

videoView = (VideoView) findViewById(R.id.videoView);

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

getNewVideo();

--

private void getNewVideo(){
    File file = //Any File. I have a list of files and they are executed in order
    videoView.stopPlayback();
    videoView.setVideoPath(file.getPath());
    videoView.start();
}

--

<VideoView
    android:id="@+id/videoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />

I need to execute this code 24/7.

The thing is, independent of duration (in the very beggining, or after 20 minutes running), SOMETIMES, the video just freezes. There's no log, there's no exception, the only thing i know is the last thing that is running before freezes is onPrepared().

The log is the same to all attempts to call the video, the only difference is that the last attemps (the one that freezes), is just stops after on prepared is executed.

ANY tip is appreciated.

The build number im using is RKM MK902_ANDROID5.1.1-20151016.1805

with a RKM MK902II Android PC device, running in a TV Screen 42'

like image 490
Otuyh Avatar asked Feb 17 '17 23:02

Otuyh


2 Answers

I don't have much experience playing local video files (I assume this because your code says you have a "list of files executed in order"), but.....

In my experience you shouldn't call videoView.start() until the media has been properly initialized by the VideoView.

When you call videoView.setVideoPath("path") the VideoView will do some initialization procedures to load the Media. After VideoView has loaded/initialized the media it will then invoke its onPreparedListener callback (if you've set one). Only when this callback has been invoked by the VideoView should you call VideoView.start() to start video playback.

So...when you set the onCompletionListener on your VideoView... you should also set an onPreparedListener. When the VideoView reports back to your class that it's prepared....call VideoView.start() to start playback.

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

Also, in my code I make a call to VideoView.suspend() before I re-use the VideoView to play a subsequent video. Try adding a call to VideoView.suspend() in the beginning of your getNewVideo() method.

like image 69
dell116 Avatar answered Sep 18 '22 16:09

dell116


add

   videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {

                getNewVideo();
                return true;
            }
        });
like image 38
mlika Avatar answered Sep 22 '22 16:09

mlika