From 8a573cb02a48534f3fdb744e20ab6fe3ae4b1d90 Mon Sep 17 00:00:00 2001 From: AShiningRay Date: Sat, 19 Oct 2024 15:30:04 -0300 Subject: [PATCH] PlatformPlayer: More state handling improvements Do a bit more than just set midiPlayer's state as REALIZED in its realize() method (or else we get errors when closing/deallocating a realized midiPlayer), and only allow getting controls if the player is at least realized (previously it only checked if it was closed) --- src/org/recompile/mobile/PlatformPlayer.java | 27 +++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/org/recompile/mobile/PlatformPlayer.java b/src/org/recompile/mobile/PlatformPlayer.java index d1fbe607..667a0b34 100644 --- a/src/org/recompile/mobile/PlatformPlayer.java +++ b/src/org/recompile/mobile/PlatformPlayer.java @@ -249,12 +249,13 @@ public long getMediaTime() public void prefetch() { if(getState() == Player.CLOSED) { throw new IllegalStateException("Cannot prefetch player, as it is in the CLOSED state."); } + if(getState() == Player.UNREALIZED) { realize(); } if(getState() == Player.REALIZED) { player.prefetch(); } } - /* Both midi and wav players do nothing other than just set their state as REALIZED here. */ + /* Both midi and wav players do little more than just set their state as REALIZED here. */ public void realize() { if(getState() == Player.CLOSED) { throw new IllegalStateException("Cannot realize player, as it is in the CLOSED state"); } @@ -282,7 +283,7 @@ public long setMediaTime(long now) public Control getControl(String controlType) { - if(getState() == Player.CLOSED) { throw new IllegalStateException("Cannot call getControl(), as the player is CLOSED."); } + if(getState() == Player.CLOSED || getState() == Player.UNREALIZED) { throw new IllegalStateException("Cannot call getControl(), as the player is either CLOSED or UNREALIZED."); } if(controlType.equals("VolumeControl")) { return controls[0]; } if(controlType.equals("TempoControl")) { return controls[1]; } @@ -292,12 +293,13 @@ public Control getControl(String controlType) if(controlType.equals("javax.microedition.media.control.TempoControl")) { return controls[1]; } if(controlType.equals("javax.microedition.media.control.MIDIControl")) { return controls[2]; } if(controlType.equals("javax.microedition.media.control.ToneControl")) { return controls[3]; } + return null; } public Control[] getControls() { - if(getState() == Player.CLOSED) { throw new IllegalStateException("Cannot call getControls(), as the player is CLOSED."); } + if(getState() == Player.CLOSED || getState() == Player.UNREALIZED) { throw new IllegalStateException("Cannot call getControls(), as the player is either CLOSED or UNREALIZED."); } return controls; } @@ -365,17 +367,28 @@ public midiPlayer(InputStream stream) catch (Exception e) { System.out.println("Couldn't load midi file:" + e.getMessage()); - midi.close(); } } - public void realize() { state = Player.REALIZED; } + public void realize() + { + try + { + midi.open(); + state = Player.REALIZED; + } + catch (Exception e) + { + System.out.println("Could not realize midi stream:" + e.getMessage()); + deallocate(); + state = Player.UNREALIZED; + } + } public void prefetch() { try - { - midi.open(); + { if(Manager.useCustomMidi && Manager.hasLoadedCustomMidi) {