Ajouts de music de fond + modification des boutons (quand on survole le bouton, sa taille grandis progressivement et la couleur change
This commit is contained in:
23
TestV2/src/fr/monkhanny/dorfromantik/utils/MusicLoader.java
Normal file
23
TestV2/src/fr/monkhanny/dorfromantik/utils/MusicLoader.java
Normal file
@@ -0,0 +1,23 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
57
TestV2/src/fr/monkhanny/dorfromantik/utils/MusicPlayer.java
Normal file
57
TestV2/src/fr/monkhanny/dorfromantik/utils/MusicPlayer.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import fr.monkhanny.dorfromantik.enums.Musics;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.FloatControl;
|
||||
|
||||
public class MusicPlayer {
|
||||
private static Clip musicClip;
|
||||
private static boolean isPlaying = false;
|
||||
|
||||
public static void loadMusic(Musics music) {
|
||||
if (music == Musics.MAIN_MENU_MUSIC) {
|
||||
musicClip = MusicLoader.loadMusic(Musics.MAIN_MENU_MUSIC.getSoundsPath());
|
||||
if (musicClip != null) {
|
||||
setVolume(Options.DEFAULT_VOLUME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void play() {
|
||||
loadMusic(Musics.MAIN_MENU_MUSIC);
|
||||
if (musicClip != null && !isPlaying) {
|
||||
musicClip.start();
|
||||
isPlaying = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void pause() {
|
||||
if (musicClip != null && isPlaying) {
|
||||
musicClip.stop();
|
||||
isPlaying = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void stop() {
|
||||
if (musicClip != null) {
|
||||
musicClip.stop();
|
||||
musicClip.setFramePosition(0);
|
||||
isPlaying = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setVolume(int volume) {
|
||||
if (musicClip != null) {
|
||||
FloatControl volumeControl = (FloatControl) musicClip.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;
|
||||
}
|
||||
}
|
23
TestV2/src/fr/monkhanny/dorfromantik/utils/SoundLoader.java
Normal file
23
TestV2/src/fr/monkhanny/dorfromantik/utils/SoundLoader.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package fr.monkhanny.dorfromantik.utils;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SoundLoader {
|
||||
|
||||
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 sound at path: " + filePath);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user