Olle Muronde
Olle Muronde

Reputation: 15

Synching audio in java

I have an audio button for pausing and playing. I am using a single button for this feature i.e when the user presses play the sound plays and then the button text changes to pause. I can't seem to get the audio to pause (the rest of it works)

private JButton playBtn;
    private Boolean pauseStatus = false;

    public SoundClass() throws UnsupportedAudioFileException, IOException {
        setLayout(new BorderLayout());

        playBtn = new JButton("Play Audio");
        playBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {

                    if(pauseStatus==false){
                    setPauseStatus(false);
                    play();}

                    if (getPlayStatus() == true) {
                        play();
                        playBtn.setText("Play Audio");
                        setPauseStatus(false);

                    } else {

                  playBtn.setText("Pause Audio");
                   setPauseStatus(true);
                    }

                } catch (LineUnavailableException ex) {
                    Logger.getLogger(SoundClass.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        });

        add(playBtn, BorderLayout.NORTH);
    }

    public void play() throws LineUnavailableException {

        try {
            File soundFile = new File("/Users/joebloggs/Desktop/Java/Ensoniq-SQ-2-Electric-Piano-C4.wav"); //you could also get the sound file with an URL
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.start();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }

    }



    public void setPauseStatus(boolean b) {
        this.pauseStatus = b;

    }

    public boolean getPlayStatus() {
        return this.pauseStatus;

    }

Upvotes: 2

Views: 115

Answers (2)

Tot Zam
Tot Zam

Reputation: 8766

Here is a separate Sound class that you can add to your program. With this class, you can stop, resume, and check if the sound is on.

public class Sound {
    private Clip clip;
    private boolean on;
    private URL src;


    public Sound(String filename){
        src = getClass().getResource(filename);
        resume();
    }

    public void resume() throws LineUnavailableException, IOException, UnsupportedAudioFileException {
        clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(src));
        clip.start();
        clip.loop(Clip.LOOP_CONTINUOUSLY); //loop the clip whenever finished so never stops playing
        on = true;
    }

    public void stop() {
        if (clip.isRunning()) {
            clip.stop();
        }
        on = false;
    }

    public boolean isOn() {
        return on;
    }
}

Then change your SoundClass class to the following, keeping all the play/resume methods in the separate Sound class:

private JButton playBtn;
private Sound sound;

public SoundClass() throws UnsupportedAudioFileException, IOException {
    setLayout(new BorderLayout());
    sound = new Sound("/Users/joebloggs/Desktop/Java/Ensoniq-SQ-2-Electric-Piano-C4.wav");
    playBtn = new JButton("Play Audio");
    playBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (sound.isOn()) {
                sound.stop();
                playBtn.setText("Play Audio");
            }
            else {
                try {
                    playBtn.setText("Pause Audio");
                    sound.resume();
                }
                catch (LineUnavailableException | IOException | UnsupportedAudioFileException e1) {
                    e1.printStackTrace();
                }
            }
        }   
    });

    add(playBtn, BorderLayout.NORTH);
}

Upvotes: 0

Titus
Titus

Reputation: 22484

You can get the frame position, stop the playback and when you want to resume playing set the frame position to the location where it was when you've stopped the playback.

For example:

Pause

int pos = clip.getFramePosition();
clip.stop();

OR

int pos = clip.getMicrosecondPosition();
clip.stop();

Resume

clip.setFramePosition(pos);
clip.start();

OR

clip.setMicrosecondPosition(pos);
clip.start();

Upvotes: 1

Related Questions