Search code examples
androidvideoandroid-videoviewviewflipper

Set time between videos


I am playing some videos in Video View using view flipper from phone storage and I want some break just like 5-10 seconds between two videos.

Here is my function to play multiple videos

    FileOutputStream fos;  
    final String [] path;
    int lengthPath;
    index = 0 ;

    try {  

        player = new VideoView(context);
        path=readFileFromMemory(DashBoard.this);
        if(path.length>0){
            player.setVideoPath(path[index]);
        }
        player.setMediaController(new MediaController(this));
        player.requestFocus();
        player.start();

        vfFlipper2.addView(player);

        ViewFlipper.LayoutParams param = new ViewFlipper.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        param.gravity = Gravity.CENTER;
        player.setLayoutParams(param);

        player.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {

                index=index+1;

                if(index<path.length){
                    player.setVideoPath(path[index]);
                    vfFlipper2.showNext();
                    player.start();
                }else{
                    index=0;
                    player.setVideoPath(path[index]);
                    vfFlipper2.showNext();
                    player.start();
                }
            }
        });



    }catch(Exception e){
        e.printStackTrace();
    }

ReadFileMemory Function is:

public String[] readFileFromMemory (Context activity)
{

    Bitmap bitmapreturned=null;

    File file;
    String []fleabc = null;
    int imageCount =0;

    File sdCard = new File(Environment.getExternalStorageDirectory()
            .getPath() + "/iDisplay/iDisplay - Videos/");
    imageCount = sdCard.listFiles().length;
    fleabc=new String[imageCount];
    for (int count = 0; count < imageCount ; count++) {

        file = new File (sdCard.listFiles()[count].getAbsolutePath());
        FileInputStream streamIn=null;
        fleabc[count]=file.getPath();               
    }
    return fleabc;
}

Solution

  • Add a Handler with 5/7 second delay then start the new video in onCompletiong

    Do this

    @Override
    public void onCompletion(MediaPlayer mp) {
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                playNextVideo();
            }
        }, 5000); // 5 second delay
    }
    
    private void playNextVideo()
    {
        index=index+1;
    
        if(index<path.length){
            player.setVideoPath(path[index]);
            vfFlipper2.showNext();
            player.start();
        }else{
            index=0;
            player.setVideoPath(path[index]);
            vfFlipper2.showNext();
            player.start();
        }
    }