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:
Code :
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.
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)
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
Re: how to stop a looping audio clip when browser window is closed
Quote:
Originally Posted by
pbrockway2
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
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?
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.
Re: how to stop a looping audio clip when browser window is closed
Quote:
Originally Posted by
jps
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?:
Code :
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.
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.
Re: how to stop a looping audio clip when browser window is closed
destroy()
Override this method and drop your resources within there.
Re: how to stop a looping audio clip when browser window is closed
Quote:
Originally Posted by
aussiemcgr
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
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.
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.
Re: how to stop a looping audio clip when browser window is closed
Quote:
Originally Posted by
jps
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:
Code :
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.
Re: how to stop a looping audio clip when browser window is closed
nvm ... second thoughts...
Re: how to stop a looping audio clip when browser window is closed
Quote:
Originally Posted by
code-challenged
I saw it, see my edit above.
btw, here's what I have so far:
Code :
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
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...
Code java:
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
}
}
Re: how to stop a looping audio clip when browser window is closed
Quote:
Originally Posted by
jps
Not sure I understand this part. Can you be more specific?
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.
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.