Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android VideoView - How to play videos in sequence

I'm trying to develop an android application that plays more than one video in one videoview. When one is finished, the second has to start and so on.

My videos are stored in the raw folder of the project, to get their filenames i do:

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 one's path to my video view

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

myVideoView.setVideoURI(videoUri);

to play the others

    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 that i try this code in real device, i get the same error "can't play video" when the first video is finished...

all the videos are .mp4 file recorded with the same device that i use for develop...

any ideas? or other ways to play more videos in sequence? I looked for a solution everywhere but i couldn't find it..

EDIT

DID IT!! ok.. the error was so silly.. thank you all for the helpful answers.

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

as i wrote, the videos are stored in raw folder.. i was using

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

adding raw folder in path

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

it finally worked.. as i wrote.. it was a silly..

like image 248
Dario Defilippi Avatar asked Feb 04 '14 10:02

Dario Defilippi


2 Answers

Well you are getting error because you are trying to initialize videoview again before reseting it. Make a change to your code like this.

myVideoView.setOnCompletionListener(new   MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {

            startOtherVid();
        }
});

Now make method startOtherVid() and initialize videoview 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 will release one videoview object and create again. There will be a short time to load, but you can handle it visually.

Edit

You can also release 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();}
});

Cheers.:)

like image 159
BlueSword Avatar answered Oct 22 '22 23:10

BlueSword


Try something like that - it works perfect 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();
        }
    });
}
}
like image 27
Rajan Avatar answered Oct 22 '22 22:10

Rajan