From 05e945cb3379b000de7ba0f279d30c1e0f7afc24 Mon Sep 17 00:00:00 2001 From: AShiningRay Date: Sat, 21 Oct 2023 18:22:01 -0300 Subject: [PATCH 1/2] PlatformPlayer: Attempt to load audio files with empty contentType 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. --- src/org/recompile/mobile/PlatformPlayer.java | 51 +++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/org/recompile/mobile/PlatformPlayer.java b/src/org/recompile/mobile/PlatformPlayer.java index 960abb56..7c400766 100644 --- a/src/org/recompile/mobile/PlatformPlayer.java +++ b/src/org/recompile/mobile/PlatformPlayer.java @@ -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; @@ -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(); @@ -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 @@ -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() From 01f743f4664ef15d3d91e8294645cdee304c665f Mon Sep 17 00:00:00 2001 From: AShiningRay Date: Sat, 21 Oct 2023 21:16:43 -0300 Subject: [PATCH 2/2] PlatformPlayer: Close midi sequencer if the stream wasn't opened Since we will manually try to load streams without an explicit type as one of the supported formats, opening a midi stream is now far more likely to fail, so account for that and close the Sequencer whenever that is the case. --- src/org/recompile/mobile/PlatformPlayer.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/org/recompile/mobile/PlatformPlayer.java b/src/org/recompile/mobile/PlatformPlayer.java index 7c400766..f329ff5b 100644 --- a/src/org/recompile/mobile/PlatformPlayer.java +++ b/src/org/recompile/mobile/PlatformPlayer.java @@ -256,7 +256,11 @@ public midiPlayer(InputStream stream) midi.setSequence(stream); state = Player.PREFETCHED; } - catch (Exception e) { System.out.println("Couldn't load MIDI file: " + e.getMessage()); } + catch (Exception e) + { + System.out.println("Couldn't load MIDI file: " + e.getMessage()); + midi.close(); + } } public void start()