From 0bce5f9b3c61cf8a70db08f0f27e9dcfb98e0c96 Mon Sep 17 00:00:00 2001 From: AShiningRay Date: Sat, 19 Oct 2024 12:54:25 -0300 Subject: [PATCH] PlatformPlayer: Tighten deallocate() and close() methods. More missing bits of implementation done with, players should now deallocate and close() properly in relation to the official docs. DOOM II RPG doesn't seem to leak memory in players anymore, which means that the mediaCache has probably outlived its usefulness, as midi performance has also been improved recently after reworking how midis are loaded in custom and default soundfonts. --- src/org/recompile/mobile/PlatformPlayer.java | 33 ++++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/org/recompile/mobile/PlatformPlayer.java b/src/org/recompile/mobile/PlatformPlayer.java index f9fd6a55..d243a016 100644 --- a/src/org/recompile/mobile/PlatformPlayer.java +++ b/src/org/recompile/mobile/PlatformPlayer.java @@ -150,11 +150,12 @@ public void close() try { player.stop(); - cacheDeallocate(); + deallocate(); state = Player.CLOSED; + player = null; notifyListeners(PlayerListener.CLOSED, null); } - catch (Exception e) { } + catch (Exception e) { System.out.println("Could not close player: " + e.getMessage()); } } public int getState() { return state; } @@ -207,14 +208,19 @@ private void notifyListeners(String event, Object eventData) } } - /* Due to the media cache, don't actually deallocate here when a MIDlet requests so */ public void deallocate() { if(getState() == Player.CLOSED) { throw new IllegalStateException("Cannot deallocate player, it is already CLOSED."); } stop(); - //player.deallocate(); - state = Player.REALIZED; + player.deallocate(); + + /* + * Only set state to REALIZED if we have effectively moved into REALIZED or higher (PREFETCHED, etc), + * as deallocate can be called during the transition from UNREALIZED to REALIZED, and if that happens, + * we can't actually set it as REALIZED, it must be kept as UNREALIZED. + */ + if(state > Player.UNREALIZED) {state = Player.REALIZED;} } /* @@ -375,10 +381,12 @@ public midiPlayer(InputStream stream) } } - public void realize() - { + public void realize() { state = Player.REALIZED; } + + public void prefetch() + { try - { + { midi.open(); if(Manager.useCustomMidi && Manager.hasLoadedCustomMidi) @@ -390,15 +398,6 @@ public void realize() midi.getTransmitter().setReceiver(MidiSystem.getReceiver()); } - state = Player.REALIZED; - } - catch (MidiUnavailableException e) { System.out.println("Couldn't realize midi player: " + e.getMessage());} - } - - public void prefetch() - { - try - { /* Make a new copy of the media stream, as prefetch() can be called more than once during the player's lifecycle */ midiSequence = MidiSystem.getSequence(new ByteArrayInputStream(stream)); midi.setSequence(midiSequence);