Thread java

Bonjour,
j'ai un pb avec mon thread que la fonction stop() ne fonctionne pas
voici mon code (c'es un lecteur audio )
public void actionPerformed(ActionEvent e){
Lecture t = new Lecture(); // thread

if(e.getSource().equals(this.b_ouvrir)) // bouton ouvrir
{
this.ouvrir(); //
}
if(e.getSource().equals(this.b_play)) // bouton play
{
if(this.fichier == null)
{
this.ouvrir();
this.sound();

t.start();
}
else
{

this.sound();
t.start();
}
}
if(e.getSource().equals(this.b_stop)) //bouton stop

{
this.sound();
t.stop();

}

}

lorsque je clic sur stop le thread n'arrête pas
merci pour vos aides
Configuration: Windows XP
Safari 532.5

5 réponses

  1. bonjour,
    class MyFrame extends JFrame implements ActionListener{
    protected Dimension defaultSize = new Dimension(900, 600);
    protected Framework framework = null;

    private JButton b_play;
    private JButton b_stop;
    private JButton b_ouvrir;

    private File fichier = null;
    private AudioFormat format;
    private byte[] samples;
    private JFileChooser jfc = new JFileChooser();
    public void getSamples(AudioInputStream stream)
    {
    int length = (int)(stream.getFrameLength() * format.getFrameSize());
    this.samples = new byte[length];
    DataInputStream in = new DataInputStream(stream);
    try
    {
    in.readFully(this.samples);
    }
    catch (IOException e){}
    }
    public void sound()
    {
    try
    {
    AudioInputStream stream = AudioSystem.getAudioInputStream(this.fichier);
    this.format = stream.getFormat();
    this.getSamples(stream);
    }
    catch (Exception e){}
    }

    public byte[] getSamples()
    {
    return this.samples;
    }
    public void play(InputStream source)
    {
    // 100 ms buffer for real time change to the sound stream
    int bufferSize = this.format.getFrameSize() * Math.round(this.format.getSampleRate() / 10);
    byte[] buffer = new byte[bufferSize];
    SourceDataLine line;
    try
    {
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, this.format);
    line = (SourceDataLine)AudioSystem.getLine(info);
    line.open(this.format, bufferSize);
    line.start();
    int numBytesRead = 0;
    while (numBytesRead != -1)
    {
    numBytesRead = source.read(buffer, 0, buffer.length);
    if (numBytesRead != -1)
    line.write(buffer, 0, numBytesRead);
    }
    line.drain();
    line.close();
    }
    catch (Exception e){}
    }
    public void ouvrir()
    {
    this.jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    this.jfc.addChoosableFileFilter(new FiltreExtension(".wav", "Fichier wav"));
    this.jfc.addChoosableFileFilter(new FiltreExtension(".wav", "Fichier wav"));
    int returnVal = this.jfc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
    this.fichier = this.jfc.getSelectedFile();
    }
    }
    private class Lecture
    extends Thread
    {
    public void run()
    {
    play(new ByteArrayInputStream(getSamples()));
    }
    }

    MyFrame()
    {
    ..... creation de frame
    b_play = new JButton("Play");
    p2.add(b_play);
    this.b_play.addActionListener(this);
    b_stop = new JButton("Stop");
    p2.add(b_stop);
    this.b_stop.addActionListener(this);
    b_ouvrir = new JButton("Ouvrir");
    p2.add(b_ouvrir);

    this.b_ouvrir.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
    Lecture t = new Lecture();

    if(e.getSource().equals(this.b_ouvrir))
    {
    this.ouvrir();
    }
    if(e.getSource().equals(this.b_play))
    {
    if(this.fichier == null)
    {
    this.ouvrir();
    this.sound();

    t.start();
    }
    else
    {

    this.sound();
    t.start();
    }
    }
    if(e.getSource().equals(this.b_stop))

    {
    this.sound();
    t.stop();

    }

    }
    public static void main(String args[]) {
    new MyFrame();
    }
    }
    //le bouton stop ne fonctionne pas et si c'es possibel d'ajouter une bouton pause
    merci pour vos aides
    1. Contributeur
      Il faudrait un peu plus de code pour voir ce qui cloche.
      1. bonjours
        this.b_stop.addActionListener(this);
        oui j'ai déjà mis
        1. Contributeur
          Bonjour,

          Tu as bien mis un listener sur le bouton STOP?