tags:

views:

76

answers:

1

Hi everyone , can anybody tell me why the following code doesn't work properly? I want to play and stop an audio file. I can do the playback but whenever I click the stop button nothing happens. Here's the code : Thank you.

..................

import java.io.*;
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.event.*;

public class SoundClipTest extends JFrame {

    final JButton button1 = new JButton("Play");
    final JButton button2 = new JButton("Stop");
    int stopPlayback = 0;

    // Constructor
    public SoundClipTest() {

        button1.setEnabled(true);
        button2.setEnabled(false);

        // button play
        button1.addActionListener(
            new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    button1.setEnabled(false);
                    button2.setEnabled(true);

                    play();
                }// end actionPerformed
            }// end ActionListener
            );// end addActionListener()          

        // button stop
        button2.addActionListener(
            new ActionListener() {

                public void actionPerformed(
                    ActionEvent e) {
                    //Terminate playback before EOF

                    stopPlayback = 1;

                }//end actionPerformed
            }//end ActionListener
            );//end addActionListener()

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Test Sound Clip");
        this.setSize(300, 200);
        JToolBar bar = new JToolBar();
        bar.add(button1);
        bar.add(button2);
        bar.setOrientation(JToolBar.VERTICAL);
        add("North", bar);
        add("West", bar);
        setVisible(true);
    }

    void play() {

        try {

            final File inputAudio = new File("first.wav");
            // First, we get the format of the input file
            final AudioFileFormat.Type fileType =
                AudioSystem.getAudioFileFormat(inputAudio).getType();
            // Then, we get a clip for playing the audio.
            final Clip c = AudioSystem.getClip();
            // We get a stream for playing the input file.
            AudioInputStream ais = AudioSystem.getAudioInputStream(inputAudio);
            // We use the clip to open (but not start) the input stream
            c.open(ais);
            // We get the format of the audio codec
            // (not the file format we got above)
            final AudioFormat audioFormat = ais.getFormat();

            c.start();
            if (stopPlayback == 1) {
                c.stop();
            }

        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }// end play

    public static void main(String[] args) {
        //new SoundClipTest().play();
        new SoundClipTest();
    }
}
+1  A: 

your if (stopPlayback == 1 ) will only be run once -- you'll have to enclose it in a while(true) loop to make sure it keeps being evaluated, make sure you add a pause as well otherwise you will burn a lot of unnecessary cycles.

Update: I was assuming you ran a second thread to watch on the stopPlayback value - I now see this is not the case. Why don't you just call the c.stop() from your ActionListener?

Simon Groenewolt
If I call the c.stop(); from the stop ActionListener the compiler says it cannot find symbol ...How can I solve the problem?
avoq
+1 @avoq: Make `Clip c` an instance variable and set it in `play()`: `c = AudioSystem.getClip()`
trashgod
move c to class-level instead of defining it inside a method (and choose a more descriptive name than 'c')
Simon Groenewolt
@avoq the Clip is only defined in your function, place the definition below the int stopPlayback line. Explanation: every pair of{} defines a scope, variables are only valid within the scope they where defined in, Clip c is defined within the scope of the play function, stopPlayback is defined within the scope of the class.
josefx