Pregunta

I have an android application and it allows to play a videofile, in particular an episode of different series from one web media resource .

A videofile is being played next way:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(mp4url), "video/mp4");
startActivity(intent);

where mp4url - is a String variable

Now I want to allow the programm play a queue of videofiles.

I tryed to make next way:

for(int iteration=arg2;iteration<episodes.size(); iteration++){
    Intent intent1 = new Intent(Intent.ACTION_VIEW);
    intent1.setDataAndType(Uri.parse(mp4Url1), "video/mp4");
    startActivity(intent1);
}

where arg2 shows a position of chosen videofile and episodes is a ArrayList variable for the queue of videofile to be played after chosen videofile.

But it did not work. Actually all videofiles are playeable but
simultaneously. I need them to play in turn. How can I reorganise my code effectivly?

Thank you for your attention.

¿Fue útil?

Solución

Create an arraylist of movie urls(e.g yourURLList) Define an int constant as static final int VIDEO_PLAYBACK_FINISHED = 32

You can keep a count to keep track of which video is being played like:

static int VIDEO_RUNNING = 0

Create a method like this inside the caller activity:

private void startMovie(int serialOfMovie){
        String vdoURL= yourURLList.get(serialOfMovie);//array list of movie urls
        if(vdoURL != null)
        {
            //video url for that index is found,if not then it would not enter here
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(vdoURL), "video/mp4");
            startActivityForResult(intent,
                     VIDEO_PLAYBACK_FINISHED);
        }

}

And inside the caller activity(the one from which you are invoking the intents you are creating) don't forget to add this snippet:

protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == VIDEO_PLAYBACK_FINISHED) {

             if (resultCode == RESULT_OK) {
                 //one video is finished
                 VIDEO_RUNNING++; //increase which vdo is running count
                 startMovie(VIDEO_RUNNING);
             }
         }
     }

Now when you are about to play this series of movies(starting from the beginning),just call startMovie(VIDEO_RUNNING);//VIDEO_RUNNING is 0 initially,so it will point the first url in url list.

Remember if you are using MediaPlayer to play movie in the activity(the one you call using intents with movie url),then when the movie has finished playing you should finish that activity. Like if you have a MediaPlayer instance called mPlayer in that activity then you should do this:

mPlayer.setOnCompletionListener(new
    OnCompletionListener() {        
        @Override
        public void onCompletion(MediaPlayer arg0) {
        finish();              
    }
});

This way you should have the queue effect with movies being played one after another.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top