Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a demo to play WAV file and stuck with this. When I begin to play the music, I disable the btnPlay. Now I want when the music finish playing, the btnPlay must be enable automatically. But I can't do this. I can get the file duration in microsecond but I don't know what to do next. Here is the code.

Java
public void playmusic(){
        try{
            btnClose.setEnabled(true);
            btnShuffle.setEnabled(true);
            btnRepeat.setEnabled(true);
            btnPause.setEnabled(true);
            btnStop.setEnabled(true);
            if(isPausing==false){
                AudioInputStream ais = AudioSystem.getAudioInputStream(f);
                clip = AudioSystem.getClip();
                clip.open(ais);
                clip.start();
            }
            else{
                isPausing=false;
                clip.start();
            }
        }
        catch(Exception ex){}
    }


Could you give me a solution for this? Thank you very much!
Posted
Updated 25-Jun-13 6:07am
v3

It would appear that you need to subscribe to an event. It would seem likely that the clip object has a completed(), DonePlaying(), Finished() or some such event that would fire when the clip is completed. In this event, it would seem likely to enable your play button, but you'll also want to disable the pause and stop you started above.

Make a new method to control your buttons too - pass in bool that says if playing or not, and then toggle your buttons there, calling it in the above routine, and in the new event handler you're going to subscribe to.
 
Share this answer
 
Comments
Minh Hikari 25-Jun-13 13:08pm    
I understand what you mean. But I don't know how to make the program know when the music is finished playing.
Check Observer Pattern.
the button is the observer and audio steam is observable subject. you can raise an notify(), once your audio is complete.

To check the status of your audio, you will need a thread to check (ie. the sized of streamed bytes or something similar) - if the stream object is pre-implemented.
 
Share this answer
 
I found the way. Just add a LineListener and everything works fine... :D


Java
public void playmusic(){
        try{
            btnClose.setEnabled(true);
            btnShuffle.setEnabled(true);
            btnRepeat.setEnabled(true);
            btnPause.setEnabled(true);
            btnStop.setEnabled(true);
            LineListener listener = new LineListener() {
                public void update(LineEvent event) {
                    if (event.getType() == LineEvent.Type.STOP) {
                        btnPlay.setEnabled(true);
                    }        
                }
            };
            
            if(isPausing==false){
                AudioInputStream ais = AudioSystem.getAudioInputStream(f);
                clip = AudioSystem.getClip();
                clip.open(ais);
                clip.start();
            }
            else{
                isPausing=false;
                clip.start();
            }
            clip.addLineListener(listener);
        }
        catch(Exception ex){}
    }
 
Share this answer
 
Comments
Shubhashish_Mandal 26-Jun-13 2:34am    
Then marked it answered,so that other will get help from the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900