Ajout d'effet sonnore dans le menu principal)
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MusicLoader {
|
||||
|
||||
public static Clip loadMusic(String filePath) {
|
||||
try {
|
||||
File musicFile = new File(filePath);
|
||||
AudioInputStream audioStream = AudioSystem.getAudioInputStream(musicFile);
|
||||
Clip clip = AudioSystem.getClip();
|
||||
clip.open(audioStream);
|
||||
return clip;
|
||||
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Failed to load music at path: " + filePath);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,57 +1,86 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Musics;
|
||||
import fr.monkhanny.dorfromantik.enums.Sounds;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.LineEvent;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
|
||||
public class MusicPlayer {
|
||||
private static Clip musicClip;
|
||||
private static boolean isPlaying = false;
|
||||
private static Clip soundClip; // Clip séparé pour les bruitages
|
||||
private static boolean isPlayingMusic = false;
|
||||
private static boolean isPlayingSound = false;
|
||||
|
||||
public static void loadMusic(Musics music) {
|
||||
if (music == Musics.MAIN_MENU_MUSIC) {
|
||||
musicClip = MusicLoader.loadMusic(Musics.MAIN_MENU_MUSIC.getSoundsPath());
|
||||
musicClip = SoundLoader.loadMusic(Musics.MAIN_MENU_MUSIC.getSoundsPath());
|
||||
if (musicClip != null) {
|
||||
setVolume(Options.DEFAULT_VOLUME);
|
||||
setVolume(musicClip, Options.DEFAULT_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void play() {
|
||||
loadMusic(Musics.MAIN_MENU_MUSIC);
|
||||
if (musicClip != null && !isPlaying) {
|
||||
public static void loadSound(Sounds sound) {
|
||||
if (sound == Sounds.SOUNDS1) {
|
||||
soundClip = SoundLoader.loadMusic(Sounds.SOUNDS1.getSoundsPath()); // Utilise soundClip pour les bruitages
|
||||
if (soundClip != null) {
|
||||
setVolume(soundClip, Options.DEFAULT_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void playMusic() {
|
||||
if (musicClip != null && !isPlayingMusic) {
|
||||
musicClip.start();
|
||||
isPlaying = true;
|
||||
isPlayingMusic = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void pause() {
|
||||
if (musicClip != null && isPlaying) {
|
||||
public static void playSound() {
|
||||
if (soundClip != null && !isPlayingSound) {
|
||||
soundClip.start();
|
||||
isPlayingSound = true;
|
||||
soundClip.addLineListener(event -> { // Réinitialiser isPlayingSound à la fin du son
|
||||
if (event.getType() == LineEvent.Type.STOP) {
|
||||
soundClip.setFramePosition(0); // Retour au début du son pour rejouer si nécessaire
|
||||
isPlayingSound = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static void pauseMusic() {
|
||||
if (musicClip != null && isPlayingMusic) {
|
||||
musicClip.stop();
|
||||
isPlaying = false;
|
||||
isPlayingMusic = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void stop() {
|
||||
public static void stopMusic() {
|
||||
if (musicClip != null) {
|
||||
musicClip.stop();
|
||||
musicClip.setFramePosition(0);
|
||||
isPlaying = false;
|
||||
isPlayingMusic = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setVolume(int volume) {
|
||||
if (musicClip != null) {
|
||||
FloatControl volumeControl = (FloatControl) musicClip.getControl(FloatControl.Type.MASTER_GAIN);
|
||||
public static void setVolume(Clip clip, int volume) {
|
||||
if (clip != null) {
|
||||
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
|
||||
float range = volumeControl.getMaximum() - volumeControl.getMinimum();
|
||||
float gain = (range * volume / 100f) + volumeControl.getMinimum();
|
||||
volumeControl.setValue(gain);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPlaying() {
|
||||
return isPlaying;
|
||||
public static boolean isPlayingMusic() {
|
||||
return isPlayingMusic;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPlayingSound() {
|
||||
return isPlayingSound;
|
||||
}
|
||||
}
|
||||
|
@@ -8,8 +8,8 @@ public class SoundLoader {
|
||||
|
||||
public static Clip loadMusic(String filePath) {
|
||||
try {
|
||||
File musicFile = new File(filePath);
|
||||
AudioInputStream audioStream = AudioSystem.getAudioInputStream(musicFile);
|
||||
File soundFile = new File(filePath);
|
||||
AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundFile);
|
||||
Clip clip = AudioSystem.getClip();
|
||||
clip.open(audioStream);
|
||||
return clip;
|
||||
|
Reference in New Issue
Block a user