Bonjour,
quand je clique sur play l'interface graphique se plante et la lecture marche de mp3
comment controler le son avec jslider et faire que le bouton play change en pause
j utilise les biliotheque importee de JLayer1.0.1
svp aider moi :(
jlp.java
package mm;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.Player;
public class jlp
{
private String fFilename = null;
private boolean remote = false;
public void stop()
throws JavaLayerException
{
try
{
InputStream in = null;
if (remote == true)
try {
in = getURLInputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else in = getInputStream();
AudioDevice dev = getAudioDevice();
Player player = new Player(in, dev);
player.close();
}
catch (IOException ex) {}
}
public void play(String filename)
throws JavaLayerException
{ fFilename = filename;
try
{
System.out.println("playing "+fFilename+"...");
InputStream in = null;
if (remote == true) in = getURLInputStream();
else in = getInputStream();
AudioDevice dev = getAudioDevice();
Player player = new Player(in, dev);
player.play();
}
catch (IOException ex)
{
throw new JavaLayerException("Problem playing file "+fFilename, ex);
}
catch (Exception ex)
{
throw new JavaLayerException("Problem playing file "+fFilename, ex);
}
}
/**
* Playing file from URL (Streaming).
*/
protected InputStream getURLInputStream()
throws Exception
{
URL url = new URL(fFilename);
InputStream fin = url.openStream();
BufferedInputStream bin = new BufferedInputStream(fin);
return bin;
}
/**
* Playing file from FileInputStream.
*/
protected InputStream getInputStream()
throws IOException
{
FileInputStream fin = new FileInputStream(fFilename);
BufferedInputStream bin = new BufferedInputStream(fin);
return bin;
}
protected AudioDevice getAudioDevice()
throws JavaLayerException
{
return FactoryRegistry.systemRegistry().createAudioDevice();
}
}
NewJFrame.java
package mm;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javazoom.jl.decoder.JavaLayerException;
public class NewJFrame {
static JTextField jTextField1 = new JTextField();
static String s="C:/Clocks.mp3";
public static void main(String[] args)throws Exception{
JFrame f = new JFrame();
Ecouteur ec = new Ecouteur();
f.setSize(350, 250);
f.setTitle("mp3 player");
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JSlider jSlider1 = new JSlider();
File name = new File(s);
jTextField1.setText(name.getName());
jButton1.addActionListener(ec);
jButton2.addActionListener(ec);
//jSlider1.addMouseListener((MouseListener) ec);
jTextField1.addActionListener(ec);
jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jButton1.setText("play");
jButton2.setFont(new java.awt.Font("Tahoma", 0, 14));
jButton2.setText("stop");
jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(f.getContentPane());
f.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(50, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(33, 33, 33)
.addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(48, Short.MAX_VALUE))
);
jTextField1.setEditable(false);
jTextField1.setBackground(new java.awt.Color(255, 255, 255));
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class Ecouteur implements ActionListener{
public void actionPerformed(ActionEvent e){
jlp j=new jlp();
if(e.getActionCommand().equals("play"))
{
try {
j.play(NewJFrame.s);
} catch (JavaLayerException e1) {
e1.printStackTrace();
}
}
if(e.getActionCommand().equals("stop"))
{
try { NewJFrame.jTextField1.setText("sdddddd");
j.stop();
} catch (JavaLayerException e1) {
e1.printStackTrace();
}
}
}
}