Skip to content

Commit 0f3581a

Browse files
committed
Better pause logic + load chunk when seeking
1 parent 4775008 commit 0f3581a

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/player/AudioFileStreaming.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,18 @@ public synchronized void reset() {
202202
}
203203

204204
@Override
205-
public synchronized long skip(long n) {
205+
public synchronized long skip(long n) throws IOException {
206206
long k = size - pos;
207207
if (n < k) k = n < 0 ? 0 : n;
208208
pos += k;
209+
210+
int chunk = pos / CHUNK_SIZE;
211+
checkAvailability(chunk, false);
212+
209213
return k;
210214
}
211215

212-
private void checkAvailability(int chunk) throws IOException {
216+
private void checkAvailability(int chunk, boolean wait) throws IOException {
213217
if (!requested[chunk]) {
214218
requestChunkFromStream(chunk);
215219
requested[chunk] = true;
@@ -220,7 +224,7 @@ private void checkAvailability(int chunk) throws IOException {
220224
requested[chunk + 1] = true;
221225
}
222226

223-
if (!available[chunk])
227+
if (wait && !available[chunk])
224228
waitFor(chunk);
225229
}
226230

@@ -240,7 +244,7 @@ public int read(@NotNull byte[] b, int off, int len) throws IOException {
240244
int chunk = pos / CHUNK_SIZE;
241245
int chunkOff = pos % CHUNK_SIZE;
242246

243-
checkAvailability(chunk);
247+
checkAvailability(chunk, true);
244248

245249
int copy = Math.min(buffer[chunk].length - chunkOff, len - i);
246250
System.arraycopy(buffer[chunk], chunkOff, b, off + i, copy);
@@ -258,7 +262,7 @@ public synchronized int read() throws IOException {
258262
return -1;
259263

260264
int chunk = pos / CHUNK_SIZE;
261-
checkAvailability(chunk);
265+
checkAvailability(chunk, true);
262266

263267
return buffer[chunk][pos++ % CHUNK_SIZE] & 0xff;
264268
}

core/src/main/java/xyz/gianlu/librespot/player/PlayerRunner.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)