Make MusicHandler track ending behaviour more constistent.

soundboard
Nekojimi 8 months ago
parent 5ec62d4d43
commit 3da1c95453
  1. 44
      src/main/java/moe/nekojimi/chords/MusicHandler.java

@ -38,7 +38,7 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
this.nowPlayingConsumer = nowPlayingConsumer;
}
private Track currentTrack;
// private Track currentTrack;
// private TrackPlayer player;
private final List<TrackPlayer> playingTracks = new ArrayList<>();
@ -61,6 +61,16 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
public void playOver(Track track)
{
if (!isPlaying())
play(track);
try
{
TrackPlayer player = new TrackPlayer(track);
playingTracks.add(player);
} catch (UnsupportedAudioFileException | IOException ex)
{
Logger.getLogger(Chords.class.getName()).log(Level.SEVERE, null, ex);
}
}
@ -71,7 +81,9 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
public boolean play(Track track, boolean immediate)
{
if (track == currentTrack)
if (playingTracks.stream().anyMatch((t) -> t.getTrack() == track))
return false;
if (track == null)
return false;
if (immediate)
@ -82,30 +94,19 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
try
{
if (currentTrack != null)
{
if (!currentTrack.isKept())
currentTrack.delete();
currentTrack = null;
}
currentTrack = track;
if (nowPlayingConsumer != null)
nowPlayingConsumer.accept(currentTrack);
if (currentTrack == null)
{
return false;
}
nowPlayingConsumer.accept(track);
// System.out.println("Playing track " + currentTrack.getLocation().getAbsolutePath());
arrayErr = false;
byteCount = 3840;
TrackPlayer player = new TrackPlayer(currentTrack);
TrackPlayer player = new TrackPlayer(track);
playingTracks.add(player);
// System.out.println("Queue filled to " + audioBuffer.getCurrentNumberOfBytes());
return true;
} catch (UnsupportedAudioFileException | IOException ex)
{
Logger.getLogger(Chords.class.getName()).log(Level.SEVERE, null, ex);
currentTrack = null;
requestTrack();
} finally
{
@ -179,6 +180,12 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
// outOfInput = true;
}
}
for (TrackPlayer emptyPlayer : emptyPlayers)
{
if (!emptyPlayer.getTrack().isKept())
emptyPlayer.getTrack().delete();
emptyPlayer.getTrack().clearInputStream();
}
playingTracks.removeAll(emptyPlayers);
ret.put(mixBuffers(mixes));
if (outOfInput)
@ -198,7 +205,9 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
public Track getCurrentTrack()
{
return currentTrack;
if (playingTracks.isEmpty())
return null;
return playingTracks.get(0).getTrack();
}
@Override
@ -251,7 +260,6 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
if (!isPlaying())
return false;
playingTracks.clear();
currentTrack = null;
requestTrack();
return true;
}

Loading…
Cancel
Save