Bonjour à tous,
Je viens vers vous car après plusieurs tentatives, je n'arrive pas à lire un fichier wave quand j’exécute mon .jar.
Tout d'abord voici le code que j'utilise pour lire du son :
et pour le jouer j'utilise
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; /** * Handles playing, stoping, and looping of sounds for the game. * @author Tyler Thomas * */ public class Sound { private Clip clip; public Sound(String fileName) { // specify the sound to play // (assuming the sound can be played by the audio system) // from a wave File try { File file = new File(fileName); if (file.exists()) { AudioInputStream sound = AudioSystem.getAudioInputStream(file); // load the sound into memory (a Clip) clip = AudioSystem.getClip(); clip.open(sound); } else { throw new RuntimeException("Sound: file not found: " + fileName); } } catch (MalformedURLException e) { e.printStackTrace(); throw new RuntimeException("Sound: Malformed URL: " + e); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); throw new RuntimeException("Sound: Unsupported Audio File: " + e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Sound: Input/Output Error: " + e); } catch (LineUnavailableException e) { e.printStackTrace(); throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e); } // play, stop, loop the sound clip } public void play(){ clip.setFramePosition(0); // Must always rewind! clip.start(); } public void loop(){ clip.loop(Clip.LOOP_CONTINUOUSLY); } public void stop(){ clip.stop(); } }
Maintenant voici mon problème.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 sound = new Sound(getClass().getResource("/ressources/Tetris_song.wav").getPath()); sound.loop();
Je suis sous eclipse et quand je lance mon programme j'ai bien du son.
Par contre quand je crée un jar impossible d'avoir du son. Il dit "java.lang.RuntimeException Sound: file not found ..."
Après quelques recherche j'ai vérifier plusieurs choses.
Dans le fichier jar j'ai bien le fichier audio. De plus j'ai aussi des images dans le même fichier que l'audio et pas de problème pour les ouvrir alors que j'utilise getClass().getResource("/ressources/Image.gif").
Je ne sais pas d’où viens l'erreur
J'espère que vous pourrez m'aider
Cordialement
Guillaume
Partager