Play and stop button for .wav file

1

I have this code:

import java.io.File;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.FlowLayout;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Audio3 extends JFrame implements ActionListener
{
    private File songFile;
    private AudioInputStream audioStream;
    private Clip clip;
    private JButton bPlay, bStop;
    private JPanel panel, panel2;

    public Audio3() throws Exception 
    {
        super("Audio");

        bPlay = new JButton("Reproducir");
        bStop = new JButton("Stop");
        panel = new JPanel();

        setLayout(new GridLayout(2,2));


        add(bPlay);
        add(bStop);

        reproducir();

        setSize(400,300);
        setVisible(true);

    }


    public Audio3(String song) throws Exception 
    {
        super("Audio TEC");

        bPlay = new JButton("Reproducir");
        bStop = new JButton("Stop");
        panel = new JPanel();

        bPlay.addActionListener(this);
        bStop.addActionListener(this);

        setLayout(new GridLayout(1,1));
        setLayout(new FlowLayout());


        add(bPlay);
        add(bStop);

        reproducir(song);

        setSize(400,300);
        setVisible(true);

    }

    private void reproducir() throws Exception 
    {
        // 1. Obtener el nombre de la cancion a reproducir
        String song = "Last_Nite.wav";

        // 2. Relacionar el nombre de la cancion con un Archivo (File) wav
        songFile = new File(song);
        // 3. Preparar el Audio Stream del archivo de la cancion 
        audioStream = AudioSystem.getAudioInputStream(songFile);

        // 4. Reproducir

        clip = AudioSystem.getClip();
        clip.open(audioStream);
    //  while(true)
            clip.start();
    }

    private void reproducir(String song) throws Exception 
    {
        // 1. Obtener el nombre de la cancion a reproducir
    //  String song = "Last_Nite.wav";

        // 2. Relacionar el nombre de la cancion con un Archivo (File) wav
        songFile = new File(song);
        // 3. Preparar el Audio Stream del archivo de la cancion 
        audioStream = AudioSystem.getAudioInputStream(songFile);

        // 4. Reproducir

        clip = AudioSystem.getClip();
        clip.open(audioStream);

            clip.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == bPlay)
        {
            clip.start();
        }
        if(e.getSource() == bStop)
        {
            clip.stop();
        }
    }

    public static void main(String args[]) throws Exception
    {
        Audio3 audio = new Audio3("Last_Nite.wav");

    }
}

What I want to do is look for another song by writing the name (.wav) that I have saved and play it, but I get this error:

pd. I already made the buttons work, I just need the above: D

Greetings.

    
asked by Carlos Andrés Cruz Pedraza 14.02.2018 в 18:10
source

1 answer

1

You have these lines:

 bPlay.addActionListener(this);
 bStop.addActionListener(this);

If you want your Audio3 object to "hear" the events of pressing the buttons, you must implement the class ActionListener :

public class Audio3 extends JFrame implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) { reproducir(); }

}
    
answered by 14.02.2018 / 18:16
source