Mercurial > hg > ristipolku
comparison game/SoundElement.java @ 23:0741dc117808
Remove sound code for later refactoring.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 01 Feb 2011 09:55:09 +0200 |
parents | afde253ec705 |
children | 736de7b28701 |
comparison
equal
deleted
inserted
replaced
22:afde253ec705 | 23:0741dc117808 |
---|---|
29 SoundElement(String filename, boolean streaming) throws IOException | 29 SoundElement(String filename, boolean streaming) throws IOException |
30 { | 30 { |
31 this.name = filename; | 31 this.name = filename; |
32 this.streaming = streaming; | 32 this.streaming = streaming; |
33 | 33 |
34 /* | |
34 ResourceLoader res = new ResourceLoader(name); | 35 ResourceLoader res = new ResourceLoader(name); |
35 if (res == null || res.getStream() == null) | 36 if (res == null || res.getStream() == null) |
36 { | 37 { |
37 throw new IOException("Could not load audio resource '"+name+"'.\n"); | 38 throw new IOException("Could not load audio resource '"+name+"'.\n"); |
38 } | 39 } |
78 } | 79 } |
79 catch (LineUnavailableException e) { | 80 catch (LineUnavailableException e) { |
80 throw new IOException("Line unavailable for '"+name+"'.\n"); | 81 throw new IOException("Line unavailable for '"+name+"'.\n"); |
81 } | 82 } |
82 } | 83 } |
84 */ | |
83 } | 85 } |
84 | 86 |
85 public void play() | 87 public void play() |
86 { | 88 { |
87 System.out.print("Sound("+name+").play()\n"); | 89 System.out.print("Sound("+name+").play()\n"); |
88 if (streaming) | |
89 { | |
90 clip.setFramePosition(0); | |
91 clip.start(); | |
92 } | |
93 else | |
94 { | |
95 if (playThread == null) | |
96 { | |
97 doPlay = true; | |
98 loopCount = 1; | |
99 playThread = new Thread(this); | |
100 playThread.start(); | |
101 } | |
102 } | |
103 } | 90 } |
104 | 91 |
105 public void loop(int n) | 92 public void loop(int n) |
106 { | 93 { |
107 System.out.print("Sound("+name+").loop("+n+")\n"); | 94 System.out.print("Sound("+name+").loop("+n+")\n"); |
108 if (isClip()) | |
109 { | |
110 clip.setFramePosition(0); | |
111 if (n < 0) | |
112 clip.loop(Clip.LOOP_CONTINUOUSLY); | |
113 else | |
114 clip.loop(n); | |
115 } | |
116 else | |
117 { | |
118 if (playThread == null) | |
119 { | |
120 doPlay = true; | |
121 loopCount = n; | |
122 playThread = new Thread(this); | |
123 playThread.start(); | |
124 } | |
125 } | |
126 } | 95 } |
127 | 96 |
128 public void stop() | 97 public void stop() |
129 { | 98 { |
130 if (isClip()) | |
131 { | |
132 if (clip.isRunning()) | |
133 clip.stop(); | |
134 } | |
135 else | |
136 { | |
137 if (playThread != null) | |
138 { | |
139 playThread.interrupt(); | |
140 doPlay = false; | |
141 playThread = null; | |
142 } | |
143 } | |
144 } | 99 } |
145 | 100 |
146 public boolean isPlaying() | 101 public boolean isPlaying() |
147 { | 102 { |
148 if (isClip()) | 103 return false; |
149 return clip.isRunning(); | |
150 else | |
151 return (playThread != null && line.isRunning()); | |
152 } | 104 } |
153 | 105 |
154 public void run() | 106 public void run() |
155 { | 107 { |
156 line.start(); | |
157 byte[] buf = new byte[line.getBufferSize()]; | |
158 | |
159 while (doPlay && (loopCount > 0 || loopCount == -1)) | |
160 { | |
161 try { | |
162 int numRead = 0; | |
163 while ((numRead = stream.read(buf, 0, buf.length)) >= 0 && doPlay) | |
164 { | |
165 int offset = 0; | |
166 while (offset < numRead) | |
167 { | |
168 System.out.print("audioThread: offs="+offset+", numread="+numRead+"\n"); | |
169 offset += line.write(buf, offset, numRead - offset); | |
170 } | |
171 } | |
172 line.drain(); | |
173 | |
174 System.out.print("audioThread: stream.reset()\n"); | |
175 stream.reset(); | |
176 } | |
177 catch (IOException e) { | |
178 } | |
179 | |
180 if (loopCount > 0) | |
181 loopCount--; | |
182 } | |
183 | |
184 line.stop(); | |
185 doPlay = false; | |
186 } | 108 } |
187 } | 109 } |
188 | |
189 |