[Jrisk-cvs] SF.net SVN: domination-code:[2535] Domination/sharedUI/src_mapstore_lobby/ net/yura/do
Brought to you by:
yuranet
|
From: <yu...@us...> - 2024-05-08 12:34:52
|
Revision: 2535
http://sourceforge.net/p/domination/code/2535
Author: yuranet
Date: 2024-05-08 12:34:49 +0000 (Wed, 08 May 2024)
Log Message:
-----------
handle oom in audio system
Modified Paths:
--------------
Domination/sharedUI/src_mapstore_lobby/net/yura/domination/audio/SimpleAudio.java
Modified: Domination/sharedUI/src_mapstore_lobby/net/yura/domination/audio/SimpleAudio.java
===================================================================
--- Domination/sharedUI/src_mapstore_lobby/net/yura/domination/audio/SimpleAudio.java 2024-05-08 12:00:25 UTC (rev 2534)
+++ Domination/sharedUI/src_mapstore_lobby/net/yura/domination/audio/SimpleAudio.java 2024-05-08 12:34:49 UTC (rev 2535)
@@ -11,14 +11,15 @@
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
-import javax.microedition.media.PlayerListener;
-public class SimpleAudio implements AudioSystem, PlayerListener, ThreadFactory {
+public class SimpleAudio implements AudioSystem, ThreadFactory {
private static final Logger LOGGER = Logger.getLogger(SimpleAudio.class.getName());
-
+
Map<String, Player> currentPlayers = new HashMap(); // filename -> player
+ private boolean outOfMemoryError;
+
/**
* we need a single thread for starting and stopping music
* otherwise if one thread starts it and another thread stops it
@@ -45,32 +46,57 @@
}
public void play(String fileName) {
+ if (outOfMemoryError) return;
+
+ Player player = null;
try {
- Player player = getPlayer(fileName);
- player.start();
+ player = getPlayer(fileName);
+ player.start(); // can throw oom
}
catch (Exception ex) {
- LOGGER.log(Level.WARNING, "unable to play " + fileName, ex);
+ startError(fileName, player, ex);
}
+ catch (OutOfMemoryError oom) {
+ outOfMemoryError = true;
+ startError(fileName, player, oom);
+ }
}
@Override
public void start(final String fileName) {
+ if (outOfMemoryError) return;
+
singleThread.execute(new Runnable() {
@Override
public void run() {
+ Player player = null;
try {
- Player player = getPlayer(fileName);
+ player = getPlayer(fileName);
player.setLoopCount(-1);
currentPlayers.put(fileName, player);
- player.start();
+ player.start(); // can throw oom
}
catch (Exception ex) {
- LOGGER.log(Level.WARNING, "unable to play " + fileName, ex);
+ startError(fileName, player, ex);
}
+ catch (OutOfMemoryError oom) {
+ outOfMemoryError = true;
+ startError(fileName, player, oom);
+ }
}
});
}
+
+ private void startError(String fileName, Player player, Throwable ex) {
+ LOGGER.log(Level.WARNING, "unable to play " + fileName, ex);
+ try {
+ currentPlayers.remove(fileName);
+ if (player != null) {
+ player.close();
+ }
+ }
+ catch (Throwable th) {}
+ }
@Override
public void stop(final String audioFile) {
@@ -80,13 +106,15 @@
try {
Player player = currentPlayers.remove(audioFile);
if (player != null) {
- if (player.getState() != Player.STARTED) {
- LOGGER.log(Level.INFO, "player not started yet, will stop with listener: " + audioFile);
- player.addPlayerListener(SimpleAudio.this);
- }
+ // this is not needed as we only start and stop from a single thread
+ //if (player.getState() != Player.STARTED) {
+ // LOGGER.log(Level.INFO, "player not started yet, will stop with listener: " + audioFile);
+ // player.addPlayerListener(SimpleAudio.this);
+ //}
player.stop();
}
else {
+ // this really should never happen
LOGGER.log(Level.INFO, "unable to stop, not found: " + audioFile);
}
}
@@ -96,14 +124,4 @@
}
});
}
-
- @Override
- public void playerUpdate(Player player, String event, Object eventData) {
- try {
- player.stop();
- }
- catch (Exception ex) {
- LOGGER.log(Level.WARNING, "unable to stop " + player, ex);
- }
- }
}
|