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

Loading…
Cancel
Save