2024-12-02 20:51:28 +01:00
|
|
|
package fr.monkhanny.dorfromantik.utils;
|
|
|
|
|
|
|
|
import javax.sound.sampled.*;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2024-12-06 22:56:22 +01:00
|
|
|
/**
|
|
|
|
* Classe utilitaire pour charger et lire des fichiers audio.
|
|
|
|
*
|
|
|
|
* Cette classe fournit une méthode statique pour charger un fichier audio
|
|
|
|
* et renvoyer un objet `Clip` qui peut être utilisé pour lire le son.
|
|
|
|
*
|
|
|
|
* @author Moncef STITI
|
|
|
|
* @version 1.0
|
|
|
|
*/
|
2024-12-02 20:51:28 +01:00
|
|
|
public class SoundLoader {
|
|
|
|
|
2024-12-06 22:56:22 +01:00
|
|
|
/**
|
|
|
|
* Charge un fichier audio à partir du chemin spécifié.
|
|
|
|
*
|
|
|
|
* @param filePath Chemin du fichier audio à charger
|
|
|
|
* @return Clip Objet `Clip` qui peut être utilisé pour lire le son
|
|
|
|
*/
|
2024-12-02 20:51:28 +01:00
|
|
|
public static Clip loadMusic(String filePath) {
|
|
|
|
try {
|
2024-12-06 22:56:22 +01:00
|
|
|
// Charger le fichier audio
|
2024-12-02 20:51:28 +01:00
|
|
|
File soundFile = new File(filePath);
|
2024-12-06 22:56:22 +01:00
|
|
|
// Créer un flux audio à partir du fichier et l'ouvrir
|
2024-12-02 20:51:28 +01:00
|
|
|
AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundFile);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|