[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");
}
}