954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

stop playing mu mp3 using jlayer

hi everybody,
i needed to play mp3 audio in my AudioPlayer project
i found jlayer is the easiest way to play mp3 audio,it plays well but can't stop playing
my GUI stops responding until the audio is finished!

any help please

this is the code of the playing button
-chooser is a jfilechooser object

file=chooser.getSelectedFile();
        FileInputStream f=new FileInputStream(file);
        ap=new AdvancedPlayer(f);
        ap=new AdvancedPlayer(f);
        ap.play();

and in the stop button

ap.stop()
ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

hi everybody, i needed to play mp3 audio in my AudioPlayer project i found jlayer is the easiest way to play mp3 audio,it plays well but can't stop playing my GUI stops responding until the audio is finished!

any help please

this is the code of the playing button -chooser is a jfilechooser object

file=chooser.getSelectedFile();
        FileInputStream f=new FileInputStream(file);
        ap=new AdvancedPlayer(f);
        ap=new AdvancedPlayer(f);
        ap.play();

and in the stop button

ap.stop()


I think the reason is that the player is taking up your entire thread, and therefore your thread of your gui will have to wait until its played finished. Create a new Runnable and play it using that, this then means your music will be playing on a separate thread and therefore not blocking your gui thread and the gui will continue as normal. see here: http://www.go4expert.com/forums/showthread.php?t=4202 You'd then have to make a variable like StopPlaying in the new class that implements the runnable, and when the user presses stop it changes the value of the boolean to true:

while(stopPlaying==false) {
}
ap.stop();//code will only reach here when stopPlaying is true...
DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

I think the reason is that the player is taking up your entire thread, and therefore your thread of your gui will have to wait until its played finished. Create a new Runnable and play it using that, this then means your music will be playing on a separate thread and therefore not blocking your gui thread and the gui will continue as normal. see here: http://www.go4expert.com/forums/showthread.php?t=4202 You'd then have to make a variable like StopPlaying in the new class that implements the runnable, and when the user presses stop it changes the value of the boolean to true:

while(stopPlaying==false) {
}
ap.stop();//code will only reach here when stopPlaying is true...


this also might be useful: http://www.informit.com/guides/content.aspx?g=java&seqNum=290 .. I couldn't find the exact way to stop it playing but i reckon you're right

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

If you start the player playing in its own thread, as correctly advised by cOrRuPtG3n3t!x, then I see no reason why you can't just call ap.stop() directly from the Swing thread (eg in a button's action listener) - no need for flag variables etc. (If you can't call stop() from a thread other than the one where play() was called, then it has no possible use!)

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

i'll try to make a separate thread

ringo_tech
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You