Hi,
I am trying to build a Music player in Java using Jlayer Library.
Hi am using AdvancedPlayer and its play() method for playing song.
It plays the file but i does not stop until song is finished even Stop button is not pressed,window listener doesn/t works.
I am unable to solve the problem.I want to play,pause and stop song when i hit the respective button.
Thank you.

Recommended Answers

All 8 Replies

without any code, that is very difficult to see. but I made an mp3 player with JLayer once, and it's doable.
but what do you mean, WindowListener? what do you need a WindowListener for? why don't you use an ActionListener?

commented: very good idea. but i dont know hhowit is +0

Thanks but i solved the problem by creating another runnable class to play mp3 & then creating thread in my main class.

But Thanks for your help.

Any changes to the UI during run time must always be made inside a Thread. Example, inside ActionListener, if you want to update UI like adding label or disable button, it must be done inside a thread.

Example:

Thread r = new Thread(new Runnable() {
    public void run()
    {
        button1.setEnabled(false);
    }
});

r.start();
commented: This information is incorrect -3
commented: not to mention irrelevant. -3

Any changes to the UI during run time must always be made inside a Thread.

This is really bad advice. I hope there are no tutorials on the web making this sort of suggestion. For one thing, anytime you do anything at runtime it must be inside a thread because all things are done in threads; that's just the only place there is, but that doesn't mean you should create a new thread for every little thing you want to do. That would make thread-safety even more of a nightmare than it already is.

For Swing and AWT UIs especially you must never do it that way. You are only allowed to make changes in the UI from the event dispatch thread. If you do it from any other thread you are not thread-safe and you are asking for trouble. You might not actually get trouble, but that's just a matter of luck.

If you are inside the actionPerformed method of an ActionListener then you can expect to be in the event dispatch thread and so it is safe to modify Swing UI components as long as you don't create a new thread to do it.

commented: Very good advice. :-) +13

Rajesh:
yes, you are right... but then again: all java code runs inside a thread.

if you mean a separate thread just to change something in the UI? ridiculous.

also: this has what to do with a musicplayer in Java??

Rajesh's post is about as wrong as it's possible to be.
ActionListeners are called on the Swing Event Dispatch Thread. Most Swing methods are not thread safe and MUST be called from the EDT and no other thread.
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
Rajesh would have you leave the EDT in order to update Swing components from a non EDT thread - exactly the opposite of right.
bguild has it right.

I have a button on a JFrame, clicking which some action is invoked. I wanted the button to get disabled once clicked, then enable again after action is complete. Inside actionPerformed method, I did button1.setEnabled(false); but it didnt get disabled immediately. But, when I put that in a separate thread, it behaved as expected (disabled immediately upon clicking).

Also I was dynamically adding a Label to the frame. It only worked when I put the code inside a separate thread.

You have made the very common mistake of putting a long-running process on the Swing EDT. That blocks all Swing activity until you return from your ActionPerformed, and so enabling/disabling etc doesn't get implemented in the GUI until after your ActionPerformed is finished. Moving your swing calls onto another thread looks like a solution, but it's a threading bug just waiting to happen. Don't be embarassed, very many beginners make the same mistake.

Study this tutorial to see the correct way to write this kind of code:
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.