Skip to content

Commit

Permalink
Merge pull request #203 from AShiningRay/try-opening-notype-streams
Browse files Browse the repository at this point in the history
PlatformPlayer: Attempt to load audio files with empty contentType
  • Loading branch information
recompileorg authored Feb 16, 2024
2 parents d8aa5d9 + 01f743f commit 896146a
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions src/org/recompile/mobile/PlatformPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package org.recompile.mobile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Vector;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
Expand Down Expand Up @@ -60,17 +62,40 @@ public PlatformPlayer(InputStream stream, String type)
{
player = new midiPlayer(stream);
}
else
else if(type.equalsIgnoreCase("audio/x-wav") || type.equalsIgnoreCase("audio/wav"))
{
if(type.equalsIgnoreCase("audio/x-wav") || type.equalsIgnoreCase("audio/wav"))
player = new wavPlayer(stream);
}
else if (type.equalsIgnoreCase("")) /* If the stream doesn't have an accompanying type, try everything we can to try and load it */
{
try
{
player = new wavPlayer(stream);
final byte[] tryStream = new byte[stream.available()];
readInputStreamData(stream, tryStream, 0, stream.available());

System.out.println("Received no explicit audio type. Trying to load as MIDI, and if it fails, WAV.");
/* Try loading it as a MIDI file first */
try { player = new midiPlayer(new ByteArrayInputStream(tryStream)); }
catch (Exception e) { }

/* If that doesn't work, try as WAV next, if it still doesn't work, we have no other players to try */
try { player = new wavPlayer(new ByteArrayInputStream(tryStream)); }
catch (Exception e)
{
System.out.println("No Player For: "+contentType);
player = new audioplayer();
}
}
else /* TODO: Implement a player for amr and mpeg audio types */
catch (IOException e)
{
System.out.println("No Player For: "+contentType);
player = new audioplayer();
System.out.println("Couldn't read input stream: " + e.getMessage());
}

}
else /* TODO: Implement a player for amr and mpeg audio types */
{
System.out.println("No Player For: "+contentType);
player = new audioplayer();
}
}
controls[0] = new volumeControl();
Expand Down Expand Up @@ -189,6 +214,18 @@ public Control[] getControls()
return controls;
}

/* Read 'n' Bytes from the InputStream. Used by IMA ADPCM decoder as well. */
public static void readInputStreamData(InputStream input, byte[] output, int offset, int nBytes) throws IOException
{
int end = offset + nBytes;
while(offset < end)
{
int read = input.read(output, offset, end - offset);
if(read < 0) throw new java.io.EOFException();
offset += read;
}
}

// Players //

private class audioplayer
Expand Down Expand Up @@ -219,7 +256,11 @@ public midiPlayer(InputStream stream)
midi.setSequence(stream);
state = Player.PREFETCHED;
}
catch (Exception e) { }
catch (Exception e)
{
System.out.println("Couldn't load MIDI file: " + e.getMessage());
midi.close();
}
}

public void start()
Expand Down

0 comments on commit 896146a

Please sign in to comment.