Android VideoView - how to play video in sequence

I am trying to develop an Android application that plays more than one video in one video. When one of them is finished, the second should begin, etc.

My videos are saved in the project source folder to get my file names that I make:

Field[] fields = R.raw.class.getFields(); final List<String> videoNames = new ArrayList<String>() ; for(Field field: fields){ videoNames.add(field.getName()); } 

then I set the first path to my video image

  videoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + videoNames.get(0)); myVideoView.setVideoURI(videoUri); 

to play the rest

  myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { videoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + videoNames.get(VideoI)); myVideoView.setVideoURI(videoUri); myVideoView.start(); if(VideoI==videoNames.size()-1) { VideoI=0; } else{ VideoI++; } } }); 

BUT...

every time I try to use this code in a real device, I get the same error “cannot play video” when the first video is completed ...

all videos are .mp4 files recorded with the same device that I use for development ...

any ideas? or other ways to play more videos in sequence? I was looking for a solution everywhere, but I could not find it.

EDIT

DID THIS!! ok .. the mistake was so stupid. Thanks to everyone for the helpful answers.

the error was in the path I was looking for ("these are not the paths you are looking for" cit.)

as I wrote, videos are stored in a raw folder. I used

  videoUri = Uri.parse("android.resource://" + MainActivity.ctx.getPackageName() + "/ + videoNames.get(VideoI)); 

add source folder to path

 videoUri = Uri.parse("android.resource://" + MainActivity.ctx.getPackageName() + "/raw/" + videoNames.get(VideoI)); 

he finally worked .. as I wrote .. it was stupid ..

+6
source share
2 answers

Well, you get an error because you are trying to initialize the video again before restarting it. Make changes to your code as follows.

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

Now make the startOtherVid() method and initialize the video here after releasing the previous one.

 private void startOtherVid(){ myVideoView.stopPlayback(); videoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + videoNames.get(VideoI)); myVideoView.setVideoURI(videoUri); myVideoView.start(); .... ..... } 

This way you release one video ad object and create it again. It will take a short time to load, but you can process it visually.

Edit

You can also free the mediaplayer object and solve your problem.

 public void onCompletion(MediaPlayer mp) { try { mp.reset(); videoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + videoNames.get(VideoI)); myVideoView.setVideoURI(videoUri); myVideoView.start(); } catch(Exception e){e.printstacktrace();} }); 

Greetings :.)

+5
source

Try something like this - it works great for me.

 public class MainActivity extends Activity { private VideoView videoView = null; String[] videoArray = {"video1", "video2"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[0]); videoView = (VideoView)findViewById(R.id.videoView); videoView.setVideoURI(videoUri); videoView.start(); videoView.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[1]); videoView.setVideoURI(videoUri); videoView.start(); } }); } } 
+5
source

All Articles