Retry opening track files after failure, and gracefully handle final failure by skipping the track.

master
Nekojimi 1 month ago
parent dd977cc899
commit abd676fa49
  1. 1
      src/main/java/moe/nekojimi/chords/MusicHandler.java
  2. 34
      src/main/java/moe/nekojimi/chords/TrackPlayer.java

@ -105,6 +105,7 @@ public class MusicHandler implements AudioSendHandler, Closeable, Consumer<Track
} catch (UnsupportedAudioFileException | IOException ex)
{
Logger.getLogger(Chords.class.getName()).log(Level.SEVERE, null, ex);
skipTrack();
} finally
{
}

@ -8,6 +8,8 @@ package moe.nekojimi.chords;
import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
@ -24,6 +26,8 @@ public class TrackPlayer implements Closeable
private static final int DESIRED_BUFFER_SIZE = 3840 * 500;
private static final int MAX_READ_FAILS = 3;
private static final int RETRY_COUNT = 10;
private static final int RETRY_DELAY = 100;
private final CircularByteBuffer audioBuffer = new CircularByteBuffer(3840 * 1024);
@ -33,8 +37,34 @@ public class TrackPlayer implements Closeable
public TrackPlayer(Track track) throws UnsupportedAudioFileException, IOException
{
AudioInputStream in = AudioSystem.getAudioInputStream(track.getLocation());
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
AudioInputStream in = null;
AudioFormat decodedFormat = null;
for (int retry = 0; retry < RETRY_COUNT; retry++)
{
try
{
in = AudioSystem.getAudioInputStream(track.getLocation());
decodedFormat = AudioSendHandler.INPUT_FORMAT;
break; // it worked!
} catch (IOException ex)
{
try
{
Thread.sleep(((long) Math.pow(2, retry)) * RETRY_DELAY);
} catch (InterruptedException ex1)
{
if (retry < RETRY_COUNT)
{
System.err.println("Open file " + track.getLocation() + " failed, retry " + retry + "...");
continue;
} else
{
throw ex;
}
}
}
}
input = AudioSystem.getAudioInputStream(decodedFormat, in);
fillBuffer(false);
}

Loading…
Cancel
Save