Thread java

Fermé
hammasaidi Messages postés 28 Date d'inscription lundi 1 février 2010 Statut Membre Dernière intervention 21 avril 2010 - 5 févr. 2010 à 23:20
hammasaidi Messages postés 28 Date d'inscription lundi 1 février 2010 Statut Membre Dernière intervention 21 avril 2010 - 8 févr. 2010 à 23:06
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
A voir également:

5 réponses

arth Messages postés 9374 Date d'inscription mardi 27 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2016 1 292
5 févr. 2010 à 23:49
Bonjour,

Tu as bien mis un listener sur le bouton STOP?
0
hammasaidi Messages postés 28 Date d'inscription lundi 1 février 2010 Statut Membre Dernière intervention 21 avril 2010
6 févr. 2010 à 00:12
bonjours
this.b_stop.addActionListener(this);
oui j'ai déjà mis
0
arth Messages postés 9374 Date d'inscription mardi 27 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2016 1 292
6 févr. 2010 à 00:44
Il faudrait un peu plus de code pour voir ce qui cloche.
0
hammasaidi Messages postés 28 Date d'inscription lundi 1 février 2010 Statut Membre Dernière intervention 21 avril 2010
7 févr. 2010 à 15:53
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
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
hammasaidi Messages postés 28 Date d'inscription lundi 1 février 2010 Statut Membre Dernière intervention 21 avril 2010
8 févr. 2010 à 23:06
bonjour,
aloo quelqu'un qui est là?
0