Capture JTextField value and play .wav

0

[EDIT] Binder: I need to capture the value of the JTextField to play any song of this program. For the moment, when I click on the Play button, it plays the song that I put there, what I want to do is give the name of a song to the JTextField (which has saved) and then reproduce it, but I can not.

I would appreciate if you help me.

CODE:

import java.io.File;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

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 panel1, panel2;
    private JTextField tfCancion;

    public Audio3() throws Exception 
    {
        super("Audio TEC");
        tfCancion = new JTextField();
        bPlay = new JButton("Play");
        bStop = new JButton("Stop");
        panel1 = new JPanel();
        panel2= new JPanel();


        panel1.setLayout(new GridLayout(2,2));
        panel2.setLayout(new FlowLayout());

        panel1.add(new JLabel("Song: "));
        panel1.add(tfCancion);
        panel1.add(bPlay);
        panel1.add(bStop);
        panel2.add(panel1);
        add(panel2);

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

        reproducir();



    }


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

        tfCancion= new JTextField();
        bPlay = new JButton("Reproducir");
        bStop = new JButton("Stop");
        panel1 = new JPanel();
        panel2 = new JPanel();

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

        panel1.setLayout(new GridLayout(2,1));
        panel2.setLayout(new FlowLayout());
        panel1.add(new JLabel("Cancion"));
        panel1.add(tfCancion);
        panel1.add(bPlay);
        panel1.add(bStop);

        panel2.add(panel1);
        add(panel2);


        reproducir(song);

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

    } 




    private void reproducir() throws Exception 
    {
        // 1. Obtener el nombre de la cancion a reproducir
        String song =("Hotel California.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);
    //  while(true)
    //      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();
        Audio3 audio = new Audio3("Hotel California.wav");
    }
}
    
asked by Carlos Andrés Cruz Pedraza 15.02.2018 в 02:01
source

1 answer

1

Your code is not well organized, you have a lot of duplicate code, I will not go into details of how you can improve it, so I recommend checking it, you can guide this presentation to understand what I mean.

As for the code, modify your actionPerformed to call your play method, like this:

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == bPlay)
    {
        //clip.start();
        try {
            reproducir(tfCancion.getText());
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    else if(e.getSource() == bStop)
    { //hacer stop
        clip.stop();
    }
}

[Original answer] If your JTextField stores the file path (as I am assuming), you must replace this line

String song =("Hotel California.wav");

By this line:

String song = tfCancion.getText();

The documentation of the JTextComponent.getText () method for reference.

[Edition] It is probably more convenient for the user to allow selecting the file, you can use the following as a base:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "Archivos WAV", "wav");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this); //this se referiría al JFrame principal
if(returnVal == JFileChooser.APPROVE_OPTION) {
        String ruta = chooser.getSelectedFile().getPath();
        //usar la ruta
        tfCancion.setText(ruta);
}

The above code uses, among other things, a JFileChooser , a more or less complete tutorial on how to use this component, you can find it in the following link .

    
answered by 15.02.2018 в 02:09