[Jrisk-cvs] SF.net SVN: domination-code:[2529] Domination/sharedUI/src_mapstore_lobby/ net/yura/do
Brought to you by:
yuranet
|
From: <yu...@us...> - 2024-05-07 21:06:51
|
Revision: 2529
http://sourceforge.net/p/domination/code/2529
Author: yuranet
Date: 2024-05-07 21:06:48 +0000 (Tue, 07 May 2024)
Log Message:
-----------
only allow playing one song as a time
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-07 19:53:35 UTC (rev 2528)
+++ Domination/sharedUI/src_mapstore_lobby/net/yura/domination/audio/SimpleAudio.java 2024-05-07 21:06:48 UTC (rev 2529)
@@ -3,6 +3,8 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.media.Manager;
@@ -16,6 +18,13 @@
Map<String, Player> currentPlayers = new HashMap(); // filename -> player
+ /**
+ * we need a single thread for starting and stopping music
+ * otherwise if one thread starts it and another thread stops it
+ * the stop may never happen as it may never find the player
+ */
+ private final Executor singleThread = Executors.newSingleThreadExecutor();
+
private Player getPlayer(String fileName) throws IOException, MediaException {
// TODO we need some sort of cache, as each time java seems to load it from scratch
@@ -38,19 +47,49 @@
}
@Override
- public void start(String fileName) {
- try {
- Player player = getPlayer(fileName);
- player.setLoopCount(-1);
- currentPlayers.put(fileName, player);
- player.start();
- }
- catch (Exception ex) {
- LOGGER.log(Level.WARNING, "unable to play " + fileName, ex);
- }
+ public void start(final String fileName) {
+ singleThread.execute(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ Player player = getPlayer(fileName);
+ player.setLoopCount(-1);
+ currentPlayers.put(fileName, player);
+ player.start();
+ }
+ catch (Exception ex) {
+ LOGGER.log(Level.WARNING, "unable to play " + fileName, ex);
+ }
+ }
+ });
}
@Override
+ public void stop(final String audioFile) {
+ singleThread.execute(new Runnable() {
+ @Override
+ public void run() {
+ 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);
+ }
+ player.stop();
+ }
+ else {
+ LOGGER.log(Level.INFO, "unable to stop, not found: " + audioFile);
+ }
+ }
+ catch (Exception ex) {
+ LOGGER.log(Level.WARNING, "unable to stop " + audioFile, ex);
+ }
+ }
+ });
+ }
+
+ @Override
public void playerUpdate(Player player, String event, Object eventData) {
try {
player.stop();
@@ -59,24 +98,4 @@
LOGGER.log(Level.WARNING, "unable to stop " + player, ex);
}
}
-
- @Override
- public void stop(String audioFile) {
- 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(this);
- }
- player.stop();
- }
- else {
- LOGGER.log(Level.INFO, "unable to stop, not found: " + audioFile);
- }
- }
- catch (Exception ex) {
- LOGGER.log(Level.WARNING, "unable to stop " + audioFile, ex);
- }
- }
}
|