PDA

View Full Version : [JAVA] Player MP3


al3000
03-06-2008, 18:46
salve ho creato questo semplice player ma non riesco a metterlo in pausa, elenco di seguito il codice: package player;
import javax.media.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import java.awt.*;
import java.awt.event.*;
public class Main{
public static void main(String[] args){
PL p=new PL();
p.setSize(300,300);
p.setVisible(true);
p.pack();
}
}
class PL extends Frame implements ActionListener,ChangeListener,WindowListener{
private JButton Play,Stop,Muto,Open;
private Panel p1,p;
private PlayerThread pt;
private boolean statoMute;
private File aperto;
private JSlider slider;
private static final int VOL_MIN=0;
private static final int VOL_INIT=50;
private static final int VOL_MAX=100;
public PL(){
super("Audio Player");
p=new Panel();
aperto=null;
slider=new JSlider(JSlider.HORIZONTAL,VOL_MIN,VOL_MAX,VOL_INIT);
p.add(new Label("Audio Player"));
p1=new Panel();
Play=new JButton("Play");
Stop=new JButton("Stop");
Open=new JButton("^");
Play.addActionListener(this);
Stop.addActionListener(this);
Open.addActionListener(this);
slider.addChangeListener(this);
addWindowListener(this);
Muto=new JButton("Mute");
Muto.addActionListener(this);
setLayout(new GridLayout(2,1));
p1.add(Open);
p1.add(Play);
p1.add(Stop);
p1.add(slider);
p1.add(Muto);
pt=null;
statoMute=false;
add(p);
add(p1);
}
public void actionPerformed(ActionEvent e) {
String nomeBottone=e.getActionCommand();
if(nomeBottone.equals("^"))
aperto=OpenFile();
if(nomeBottone.equals("Play")){
if(aperto!=null){
pt.playerPause();
}
else{
aperto=OpenFile();
pt=new PlayerThread(aperto);
pt.start();
}

}
if(nomeBottone.equals("Stop")&&pt!=null){
pt.playerStop();
}
if(nomeBottone.equals("Mute")){
statoMute=!statoMute;
pt.playerMute(statoMute);
}
}
public File OpenFile(){
FileDialog fd=new FileDialog(this,"File",FileDialog.LOAD);
fd.setVisible(true);
File f = new File (fd.getDirectory(), fd.getFile());
fd.dispose();
return f;
}

public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
pt.playerVolume((float)source.getValue());
}
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
pt.playerStop();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
class PlayerThread extends Thread{
private Player p;
private File file;
PlayerThread(File file){
this.file=file;
p=null;
}
void playerStart(){
try {
p = Manager.createPlayer(file.toURL());
p.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
void playerPause(){
try{
if(playerState())
synchronized(p){
p.wait();
}
else
synchronized(p){
p.notifyAll();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
boolean playerState(){
if(p.getState()==600)
return true;
else
return false;
}
void playerStop(){
p.stop();
p.close();
}
void playerMute(boolean stato){
p.getGainControl().setMute(stato);
}
void playerVolume(float level){
p.getGainControl().setLevel(level/100);
}
public void run(){
playerStart();
}
}
se si preme il tasto play dopo che il player é partito l'intero programma si blocca ma la riproduzione continua.
potete aiutarmi ?

al3000
04-06-2008, 15:47
UP