Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: how to stop a looping audio clip when browser window is closed

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question how to stop a looping audio clip when browser window is closed

    I'm trying to fix some very old code.

    The problem is that the looping audio clip continues, even when the browser window is closed.

    In other words, the browser needs to be completely shut down for the music (i.e. the applet) to stop running in the background or whatever. I need a way to, at the very least, stop the sound from playing when the window containing the applet is closed. Even better: close the applet completely when closing the containing window (is this possible?).

    Here's how the code basically works:

    AudioClip music;
     
    music=getAudioClip(getDocumentBase(),"file.mid");
     
    music.loop();

    Any ideas? I figure this should be fairly straightforward to do. I just don't know how to approach it.

    Again, essentially, I need a "music.stop();" command to be issued on window close, at the very least.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    AudioClip has a method that stops the clip playing. Call it from within the "milestone" method that is cqlled when you leave/close the page. See Methods for Milestones (The Java™ Tutorials > Deployment > Java Applets)

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    With what little you provided, maybe call jFrame.EXIT_ON_CLOSE or use the windowClosing method of a WindowListener. If that seems foreign to you, post more code so we can see what you are working with

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Quote Originally Posted by pbrockway2 View Post
    AudioClip has a method that stops the clip playing. Call it from within the "milestone" method that is cqlled when you leave/close the page. See Methods for Milestones (The Java™ Tutorials > Deployment > Java Applets)
    I realize this is a very newb issue, but I don't see how that page helps me. I read it through thrice. It just seems to mention some things in passing (it says there are 4 processes that an applet will halt at certain milestones). It gives no examples of code, and I am very rusty with coding.

    Quote Originally Posted by jps View Post
    With what little you provided, maybe call jFrame.EXIT_ON_CLOSE or use the windowClosing method of a WindowListener. If that seems foreign to you, post more code so we can see what you are working with
    I'd post more code, but it's many lines of code and quite inefficient (I originally coded this thing a decade ago). Just simplifying it to something I could post would take a very long time, and I might still miss important parts.

    What sort of code/information do you need to see?
    Last edited by code-challenged; August 9th, 2012 at 09:20 PM.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Well I am just guessing you are using jframe. Seeing code would simplify the guessing what you are using process.
    Have you investigated the windowClosing method? This method is called when the window is closed, and gives you the opportunity to do stuff when the window closes. The EXIT_ON_CLOSE will terminate your app when the window closes the same way System.exit would. That should also drop the sound loop. Unless you have something going on that keeps it open. Again, without seeing the code involved with the sound, it is not easy to just invent a solution. I would suggest you check out those two options. But if your code is really in that bad of shape, perhaps an up to date restart is in order. (no one says it is fun or easy, but some say it is required, again, without seeing the code I can't say)
    Good luck, hope it works out with an easy fix.

  6. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Quote Originally Posted by jps View Post
    Well I am just guessing you are using jframe. Seeing code would simplify the guessing what you are using process.
    Have you investigated the windowClosing method? This method is called when the window is closed, and gives you the opportunity to do stuff when the window closes. The EXIT_ON_CLOSE will terminate your app when the window closes the same way System.exit would. That should also drop the sound loop. Unless you have something going on that keeps it open. Again, without seeing the code involved with the sound, it is not easy to just invent a solution. I would suggest you check out those two options. But if your code is really in that bad of shape, perhaps an up to date restart is in order. (no one says it is fun or easy, but some say it is required, again, without seeing the code I can't say)
    Good luck, hope it works out with an easy fix.
    Just did a quick text search of my code, and not a single "jframe" came up, so I'm guessing I'm not using that.

    Does the header of my code help you in any way?:

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class programName extends Applet implements KeyListener, MouseListener {

    I've definitely gone from intermediate to beginner in some ways. I barely know how to talk about this code anymore. But then again, I haven't coded regularly in many years.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    I don't know the solution, but I do know the problem: the JVM doesn't stop running just because you leave the page or close the browser. There is a brief period of time where the JVM still runs after you have left the applet. I'm unsure how to tell whether or not the applet has been left; I would actually like to know the answer myself.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    destroy()
    Override this method and drop your resources within there.

  9. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Quote Originally Posted by aussiemcgr View Post
    I don't know the solution, but I do know the problem: the JVM doesn't stop running just because you leave the page or close the browser. There is a brief period of time where the JVM still runs after you have left the applet.
    That period of time seems to be 60 seconds. I just tested it.

    Perhaps there is a way to tell it to shorten that time to say, 1 second?

    Quote Originally Posted by jps View Post
    destroy()
    Override this method and drop your resources within there.
    I appreciate you trying to help, but unless you give me examples of code (where I can extrapolate from and adapt into my own code), I don't think I can figure out how to use that. Like, I see that as "use the destroy function, and apply it to the resources in question". In practice, I don't know how to do it, syntactically or logically.
    Last edited by code-challenged; August 10th, 2012 at 09:09 PM.

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    You may have been writing at the time I posted. Just so the thread gets a bump.. See post #8.

  11. #11
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Quote Originally Posted by jps View Post
    You may have been writing at the time I posted. Just so the thread gets a bump.. See post #8.
    I saw it, see my edit above.

    btw, here's what I have so far:

    class killApp extends Thread {
       public void destroy() {
       }
     }

    Incredibly basic, I know. Am I even on the right track? I still have no way to tell it to run that thread when the window closes. That's what I'm not understanding how to do.

    Also: if I'm missing anything obvious (like inputting things into the method or thread), please tell me.
    Last edited by code-challenged; August 10th, 2012 at 09:24 PM.

  12. #12
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    nvm ... second thoughts...
    Last edited by pbrockway2; August 10th, 2012 at 09:26 PM.

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Quote Originally Posted by code-challenged View Post
    I saw it, see my edit above.

    btw, here's what I have so far:

    class killApp extends Thread {
       public void destroy() {
       }
     }

    Incredibly basic, I know. Am I even on the right track? I still have no way to tell it to run that thread when the window closes. That's what I'm not understanding how to do.

    Also: if I'm missing anything obvious (like inputting things into the method or thread), please tell me.
    Well it is a method of applet, not Thread. Second it is a method of applet, and not your class, so an Override notation would be useful. Follow the link in post #8 for more information on it

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Ok I read your edited post. It is better not to edit, as when I (we?) read, very rarely do I reread previous posts, just the new stuff...
    So some code...
    public class MyApplet extends Applet {
     
        private void methodOne() {
            //do stuff
        }
     
        @Override//etc
        public void destroy() {
            //kill looping sound and/or threads running and/or other things
        }
     
    }
    Last edited by jps; August 10th, 2012 at 10:46 PM. Reason: added code tags

  15. #15
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    Quote Originally Posted by jps View Post
        @Override//etc
    Not sure I understand this part. Can you be more specific?

  16. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    When the class Applet was defined, it included a method called destroy(). When an Applet's containing window is closed (and other times) this destroy() method is called. When you want to use the method within your class which extends Applet, you override the method. That means within your class you redefine the method's body, giving you the power to say what happens when this method is called. The @Override is for javadoc. If you have not learned that yet, it is a way to make comments in your code available to users of the code in a formatted manner. Also within an IDE you can mouse-over keywords and get a popup which shows the javadoc comments as you type your code. Search for javadoc for more information on that, there is plenty to read about it online.

  17. #17
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to stop a looping audio clip when browser window is closed

    I get the feeling this topic is beyond the scope of my knowledge. I may have understood it better a few years ago, but like I said, I am very rusty. I do understand some of it though, conceptually at least.

    I will look into this when I find the time. Thanks for your posts thus far.

Similar Threads

  1. Take dialog out of browser window
    By IliaPrikhodko in forum Web Frameworks
    Replies: 0
    Last Post: April 15th, 2012, 07:59 PM
  2. How do I stop my answer from looping?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 5th, 2012, 10:50 AM
  3. [SOLVED] How to stop the same window from having more than one instance of it open at a time.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 05:47 PM
  4. [SOLVED] Make it stop looping! please. :)
    By Hallowed in forum What's Wrong With My Code?
    Replies: 11
    Last Post: May 7th, 2011, 11:20 AM
  5. Do I need to close an audio clip?
    By m2oswald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2010, 01:20 AM