import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import javazoom.jl.player.*;
import javax.swing.filechooser.*;
import javax.swing.text.*;
public class MusicPlayer implements ActionListener
{
JFrame frame;
JPanel panel;
Dimension dim;
JTextArea main;
ArrayList music;
int pause;
mp player;
public MusicPlayer()
{
frame = new JFrame();
panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds(dim.width/2-250,dim.height/2-250,500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
music = new ArrayList();
initGui();
frame.setVisible(true);
}
public void initGui()
{
main = new JTextArea();
main.setEditable(false);
DefaultCaret caret = (DefaultCaret)main.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JScrollPane jsp = new JScrollPane(main);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setBounds(0,0,250,250);
panel.add(jsp);
Box buttons = new Box(BoxLayout.Y_AXIS);
JButton play = new JButton("Play");
play.addActionListener(this);
buttons.add(play);
JButton pause = new JButton("Pause");
pause.addActionListener(this);
buttons.add(pause);
JButton load = new JButton("Load");
load.addActionListener(this);
buttons.add(load);
buttons.setBounds(250,0,250,250);
panel.add(buttons);
}
public static void main(String args[])
{
new MusicPlayer();
}
public void actionPerformed(ActionEvent e)
{
String a = e.getActionCommand();
if(a=="Load")
{
JFileChooser jfc = new JFileChooser();
jfc.setMultiSelectionEnabled(true);
jfc.setFileFilter(new FileNameExtensionFilter("MP3","mp3"));
jfc.showOpenDialog(frame);
File file = jfc.getSelectedFile();
music.add(file.getPath());
main.append(file.getName());
panel.repaint();
}
else if(a=="Play")
{
if(pause==0)
{
String dir = (String)music.get(0);
player = new mp(dir);
}
else
{
player = new mp(pause);
}
}
else if(a=="Pause")
{
player.close();
pause = player.getPos();
}
}
}
class mp implements Runnable
{
Player ap;
Thread thread;
boolean isRun;
String song;
int u = Integer.MAX_VALUE;
int a = 0;
public mp(String song)
{
isRun = true;
this.song = song;
thread = new Thread(this);
thread.start();
}
public mp(int i)
{
isRun = true;
this.song = song;
u = i;
thread = new Thread(this);
thread.start();
}
public void run()
{
while(isRun==true)
{
try
{
FileInputStream fis = new FileInputStream(song);
ap = new Player(fis);
ap.play(u);
}
catch(Exception ex)
{
}
}
}
public int getPos()
{
return a;
}
public void close()
{
a = Integer.MAX_VALUE-ap.getPosition();
ap.close();
isRun = false;
thread = null;
}
}