java jukebox

i want to implement a jukebox in java
where a user chooses a tune from a category, then this gets sent to a play list, if tunes are waiting then it gets put to the back on queue, then gets played when the others have finished. What would be the structure of this in code?
create a tune class, then use a vector of tunes for a playlist.One thing i was wondering is how to know when a particular tune is playing and then call the next one from the playlist, would this use a thread?
please help!! please provide some example code.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

_lychee_Connect With a Mentor Commented:
well, in ur tuneplayer class u can have a boolean that records if it is playing, say

private boolean playing;

add a query function... like

public boolean isPlaying() {return playing;}

and in the method play (which plays the tune) u can do something like

public void play(Tune t) {
   playing = true;
   ..... play the tune ......
   playing = false;

no synchro is needed cos this state doesn't matter much anyway....

alternatively, if the reason for wanting to know if it's playing is to do some UI thingy u can call the UI function to start/stop the animation b4/after playing

u're most welcome :-)
dappleAuthor Commented:
Edited text of question.
u can do it like so:

have 2 threads -- player and user-interface

an intermediate class will act as a 'tune queue' -- something like an event queue... say it has a put method (for the UI to put in tunes) and a get method (for the player to get the next tune in place)... the get can block if the queue is empty and the put will return immediately.. so maybe something like (i'm using the Collection API here):

public class TuneQueue {
   private List l;

   public TuneQueue() {
      l = new LinkedList();

   public synchronized Tune get() {
      while (l.size() == 0) {
         try {
         } catch (Exception e) { }
      return (Tune) l.remove(0);

   public synchronized void put(Tune a) {
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

so the player thread will just keep on going... like

for (;;) {
    tune = tuneQueue.get();

and the UI thread will put in tunes whenever the user selects it... by using tuneQueue.put(whatever)...
dappleAuthor Commented:
would it be
return(l(0)) not tune
what would cause the player thread to exit?
sorry... i don't get the second line...
if u're asking about the
return (Tune) l.remove(0); statement, the explanation is
l.remove(0) will remove the first element of l (which is the oldest) and return it as an Object... so the cast is to cast it to Tune for the return...

nothing unfortunately... however, a jukebox is supposed to "turn off" when the jukebox is "off"ed... that'll probably come from the UI thread... u can just call System.exit(int) and all threads will be 'killed'...

however, when there are no tunes, that thread will block and wait for tunes to be added
dappleAuthor Commented:
ah i see casted
one last thing how will i know when
a tune is playing and for the next one to be played, please answer the question for the points
you have been very helpful !
just realised there wuz something else... i don't really understand  "for the next one to be played"... do u mean how do u know the next song to be played izzit?

well... u can implement a query method in the TuneQueue class.... something like

public synchronized Tune nextTune() {
   return (l.size()>0) ? l.get(0) : null;

which'll return null if no tunes are in the queue or the next tune
dappleAuthor Commented:
ok understand
cheerz thanks for your help
sorry to ask u so many questions!

noprob :)
we're here to help after all!
All Courses

From novice to tech pro — start learning today.