3

I was testing a heavy loaded video, which loads the video and after Thread.sleep(1000); it plays second video. But once i play one after another in loop it freeze.

When i removed all those Thread.sleep(1000); it worked perfectly without freeze.

But i need to make a delay (but not using Thread.sleep method), how can we do this?

package test;

public class Test
{
  static String what = "0";
  public static void main(String args[]) 
  {                    
    Load.video720p("/tmp/START.mp4"); // This is 8 second movie playing
    new javax.swing.Timer(8000, new ActionListener() 
    {
        public void actionPerformed(ActionEvent ae) 
        {
          if (what.equals("0") ) 
          {
            /* After 8 seconds play 0.mp4 */
            callMe();
            what = "1";
          } else {
            /* After 8 seconds play 1.mp4 */
            callMe();
            what = "0";
          }
        }
    }).start(); /* Keep on looping every 8 seconds. */
  }

  /* 8 seconds interval call me. */
  public static void callMe()
  {
      try {

           /* Try 0: Freeze/Do not play */
           Load.video720p("/tmp/" +  what + ".mp4");                  

           /* Try 1: Does not change films (cant run)
           new Thread(new Runnable() {
             public void run() {
            Load.video720p("/tmp/" +  what + ".mp4", EVENT_TRIGGER_TRUE);                  
         }
           });*/

           /* Try 2: Fails
           try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
               public void run() {
                    Load.video720p("/tmp/" +  what + ".mp4");
               }
         });
        } catch (Exception e) { 
            System.err.println(e);
        }*/

        /* Try 3: Failes
        try {
         java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    Load.video720p("/tmp/" +  what + ".mp4");  
                }
         });
        } catch (Exception e) { 
            System.err.println(e);
        }*/

      } catch (Exception e) {
        System.out.println(e);
      } 
    }
} 
  • it's difficult to guess, what's going on in your program. please show some sample code of what you did – Hachi Nov 18 '11 at 12:15
  • Perhaps if the video code had some kind of callback to inform it has freed all the resources after each video is played. – Mister Smith Nov 18 '11 at 12:22
  • I think we need to know what is in `Load.video720p`. Can you show us the code? – Stephen C Nov 18 '11 at 12:24
  • @StephenC: well its an private SDK, i purchased which is closed source. And its a simple player only. –  Nov 18 '11 at 12:27
  • But you'll probably have some `Player` class or similar. If there were a method, say , `onStopped` or `onClosed` that would be the "cleaner" way you are looking for. – Mister Smith Nov 18 '11 at 13:27

2 Answers2

4

If you're calling sleep() on the event handling thread, then yes, your GUI will freeze during that time. A better idea is to use a SwingTimer which will allow you to trigger playing of the second video after a specified delay without calling sleep().

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Still could not make it working. It freeze as soon as its used inside Thread or Runnable or SwingTimer's... –  Nov 19 '11 at 17:08
  • Well, I answered before you showed the original code, remember. I assumed you already *were* playing the movie on the event thread, and that was the problem. But it turned out you weren't even creating a GUI, so obviously that's not it at all. Based on your comments about the proprietary player class, I think the only thing you can do is complain to the vendor, or go through their support or forums. This is obviously a bug or issue with that player library; it has nothing to do with any general Java programming issues. – Ernest Friedman-Hill Nov 19 '11 at 18:49
1

as mentioned above, SwingTimer is a good solution

if you want it a little more complicated but self-made start a new Thread to handle the sleeps and new Loads. but I would prefer the SwingTimer too

Hachi
  • 3,237
  • 1
  • 21
  • 29
  • no i didn't mean to use both; i meant sth like "new Thread { Load; sleep; ... }" and if this doesn't help you could try "new Thread { Load; new Thread { sleep; Load; }" but this is all just experimenting – Hachi Nov 18 '11 at 13:39
  • This is good idea, but it cant call Load.video720p method/function. e.g `new Thread() { Load.video720p("/tmp/ihateWhennotworks.mp4"); };` –  Nov 18 '11 at 14:08