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 3 of 3

Thread: Specifying the order & starting point of a queue<String>

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Specifying the order & starting point of a queue<String>

    I have a User Interface that handles a playlist, when you double click the song, it plays the song you selected, but after the song ends it starts at the first song in the directory ( I hope this made sense... )


    The class that plays MIDI files, loads all the songs from a directory into a queue in the EXACT order as they are in the directory


    What I want to do is whenever I play a song in the playlist, I want it to remove all the songs BEFORE it from the queue so that it only plays the songs following it

    But I'm clueless to how to do that

    This class is where the queue takes place,

    package sign;
     
    import java.util.Queue;
    import java.util.LinkedList;
    import java.io.File;
    import javax.sound.midi.*;
     
    public class PlayMusicInOrder {
     
            private static Sequencer seq;
     
            private static Queue<String> songQueue;
     
            public PlayMusicInOrder (Queue<String> songQueue) {
                    Sequencer seq = null;
                    try {
                            seq = MidiSystem.getSequencer();
                            seq.addMetaEventListener( new MetaEventListener()
                            {
                                    public void meta(MetaMessage msg){
                                            if(msg.getType()==0x2F)
                                            {
                                                    playNextSong();
                                            }
                                    }
                            });
                    }
                    catch (Exception e){}
                    PlayMusicInOrder.seq = seq;
                    PlayMusicInOrder.songQueue = songQueue;
     
            }
     
     
            public static void playNextSong(){
                    String nextSong = songQueue.remove();
                    System.out.printf("Creating sequence for %s", nextSong);
                    try {
                            Sequence sequence = MidiSystem.getSequence(new File(nextSong));
                            seq.setSequence(sequence);
                            System.out.println("Playing");
                            PlayMusicInOrder.seq.start();
                    }       catch (Exception e){
                            e.printStackTrace();
                            playNextSong();
                    }
            }
     
            public void startplaying () {
                    try {
                            seq.open();
                    }
                    catch (Exception e){ e.printStackTrace(); }
                    playNextSong();
            }
            public static void play(){
                    Queue<String> songqueue = new LinkedList<String>();
                    PlayMusicInOrder.addMidiFiles(songqueue,new File( "C:\\Users\\Kyle\\Desktop\\clieent\\Client\\cache\\Music\\"));
                    PlayMusicInOrder player = new PlayMusicInOrder (songqueue);
                    player.startplaying();
            };
     
            public static void addMidiFiles(Queue<String> queue, File topdir){
                    try {
                            for (File f: topdir.listFiles())
                                    if (f.isDirectory()) {
                                            addMidiFiles(queue, f);
                                    } else if (f.isFile())
                                    {
                                            //System.out.println(f);
                                            queue.add(f.getAbsolutePath());
                                    }
                                    else
                                    {}
                    } catch (NullPointerException e) {
                            e.printStackTrace();
                    }
            }
    }


    Any help would be appreciated

    If I'm missing any information, please let me know.

    p.s What is a 'trackback'?

    I'm new here


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Specifying the order & starting point of a queue<String>

    I want it to remove all the songs BEFORE it from the queue
    Look at the API doc for the Queue class and find a method that removes the next item in the queue.
    Make a loop that removes elements from the queue until it gets to the desired place in the queue.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    My Mood
    Happy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Specifying the order & starting point of a queue<String>

    Quote Originally Posted by Norm View Post
    Look at the API doc for the Queue class and find a method that removes the next item in the queue.
    Make a loop that removes elements from the queue until it gets to the desired place in the queue.

    Thanks

Similar Threads

  1. [SOLVED] Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 19th, 2011, 04:18 PM

Tags for this Thread