Better handling of misnamed MP3/OGG files. (part of #120)
This also catches more MP3 loading errors that could occur and properly cleans up resources. Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
parent
4b68cebc21
commit
20d40dd520
|
@ -76,8 +76,9 @@ public class Mp3InputStream extends InputStream implements AudioInputStream {
|
||||||
/**
|
/**
|
||||||
* Create a new stream to decode MP3 data.
|
* Create a new stream to decode MP3 data.
|
||||||
* @param input the input stream from which to read the MP3 file
|
* @param input the input stream from which to read the MP3 file
|
||||||
|
* @throws IOException failure to read the header from the input stream
|
||||||
*/
|
*/
|
||||||
public Mp3InputStream(InputStream input) {
|
public Mp3InputStream(InputStream input) throws IOException {
|
||||||
decoder = new Decoder();
|
decoder = new Decoder();
|
||||||
bitstream = new Bitstream(input);
|
bitstream = new Bitstream(input);
|
||||||
try {
|
try {
|
||||||
|
@ -85,6 +86,10 @@ public class Mp3InputStream extends InputStream implements AudioInputStream {
|
||||||
} catch (BitstreamException e) {
|
} catch (BitstreamException e) {
|
||||||
Log.error(e);
|
Log.error(e);
|
||||||
}
|
}
|
||||||
|
if (header == null) {
|
||||||
|
close();
|
||||||
|
throw new IOException("Failed to read header from MP3 input stream.");
|
||||||
|
}
|
||||||
|
|
||||||
channels = (header.mode() == Header.SINGLE_CHANNEL) ? 1 : 2;
|
channels = (header.mode() == Header.SINGLE_CHANNEL) ? 1 : 2;
|
||||||
sampleRate = header.frequency();
|
sampleRate = header.frequency();
|
||||||
|
|
|
@ -152,16 +152,30 @@ public class OpenALStreamPlayer {
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
audio = new OggInputStream(url.openStream());
|
audio = new OggInputStream(url.openStream());
|
||||||
} else {
|
} else {
|
||||||
if (ref.toLowerCase().endsWith(".mp3"))
|
if (ref.toLowerCase().endsWith(".mp3")) {
|
||||||
audio = new Mp3InputStream(ResourceLoader.getResourceAsStream(ref));
|
try {
|
||||||
else
|
|
||||||
audio = new OggInputStream(ResourceLoader.getResourceAsStream(ref));
|
|
||||||
|
|
||||||
if (audio.getRate() == 0 && audio.getChannels() == 0) {
|
|
||||||
if (ref.toLowerCase().endsWith(".mp3"))
|
|
||||||
audio = new OggInputStream(ResourceLoader.getResourceAsStream(ref));
|
|
||||||
else
|
|
||||||
audio = new Mp3InputStream(ResourceLoader.getResourceAsStream(ref));
|
audio = new Mp3InputStream(ResourceLoader.getResourceAsStream(ref));
|
||||||
|
} catch (IOException e) {
|
||||||
|
// invalid MP3: check if file is actually OGG
|
||||||
|
try {
|
||||||
|
audio = new OggInputStream(ResourceLoader.getResourceAsStream(ref));
|
||||||
|
} catch (IOException e1) {
|
||||||
|
throw e; // invalid OGG: re-throw original MP3 exception
|
||||||
|
}
|
||||||
|
if (audio.getRate() == 0 && audio.getChannels() == 0)
|
||||||
|
throw e; // likely not OGG: re-throw original MP3 exception
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
audio = new OggInputStream(ResourceLoader.getResourceAsStream(ref));
|
||||||
|
if (audio.getRate() == 0 && audio.getChannels() == 0) {
|
||||||
|
// invalid OGG: check if file is actually MP3
|
||||||
|
AudioInputStream audioOGG = audio;
|
||||||
|
try {
|
||||||
|
audio = new Mp3InputStream(ResourceLoader.getResourceAsStream(ref));
|
||||||
|
} catch (IOException e) {
|
||||||
|
audio = audioOGG; // invalid MP3: keep OGG stream
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user