Compare commits
2 Commits
f8139bd4f6
...
c1d58da8a4
Author | SHA1 | Date |
---|---|---|
|
c1d58da8a4 | |
|
e207b34c0f |
|
@ -95,6 +95,11 @@ public class MusicHandler implements AudioSendHandler, Closeable
|
||||||
currentSong.delete();
|
currentSong.delete();
|
||||||
currentSong = null;
|
currentSong = null;
|
||||||
}
|
}
|
||||||
|
if (player != null)
|
||||||
|
{
|
||||||
|
player.close();
|
||||||
|
player = null;
|
||||||
|
}
|
||||||
currentSong = queueManager.nextSongNeeded();
|
currentSong = queueManager.nextSongNeeded();
|
||||||
if (nowPlayingConsumer != null)
|
if (nowPlayingConsumer != null)
|
||||||
nowPlayingConsumer.accept(currentSong);
|
nowPlayingConsumer.accept(currentSong);
|
||||||
|
@ -179,57 +184,6 @@ public class MusicHandler implements AudioSendHandler, Closeable
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// private void fillBuffer(boolean canSkip)
|
|
||||||
// {
|
|
||||||
// // use what we have in our buffer to send audio as PCM
|
|
||||||
// while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
|
|
||||||
// if (!readData())
|
|
||||||
// if (!canSkip || !nextSong())
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private boolean readData()
|
|
||||||
// {
|
|
||||||
// if (din == null)
|
|
||||||
// return false;
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// // if (din.available() == 0)
|
|
||||||
// // return false;
|
|
||||||
// int bytesToRead = DESIRED_BUFFER_SIZE - audioBuffer.getCurrentNumberOfBytes();
|
|
||||||
// int space = audioBuffer.getSpace();
|
|
||||||
// if (din.available() > 0 && din.available() < bytesToRead)
|
|
||||||
// bytesToRead = din.available();
|
|
||||||
// if (bytesToRead > space)
|
|
||||||
// bytesToRead = space;
|
|
||||||
// if (bytesToRead == 0)
|
|
||||||
// return false;
|
|
||||||
// byte[] bytes = new byte[bytesToRead];
|
|
||||||
// // byte[] bytes = din.readNBytes(bytesToRead);
|
|
||||||
// int read = din.read(bytes);
|
|
||||||
//// System.out.println("Wanted: " + byteCount + " Space:" + space + " Available: " + din.available() + " To read: " + bytesToRead + " Read: " + read);
|
|
||||||
// if (read < 0)
|
|
||||||
// return false;
|
|
||||||
//// queue.add(bytes);
|
|
||||||
//
|
|
||||||
// audioBuffer.add(bytes, 0, read);
|
|
||||||
// } catch (IOException ex)
|
|
||||||
// {
|
|
||||||
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
// return false;
|
|
||||||
// } catch (ArrayIndexOutOfBoundsException ex)
|
|
||||||
// {
|
|
||||||
// if (!arrayErr)
|
|
||||||
// arrayErr = true;
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
public Song getCurrentSong()
|
public Song getCurrentSong()
|
||||||
{
|
{
|
||||||
return currentSong;
|
return currentSong;
|
||||||
|
|
|
@ -24,6 +24,7 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer;
|
||||||
public class TrackPlayer extends Thread implements Closeable
|
public class TrackPlayer extends Thread implements Closeable
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private static final boolean DEBUG_PRINT = false;
|
||||||
private static final int DESIRED_BUFFER_SIZE = 3840 * 500;
|
private static final int DESIRED_BUFFER_SIZE = 3840 * 500;
|
||||||
private static final int MAX_READ_FAILS = 3;
|
private static final int MAX_READ_FAILS = 3;
|
||||||
|
|
||||||
|
@ -73,23 +74,30 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
System.out.println("DISK THREAD: waiting");
|
if (DEBUG_PRINT)
|
||||||
fillBufferWait.wait();
|
System.out.println("DISK THREAD: waiting");
|
||||||
|
fillBufferWait.wait(5000);
|
||||||
} catch (InterruptedException ex)
|
} catch (InterruptedException ex)
|
||||||
{
|
{
|
||||||
// this is normal
|
// this is normal
|
||||||
}
|
}
|
||||||
System.out.println("DISK THREAD: kicked");
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("DISK THREAD: kicked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
boolean notAtEnd = fillBuffer(true);
|
boolean notAtEnd = fillBuffer(true);
|
||||||
if (!notAtEnd)
|
if (!notAtEnd)
|
||||||
|
{
|
||||||
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("DISK THREAD: end of input");
|
||||||
ended = true;
|
ended = true;
|
||||||
|
}
|
||||||
synchronized (bufferFilledWait)
|
synchronized (bufferFilledWait)
|
||||||
{
|
{
|
||||||
System.out.println("DISK THREAD: buffer filled; kicking read thread");
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("DISK THREAD: buffer filled; kicking read thread");
|
||||||
bufferFilledWait.notifyAll();
|
bufferFilledWait.notifyAll();
|
||||||
}
|
}
|
||||||
} catch (IOException ex)
|
} catch (IOException ex)
|
||||||
|
@ -98,7 +106,9 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
ended = true;
|
ended = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("DISK THREAD ENDED");
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("DISK THREAD ENDED");
|
||||||
|
bufferFilledWait.notifyAll(); // kick read thread in case it was waiting for us
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean has(int byteCount)
|
public boolean has(int byteCount)
|
||||||
|
@ -131,26 +141,31 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
{
|
{
|
||||||
synchronized (fillBufferWait)
|
synchronized (fillBufferWait)
|
||||||
{
|
{
|
||||||
System.out.println("READ THREAD: kicking disk thread");
|
if (!ended)
|
||||||
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
|
{
|
||||||
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("READ THREAD: kicking disk thread");
|
||||||
|
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
int bytes;
|
int bytes;
|
||||||
synchronized (audioBuffer)
|
synchronized (audioBuffer)
|
||||||
{
|
{
|
||||||
bytes = audioBuffer.getCurrentNumberOfBytes();
|
bytes = audioBuffer.getCurrentNumberOfBytes();
|
||||||
}
|
}
|
||||||
if (bytes == 0)
|
if (bytes == 0 && !ended)
|
||||||
{
|
{
|
||||||
synchronized (bufferFilledWait)
|
synchronized (bufferFilledWait)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
System.out.println("READ THREAD: waiting for disk thread");
|
System.out.println("READ THREAD: waiting for disk thread");
|
||||||
bufferFilledWait.wait(); // wait for disk thread to fill the buffer
|
bufferFilledWait.wait(5000); // wait for disk thread to fill the buffer
|
||||||
} catch (InterruptedException ex)
|
} catch (InterruptedException ex)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
System.out.println("READ THREAD: kicked");
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("READ THREAD: kicked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
synchronized (audioBuffer)
|
synchronized (audioBuffer)
|
||||||
|
@ -172,12 +187,13 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
|
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
|
||||||
{
|
{
|
||||||
boolean read = readData();
|
boolean read = readData();
|
||||||
if (!read && !canSkip)
|
if (!read)
|
||||||
return false;
|
{
|
||||||
else if (fails < MAX_READ_FAILS)
|
if (canSkip && fails < MAX_READ_FAILS)
|
||||||
fails++;
|
fails++;
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -210,7 +226,8 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
byte[] bytes = new byte[bytesToRead];
|
byte[] bytes = new byte[bytesToRead];
|
||||||
// byte[] bytes = din.readNBytes(bytesToRead);
|
// byte[] bytes = din.readNBytes(bytesToRead);
|
||||||
int read = input.read(bytes);
|
int read = input.read(bytes);
|
||||||
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read);
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read);
|
||||||
if (read < 0)
|
if (read < 0)
|
||||||
return false;
|
return false;
|
||||||
// queue.add(bytes);
|
// queue.add(bytes);
|
||||||
|
|
Loading…
Reference in New Issue