how can I stop my applet freezing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Garcia
    New Member
    • Aug 2010
    • 2

    how can I stop my applet freezing?

    hi I'm completely new in Java's world, I made an applet that download a video file from the server, but here is the thing, when the recording process gets started my applet just freeze (doesn't accept any kind of event, like click button, enable or disable components) and when it finish then the applet come back to a normal state.


    here is the code :

    Code:
    public void startRecordingProcess(String file){
       
         try
         {
            
            /*
             * Get a connection to the URL and start up
             * a buffered reader.
             */
            browseFolderBtn.setEnabled(false);
            long startTime = System.currentTimeMillis();
    
            System.out.println("Connecting to the server\n");
    
            URL url = new URL("http://xserverX/hd/video.flv");
            url.openConnection();
            InputStream reader = url.openStream();
    
            /*
             * Setup a buffered file writer to write
             * out what we read from the website.
             */
            
            FileOutputStream writer = new FileOutputStream(file);
            byte[] buffer = new byte[153600];
            int totalBytesRead = 0;
            int bytesRead = 0;
    
            System.out.println("Recording video........\n");
    
            while ((bytesRead = reader.read(buffer)) > 0)
            {
               writer.write(buffer, 0, bytesRead);
               buffer = new byte[153600];
               totalBytesRead += bytesRead;
            }
    
            long endTime = System.currentTimeMillis();
    
            System.out.println("Finish. " + (new Integer(totalBytesRead).toString()) + " bytes read (" + (new Long(endTime - startTime).toString()) + " millseconds).\n");
            writer.close();
            reader.close();
            browseFolderBtn.setEnabled(true);
         }
         catch (MalformedURLException e)
         {
            e.printStackTrace();
         }
         catch (IOException e)
         {
            e.printStackTrace(); 
         }
        }
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi there!

    I'm no expert on applet writing, but I'm guessing you aren't using threads? That would certainly explain the freezing up of the applet when you start downloading. If you create a separate thread for the download, the applet should continue working.

    Greetings,
    Nepomuk

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      What is the size of the video that you are trying to download? Is it too big?

      Regards
      Dheeraj Joshi

      Comment

      • Ivan Garcia
        New Member
        • Aug 2010
        • 2

        #4
        @Nepomuk right now im creating a thread for the download process, at the end of the day I hope this class helps me to download multiple video files at the same time. Thank you.

        @Dheeraj Joshi actually the video size is dynamic but it could be between 500 mb to 4GB, does that make a difference?. Thank you.

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Yes, it could allow you to download several videos at a time. Or have it do something else at the same time. So, hope it works out. :-)

          From what I can tell, the video size shouldn't normally cause anything like this - if anything it would fill up the RAM and therefore block the computer from doing much at all, but that would influence more than just the rest of the applet. 4GB could possibly do that, if it's not written to disk fast enough, but I think that from the way your program works that shouldn't be the problem.

          Greetings,
          Nepomuk

          Comment

          Working...