2024-04-07 17:14:17 +02:00
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
/**
|
2024-04-29 20:06:02 +02:00
|
|
|
* Fournit un bouton qui bascule entre la lecture et l'arrêt de la musique lorsqu'il est cliqué.
|
2024-04-07 17:14:17 +02:00
|
|
|
* @version 1.0
|
|
|
|
* @author Moncef STITI
|
|
|
|
* @author Marco ORFAO
|
|
|
|
*/
|
2024-04-29 20:06:02 +02:00
|
|
|
public class MusicButton extends JButton {
|
2024-04-09 23:28:32 +02:00
|
|
|
private static MusicPlayer currentMusicPlayer;
|
2024-04-07 17:14:17 +02:00
|
|
|
private ImageIcon iconOn;
|
|
|
|
private ImageIcon iconOff;
|
|
|
|
private MusicPlayer musicPlayer;
|
|
|
|
|
|
|
|
/**
|
2024-04-29 20:06:02 +02:00
|
|
|
* Constructeur : Construit un MusicButton.
|
|
|
|
* @param onIconPath Le chemin du fichier d'icône lorsque la musique est activée.
|
|
|
|
* @param offIconPath Le chemin du fichier d'icône lorsque la musique est désactivée.
|
|
|
|
* @param musicFilePath Le chemin du fichier musical à jouer.
|
2024-04-07 17:14:17 +02:00
|
|
|
*/
|
|
|
|
public MusicButton(String onIconPath, String offIconPath, String musicFilePath) {
|
|
|
|
|
2024-04-08 14:43:35 +02:00
|
|
|
this.iconOn = new ImageIcon(onIconPath);
|
|
|
|
this.iconOff = new ImageIcon(offIconPath);
|
|
|
|
setIcon(this.iconOff);
|
2024-04-09 23:28:32 +02:00
|
|
|
|
|
|
|
// Vérifie s'il y a déjà une musique en cours de lecture et l'arrête si nécessaire
|
|
|
|
if (currentMusicPlayer != null && currentMusicPlayer.isPlaying()) {
|
|
|
|
currentMusicPlayer.stop();
|
|
|
|
currentMusicPlayer = null;
|
|
|
|
}
|
|
|
|
|
2024-04-08 14:43:35 +02:00
|
|
|
this.musicPlayer = new MusicPlayer(musicFilePath);
|
2024-04-07 17:14:17 +02:00
|
|
|
|
|
|
|
addActionListener(e -> {
|
2024-04-09 23:28:32 +02:00
|
|
|
if (currentMusicPlayer != null && currentMusicPlayer.isPlaying()) {
|
|
|
|
currentMusicPlayer.stop();
|
|
|
|
currentMusicPlayer = null;
|
2024-04-08 14:43:35 +02:00
|
|
|
setIcon(this.iconOff);
|
2024-04-07 17:14:17 +02:00
|
|
|
} else {
|
2024-04-08 14:43:35 +02:00
|
|
|
this.musicPlayer.play();
|
|
|
|
setIcon(this.iconOn);
|
2024-04-09 23:28:32 +02:00
|
|
|
currentMusicPlayer = this.musicPlayer;
|
2024-04-07 17:14:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|