Skip to content

Commit

Permalink
replace anonymous inner class with lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
9Lukas5 committed Jan 3, 2018
1 parent 2c94cae commit d2e464c
Showing 1 changed file with 57 additions and 61 deletions.
118 changes: 57 additions & 61 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.2.0
* v1.2.1
*/
package de.wiest_lukas.lib;

Expand Down Expand Up @@ -92,79 +92,75 @@ public AACPlayer(String pathToFile)
private void initThread()
{
interrupted = false; // needs to be reset, if the Thread is recreated, too.
playback = new Thread()
playback = new Thread(() ->
{
@Override
public void run()
// local vars
byte[] b; // array for the actual audio Data during the playback
AudioTrack track; // track we are playing atm
AudioFormat af; // the track's format
SourceDataLine line; // the line we'll use the get our audio to the speaker's
Decoder dec; // decoder to get the audio bytes
Frame frame; //
SampleBuffer buf; //
int currentTrack; // index of current track from playlist
MP4Container cont; // container to open the current track with
Movie movie; // and get the content from the container

try
{
// local vars
byte[] b; // array for the actual audio Data during the playback
AudioTrack track; // track we are playing atm
AudioFormat af; // the track's format
SourceDataLine line; // the line we'll use the get our audio to the speaker's
Decoder dec; // decoder to get the audio bytes
Frame frame; //
SampleBuffer buf; //
int currentTrack; // index of current track from playlist
MP4Container cont; // container to open the current track with
Movie movie; // and get the content from the container

try
// for-next loop to play each titel from the playlist once
for (currentTrack = 0; currentTrack < files.length; currentTrack++)
{
// for-next loop to play each titel from the playlist once
for (currentTrack = 0; currentTrack < files.length; currentTrack++)
{
cont = new MP4Container(new RandomAccessFile(files[currentTrack], "r")); // open titel with random access
movie = cont.getMovie(); // get content from container,
track = (AudioTrack) movie.getTracks().get(0); // grab first track and set the audioformat
af = new AudioFormat(track.getSampleRate(), track.getSampleSize(), track.getChannelCount(), true, true);
line = AudioSystem.getSourceDataLine(af); // get a DataLine from the AudioSystem
line.open(); // open and
line.start(); // start it

dec = new Decoder(track.getDecoderSpecificInfo());
cont = new MP4Container(new RandomAccessFile(files[currentTrack], "r")); // open titel with random access
movie = cont.getMovie(); // get content from container,
track = (AudioTrack) movie.getTracks().get(0); // grab first track and set the audioformat
af = new AudioFormat(track.getSampleRate(), track.getSampleSize(), track.getChannelCount(), true, true);
line = AudioSystem.getSourceDataLine(af); // get a DataLine from the AudioSystem
line.open(); // open and
line.start(); // start it

buf = new SampleBuffer();
dec = new Decoder(track.getDecoderSpecificInfo());

playback:
while(!interrupted && 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

while (paused) // check if we should pause
{
Thread.sleep(500); // if yes, stay half a second

if (interrupted) // check if we should stop possibly
break playback; // if yes, break playback loop
}
}
buf = new SampleBuffer();

line.drain();
line.close(); // after titel is over or playback loop got broken, close line
playback:
while(!interrupted && 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

if (interrupted)
while (paused) // check if we should pause
{
interrupt();
return; // if interrupt is set, clear it and leave
Thread.sleep(500); // if yes, stay half a second

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

line.drain(); // make sure the sound written to the buffer get's played back
line.close(); // after titel is over or playback loop got broken, close line

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
else if (repeat && (currentTrack == files.length -1)) // else check if we are at the end of the playlist
currentTrack = -1; // and should repeat the whole list. If so, set currentTrack -1, so it get's 0 on for-next bottom
if (interrupted)
{
Thread.currentThread().interrupt();
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
else if (repeat && (currentTrack == files.length -1)) // else check if we are at the end of the playlist
currentTrack = -1; // and should repeat the whole list. If so, set currentTrack -1, so it get's 0 on for-next bottom
}
catch (LineUnavailableException | IOException | InterruptedException e)
{
e.printStackTrace();
}
}
};
catch (LineUnavailableException | IOException | InterruptedException e)
{
e.printStackTrace();
}
});
}

/**
Expand Down

0 comments on commit d2e464c

Please sign in to comment.