Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
9Lukas5 committed Jan 3, 2018
2 parents 6125b01 + d70b706 commit 84d3af4
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/de/wiest_lukas/lib/AACPlayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Copyright (C) 2017 Lukas Wiest
* v1.1.0
* v1.2.0
*/
package de.wiest_lukas.lib;

Expand All @@ -27,7 +27,8 @@ public class AACPlayer
private boolean repeat; // controls the looping behaviour of the complete filelist
private Thread playback; // in this thread the actual playback will happen
private boolean paused; // get's checked regularly from the playback thread and controls it pause if set true
private boolean muted;
private boolean muted; // if set, the playback goes on, but doesn't write the read bytes to the AudioSystem
private boolean interrupted;// needed for Windows, as of broken SourceDataLine interrupt clearing on write method
private File[] files; // file list that the playback will play down

/**
Expand All @@ -41,6 +42,7 @@ public AACPlayer(File[] files)
repeat = false;
paused = false;
muted = false;
interrupted = false;

// LinkedList for all given files, which are a valid audiofile
List<File> validFiles = new LinkedList<>();
Expand Down Expand Up @@ -89,6 +91,7 @@ public AACPlayer(String pathToFile)

private void initThread()
{
interrupted = false; // needs to be reset, if the Thread is recreated, too.
playback = new Thread()
{
@Override
Expand Down Expand Up @@ -122,33 +125,35 @@ public void run()
dec = new Decoder(track.getDecoderSpecificInfo());

buf = new SampleBuffer();
while(track.hasMoreFrames()) // while we have frames left

playback:
while(!isInterrupted() && track.hasMoreFrames()) // while we have frames left
{
frame = track.readNextFrame(); // read next frame,
dec.decodeFrame(frame.getData(), buf); // decode it and put into the buffer
b = buf.getData(); // write the frame data from the buffer to our byte-array
if (!muted) // only write the sound to the line if we aren't muted
line.write(b, 0, b.length); // and from there write the byte array into our open AudioSystem DataLine
frame = track.readNextFrame(); // read next frame,
dec.decodeFrame(frame.getData(), buf); // decode it and put into the buffer
b = buf.getData(); // write the frame data from the buffer to our byte-array
if (!muted) // only write the sound to the line if we aren't muted
line.write(b, 0, b.length); // and from there write the byte array into our open AudioSystem DataLine

while (paused) // check if we should pause
if (interrupted && !isInterrupted()) // check for interrupt clearing source Data Line system
{
Thread.sleep(500); // if yes, stay half a second

if (Thread.interrupted()) // check if we should stop possibly
{
line.close(); // if yes, close line and
return; // exit thread
}
System.err.println("[LOG] - E - Source Data Line on your system clears interrupted flag!");
interrupt(); // and correct it if necessary
}

if (Thread.interrupted()) // if not in pause, still check on each frame if we should
{ // stop. If so
line.close(); // close line and
return; // exit thread
while (paused) // check if we should pause
{
Thread.sleep(500); // if yes, stay half a second

if (isInterrupted()) // check if we should stop possibly
break playback; // if yes, break playback loop
}
}

line.close(); // after titel is over, close line
line.close(); // after titel is over or playback loop got broken, close line

if (Thread.interrupted())
return; // if interrupt is set, clear it and leave

if (loop) // if we should loop current titel, set currentTrack -1,
currentTrack--; // as on bottom of for-next it get's +1 and so the same titel get's played again
Expand Down Expand Up @@ -185,7 +190,9 @@ public void play()
*/
public void stop()
{
playback.interrupt(); // tell playback to stop
if (playback != null) // avoid null Pointer exception, if someone stops before playing
playback.interrupt(); // tell playback to stop
interrupted = true; // set own interrupted flag
}

/**
Expand Down

0 comments on commit 84d3af4

Please sign in to comment.