
The ActionListeners here are not running concurrently...
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javazoom.jl.player.Player;
import java.io.FileInputStream;
public class Play2 extends JFrame
{
JButton b,b1;
JTextField t;
Play2()
{
JFrame j=new JFrame("MusicPlayer");
j.setSize(300,300);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
b=new JButton("Play");
b1=new JButton("Stop");
JPanel p=new JPanel();
t=new JTextField(20);
p.add(t);
p.add(b);
p.add(b1);
j.add(p);
/**try {
j.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("jesus.jpg")))));
}
catch (IOException e) {
e.printStackTrace();
}
j.getContentPane().setOpaque(false);*/
j.setVisible(true);
}
public void awt()
{
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
try
{
String fname=t.getText();
File directory = new File(fname);
boolean isDirectory = directory.isDirectory();
if (isDirectory)
{
// It returns true if directory is a directory.
System.out.println("the name you have entered is a directory : " + directory);
//It returns the absolutepath of a directory.
System.out.println("the path is " + directory.getAbsolutePath());
}
else
{
// It returns false if directory is a file.
System.out.println("the name you have entered is a file : " + directory);
//It returns the absolute path of a file.
System.out.println("the path is " + directory.getAbsolutePath());
}
String s=directory.getAbsolutePath();
s=s.replace("\\","/") ;
FileInputStream fis=new FileInputStream(s);
final Player playMp3=new Player(fis);
playMp3.play();
}
catch(Exception e){
System.out.println(e);}
}//end actionPerformed
}//end ActionListener
);//end addActionListener()
b1.addActionListener(
new ActionListener(){
public void actionPerformed(
ActionEvent ae){
//Terminate playback before EOF
try
{
String fname=t.getText();
File directory = new File(fname);
boolean isDirectory = directory.isDirectory();
if (isDirectory)
{
// It returns true if directory is a directory.
System.out.println("the name you have entered is a directory : " + directory);
//It returns the absolutepath of a directory.
System.out.println("the path is " + directory.getAbsolutePath());
}
else
{
// It returns false if directory is a file.
System.out.println("the name you have entered is a file : " + directory);
//It returns the absolute path of a file.
System.out.println("the path is " + directory.getAbsolutePath());
}
String s=directory.getAbsolutePath();
s=s.replace("\\","/") ;
FileInputStream fis=new FileInputStream(s);
final Player playMp3=new Player(fis);
playMp3.close();
}
catch(Exception e){
System.out.println(e);}
}//end actionPerformed
}//end ActionListener
);//end addActionListener()
}
public static void main(String[]args)
{
Play2 f=new Play2();
f.awt();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.