Hi,
I am making a song player in java with playlist functionality. I got my mp3 songs running all right. What i basically do in my player is search a folder for mp3 files and then add all to the playlist. Till here, it's fine and doing good. But the problem is that i don't know how to use timer classes of java to schedule next song's playing in java after first's stop. I am using jTable to store song details and on click of the row on table song starts playing but after that since i am not able to code in timer, program does not play next song.
I need help with scheduling timers, provided length of current song is known to me.
Below is the code:
Custom Player- My player class.
mouse clicked action is on jTable1

        ` MouseListener ml=new MouseListener(){
        CustomPlayer p=new CustomPlayer();
        public void mouseClicked(MouseEvent e) {
            int column=jTable1.getSelectedColumn();
            final int row=jTable1.getSelectedRow();

            if(jTable1.getValueAt(row, column)!=null && column==0){

                System.out.println(jTable1.getValueAt(row, column));

                        if(p.isRunning)p.close();

                p.setPath((String) jTable1.getValueAt(row, 6));
                Timer t=new Timer();
                p.play(-1);


                }

            }

        public void mousePressed(MouseEvent e) {
        }public void mouseReleased(MouseEvent e) {
        }public void mouseEntered(MouseEvent e) {
        }public void mouseExited(MouseEvent e) {
        }
            };
        jTable1.addMouseListener(ml);
            }`

I hope i am clear. Thanks.

Recommended Answers

All 3 Replies

We don't have the specs for CustomPlayer, but most players have a listener interface where you can get a callback when the track has finished playing. That would be the best way to do it.
If not, there's loads of stuff on how to use timers, both in the API doc and on the web in general, eg
http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html

I don't think you got my question. I wanted to know how can i play the song next queued up in the playlist.

I tried to use timer.scheduleAtFixedRate() method but it does not solve the purpose as song durations are not fixed.

Also, custom player is a class made by some guy on Stack Overflow(i found it in a forum answer).
Custom Player has following methods and variables-> i.) isRunning- checks if playback is on(toggled on pause also) ii.)close()- method to close audio stream iii.) setPath()- sets path of player to a new mp3 file(used for changing songs) iv.) play() -for playback v.) pause- pauses the song

This class is working fine.
I just need help with changing the song which are not triggered by mouse click, because they are queued up next on playlist.(hence, the timer assistance)
Thanks.

that's exactly what I thought your question was!
You need to run some code (to start the next song) when the previous song finishes - right? CustomPlayer does not have any callbacks, so you can't do it that way. Timer is your only option, but scheduleAtFixedRate is not the right method.
If you start the current song and know its duration is x seconds, then you start a non-repeating Timer with a delay of x+1 seconds. When the Timer fires you play the next song. It's like the very first example of the link I gave you.

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.