Skip to content

Commit

Permalink
PlatformPlayer: More state handling improvements
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
AShiningRay committed Oct 19, 2024
1 parent 5dbc4cf commit 8a573cb
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/org/recompile/mobile/PlatformPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"); }
Expand Down Expand Up @@ -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]; }
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 8a573cb

Please sign in to comment.