I have a Java application, with 2 buttons : Start and Stop.
When I click on the Start button, I read an audio file. But there is a problem. When I read the music, it is impossible to click on the other button. I lost the control of my application until the entire audio file is read. So I can't stop the reading by clicking my button.
I think I have to use a thread, but I have some difficulties to understand how thread are working.
Can you help me please ?
Here is my ServerBoard :
public class ServerBoard extends JFrame
{
private TCPServer tcpserver;
private Sound sound;
private JTextArea messagearea;
private JButton startserver;
private JButton playmusic;
private JButton stopmusic;
/** CONSTRUCTEUR **/
public ServerBoard(int l, int h)
{
...
}
/** Initialisation de la fenetre **/
public void initialize()
{
...
}
/**********************************************************************************************
************************************** ADD PANELS **************************************
**********************************************************************************************/
private JPanel createPanelEast()
{
...
return panelEast;
}
/**********************************************************************************************
************************************* ADD LISTENERS ************************************
**********************************************************************************************/
private class ServerBoardListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String namebutton=e.getActionCommand().trim();
if(namebutton.equals("START SERVER"))
{
startServer();
}
else if(namebutton.equals("PLAY MUSIC"))
{
playMusic();
}
else if(namebutton.equals("STOP MUSIC"))
{
stopMusic();
}
}
}
/**********************************************************************************************
****************************************** METHODS ******************************************
**********************************************************************************************/
// METHODE POUR DEMARRER LE SERVEUR
public void startServer()
{
// Une fois clique, on desactive le bouton
startserver.setEnabled(false);
// On cree l'objet OnMessageReceved demande par le constructeur de TCPServer
tcpserver=new TCPServer(new TCPServer.OnMessageReceived()
{
public void messageReceived(String message)
{
messagearea.append(message);
}
});
tcpserver.start();
}
// METHODE POUR LIRE UN FICHIER AUDIO
public void playMusic()
{
System.out.println("lecture de son");
try
{
Sound sound = new Sound("C:/Documents and Settings/cngo/Bureau/Stage-Save/TCPIP_AndroidJava/TCPIP_V6_Sound/OpeningSuite.mp3");
System.out.println("playing : " + sound.isPlaying());
sound.play();
System.out.println("playing : " + sound.isPlaying());
}
catch (Exception e){e.printStackTrace();}
}
// STOP LA MUSIQUE
public void stopMusic()
{
System.out.println("arret de la musique");
try
{
sound.stop();
}
catch (Exception e) {e.printStackTrace();}
}
}
Here is my Sound.class :
public class Sound
{
private boolean isPlaying = false;
private AdvancedPlayer player = null;
public Sound(String path) throws Exception
{
InputStream in = (InputStream)new BufferedInputStream(new FileInputStream(new File(path)));
player = new AdvancedPlayer(in);
}
public boolean isPlaying()
{
return isPlaying;
}
public void play() throws Exception
{
if (player != null)
{
isPlaying = true;
player.play();
}
}
public Sound(String path,PlaybackListener listener) throws Exception
{
InputStream in = (InputStream)new BufferedInputStream(new FileInputStream(new File(path)));
player = new AdvancedPlayer(in);
player.setPlayBackListener(listener);
}
public void play(int begin,int end) throws Exception
{
if (player != null)
{
isPlaying = true;
player.play(begin,end);
}
}
public void stop() throws Exception
{
if (player != null)
{
player.stop();
isPlaying = false;
}
}
}
Thanks to you in advance for your help.