Bonjour,
Je suis entrain de faire un petit lecteur audio wav. (Faut bien commencer quelque part)
Mais j'ai une fuite de mémoire à cette endroit :
Mais "stream.getFrameLength() * format.getFrameSize()" ne fait que 9MO avec la chanson utlisée :/
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 int length = (int) (stream.getFrameLength() * format.getFrameSize()); samples = new byte[length];
L'erreur est : "Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space"
Voici le code complet qui vous intéresse :
Pourquoi est-ce qu'il y a une fuite ?
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 public SoundJava(String filePath) { try { File file = new File(formatString(filePath)); AudioInputStream stream = AudioSystem.getAudioInputStream(file); format = stream.getFormat(); samples = getSamples(stream); } catch (Exception e) { } } public byte[] getSamples() { return samples; } public byte[] getSamples(AudioInputStream stream) { int length = (int) (stream.getFrameLength() * format.getFrameSize()); System.out.println( (stream.getFrameLength() * format.getFrameSize())/8 /1000000 ); samples = new byte[length]; DataInputStream in = new DataInputStream(stream); try { in.readFully(samples); } catch (Exception e) { } return samples; } public void play(InputStream source2) { source = source2; int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10); buffer = new byte[bufferSize]; try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufferSize); } catch (LineUnavailableException e) { e.printStackTrace(); return; } } private void go(){ line.start(); try { numBytesRead = 0; while (numBytesRead != -1) { if(sleep == false){ numBytesRead = source.read(buffer, 0, buffer.length); if (numBytesRead != -1) line.write(buffer, 0, numBytesRead); }else{} } } catch (IOException e) { e.printStackTrace(); } line.drain(); line.close(); }
Surtout que si je change la longueur, la chanson marche (enfin jusqu'à un certain moment).
Cordialement,
rXp<!>
Partager