Compare commits
No commits in common. "c1d58da8a45369e2d375bfa765426e48ecd6f8e6" and "f8139bd4f68ab00cb7c3a61bb026b3912c08aa26" have entirely different histories.
c1d58da8a4
...
f8139bd4f6
|
@ -95,11 +95,6 @@ public class MusicHandler implements AudioSendHandler, Closeable
|
|||
currentSong.delete();
|
||||
currentSong = null;
|
||||
}
|
||||
if (player != null)
|
||||
{
|
||||
player.close();
|
||||
player = null;
|
||||
}
|
||||
currentSong = queueManager.nextSongNeeded();
|
||||
if (nowPlayingConsumer != null)
|
||||
nowPlayingConsumer.accept(currentSong);
|
||||
|
@ -184,6 +179,57 @@ public class MusicHandler implements AudioSendHandler, Closeable
|
|||
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()
|
||||
{
|
||||
return currentSong;
|
||||
|
|
|
@ -24,7 +24,6 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer;
|
|||
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 MAX_READ_FAILS = 3;
|
||||
|
||||
|
@ -74,30 +73,23 @@ public class TrackPlayer extends Thread implements Closeable
|
|||
{
|
||||
try
|
||||
{
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println("DISK THREAD: waiting");
|
||||
fillBufferWait.wait(5000);
|
||||
System.out.println("DISK THREAD: waiting");
|
||||
fillBufferWait.wait();
|
||||
} catch (InterruptedException ex)
|
||||
{
|
||||
// this is normal
|
||||
}
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println("DISK THREAD: kicked");
|
||||
System.out.println("DISK THREAD: kicked");
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
boolean notAtEnd = fillBuffer(true);
|
||||
if (!notAtEnd)
|
||||
{
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println("DISK THREAD: end of input");
|
||||
ended = true;
|
||||
}
|
||||
synchronized (bufferFilledWait)
|
||||
{
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println("DISK THREAD: buffer filled; kicking read thread");
|
||||
System.out.println("DISK THREAD: buffer filled; kicking read thread");
|
||||
bufferFilledWait.notifyAll();
|
||||
}
|
||||
} catch (IOException ex)
|
||||
|
@ -106,9 +98,7 @@ public class TrackPlayer extends Thread implements Closeable
|
|||
ended = true;
|
||||
}
|
||||
}
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println("DISK THREAD ENDED");
|
||||
bufferFilledWait.notifyAll(); // kick read thread in case it was waiting for us
|
||||
System.out.println("DISK THREAD ENDED");
|
||||
}
|
||||
|
||||
public boolean has(int byteCount)
|
||||
|
@ -141,31 +131,26 @@ public class TrackPlayer extends Thread implements Closeable
|
|||
{
|
||||
synchronized (fillBufferWait)
|
||||
{
|
||||
if (!ended)
|
||||
{
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println("READ THREAD: kicking disk thread");
|
||||
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
|
||||
}
|
||||
System.out.println("READ THREAD: kicking disk thread");
|
||||
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
|
||||
}
|
||||
int bytes;
|
||||
synchronized (audioBuffer)
|
||||
{
|
||||
bytes = audioBuffer.getCurrentNumberOfBytes();
|
||||
}
|
||||
if (bytes == 0 && !ended)
|
||||
if (bytes == 0)
|
||||
{
|
||||
synchronized (bufferFilledWait)
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("READ THREAD: waiting for disk thread");
|
||||
bufferFilledWait.wait(5000); // wait for disk thread to fill the buffer
|
||||
bufferFilledWait.wait(); // wait for disk thread to fill the buffer
|
||||
} catch (InterruptedException ex)
|
||||
{
|
||||
}
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println("READ THREAD: kicked");
|
||||
System.out.println("READ THREAD: kicked");
|
||||
}
|
||||
}
|
||||
synchronized (audioBuffer)
|
||||
|
@ -187,13 +172,12 @@ public class TrackPlayer extends Thread implements Closeable
|
|||
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
|
||||
{
|
||||
boolean read = readData();
|
||||
if (!read)
|
||||
{
|
||||
if (canSkip && fails < MAX_READ_FAILS)
|
||||
fails++;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
if (!read && !canSkip)
|
||||
return false;
|
||||
else if (fails < MAX_READ_FAILS)
|
||||
fails++;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -226,8 +210,7 @@ public class TrackPlayer extends Thread implements Closeable
|
|||
byte[] bytes = new byte[bytesToRead];
|
||||
// byte[] bytes = din.readNBytes(bytesToRead);
|
||||
int read = input.read(bytes);
|
||||
if (DEBUG_PRINT)
|
||||
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read);
|
||||
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read);
|
||||
if (read < 0)
|
||||
return false;
|
||||
// queue.add(bytes);
|
||||
|
|
Loading…
Reference in New Issue