Android VideoView launches multiple videos sequentially

I have been testing this for about three days without a break.

Here is a 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 fact is that regardless of the duration (at the very beginning or after 20 minutes of work), SOMETIMES, the video just hangs. There is no log, there is no exception, the only thing I know is the last thing that works before onPrepared () hangs.

The log is the same for all attempts to call a video, the only difference is that the last attemps (the one that hangs) just stop after it is prepared.

ANY tip is appreciated.

Build number im used by RKM MK902_ANDROID5.1.1-20151016.1805

with RKM MK902II PC-PC operating on 42 'TV

+6
source share
2 answers

I do not have much experience working with local video files (I assume this is because your code says that you have a "list of files in order"), but .....

In my experience, you shouldn't call videoView.start() until the media file is properly initialized with VideoView.

When you call videoView.setVideoPath("path") , VideoView will perform some initialization procedures to load Media. After the VideoView has loaded / initialized the medium, it will call the onPreparedListener callback (if you installed it). Only when this callback was called by VideoView, you need to call videoView.start() to start playing the video.

So ... when you install onCompletionListener in your VideoView ... you must also install onPreparedListener. When the VideoView returns to your class that it 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 reusing VideoView to play the next video. Try adding a call to VideoView.suspend() at the top of your getNewVideo() method.

+5
source

add

  videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mediaPlayer, int i, int i1) { getNewVideo(); return true; } }); 
+1
source

Source: https://habr.com/ru/post/1015078/


All Articles