@@ -41,6 +41,7 @@ public class PlayerRunner implements Runnable {
4141 private final Mixer mixer ;
4242 private final Controller controller ;
4343 private final int duration ;
44+ private final Object pauseLock = new Object ();
4445 private byte [] buffer ;
4546 private int count ;
4647 private int index ;
@@ -56,6 +57,7 @@ public class PlayerRunner implements Runnable {
5657 PlayerRunner (@ NotNull AudioFileStreaming audioFile , @ NotNull NormalizationData normalizationData ,
5758 @ NotNull Player .PlayerConfiguration configuration , @ NotNull Listener listener , int duration ) throws IOException , PlayerException {
5859 this .audioIn = audioFile .stream ();
60+ this .duration = duration ;
5961 this .listener = listener ;
6062 this .normalizationFactor = normalizationData .getFactor (configuration );
6163 this .mixer = AudioSystem .getMixer (AudioSystem .getMixerInfo ()[0 ]);
@@ -67,7 +69,6 @@ public class PlayerRunner implements Runnable {
6769 readHeader ();
6870 initializeSound ();
6971 this .controller = new Controller (outputLine );
70- this .duration = duration ;
7172
7273 audioIn .mark (-1 );
7374
@@ -185,20 +186,20 @@ private void readBody() throws PlayerException, IOException {
185186
186187 index = joggSyncState .buffer (BUFFER_SIZE );
187188 buffer = joggSyncState .data ;
188-
189189 if (index == -1 )
190190 break ;
191191
192192 count = audioIn .read (buffer , index , BUFFER_SIZE );
193193 joggSyncState .wrote (count );
194-
195194 if (count == 0 )
196195 break ;
197196 } else {
198197 outputLine .stop ();
199198
200199 try {
201- Thread .sleep (500 );
200+ synchronized (pauseLock ) {
201+ pauseLock .wait ();
202+ }
202203 } catch (InterruptedException ex ) {
203204 throw new RuntimeException (ex );
204205 }
@@ -207,8 +208,6 @@ private void readBody() throws PlayerException, IOException {
207208 }
208209
209210 private void decodeCurrentPacket () {
210- long granulepos = joggPacket .granulepos ;
211-
212211 if (jorbisBlock .synthesis (joggPacket ) == 0 )
213212 jorbisDspState .synthesis_blockin (jorbisBlock );
214213
@@ -231,13 +230,14 @@ private void decodeCurrentPacket() {
231230 convertedBuffer [sampleIndex ] = (byte ) (value );
232231 convertedBuffer [sampleIndex + 1 ] = (byte ) (value >>> 8 );
233232
234- sampleIndex += 2 * ( jorbisInfo .channels ) ;
233+ sampleIndex += 2 * jorbisInfo .channels ;
235234 }
236235 }
237236
238237 outputLine .write (convertedBuffer , 0 , 2 * jorbisInfo .channels * range );
239238 jorbisDspState .synthesis_read (range );
240239
240+ long granulepos = joggPacket .granulepos ;
241241 if (granulepos != -1 && joggPacket .e_o_s == 0 ) {
242242 granulepos -= samples ;
243243 pcm_offset = granulepos ;
@@ -247,7 +247,7 @@ private void decodeCurrentPacket() {
247247 }
248248
249249 private void checkPreload () {
250- if (!calledPreload && (duration / 1000 ) - time () <= TRACK_PRELOAD_THRESHOLD ) {
250+ if (!calledPreload && ! stopped && (duration / 1000 ) - time () <= TRACK_PRELOAD_THRESHOLD ) {
251251 calledPreload = true ;
252252 listener .preloadNextTrack ();
253253 }
@@ -281,6 +281,9 @@ public void run() {
281281
282282 void play () {
283283 playing = true ;
284+ synchronized (pauseLock ) {
285+ pauseLock .notifyAll ();
286+ }
284287 }
285288
286289 void pause () {
0 commit comments