Skip to content

Commit

Permalink
PlatformPlayer: Attempt to load audio files with empty contentType
Browse files Browse the repository at this point in the history
In "Gangstar Rio - City of Saints", it appears this empty type
is actually used by WAV files, so let's try opening streams that
come with those empty strings, they might contain one of the
supported media formats.

As opposed to printing "No player for: " on the console, Gangstar
Rio now plays SFX correctly. Jurassic Park on the other hand,
fails to open its very first audio stream as either MIDI or WAV.
  • Loading branch information
AShiningRay committed Oct 22, 2023
1 parent d8aa5d9 commit 05e945c
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 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,7 @@ 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()); }
}

public void start()
Expand Down

0 comments on commit 05e945c

Please sign in to comment.