From d50ee914efeea96a989d7cfc4eaf20cae072e270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Thu, 28 Jun 2012 16:45:01 +0200 Subject: [PATCH] extended mpd control to check if track is playing after starting new song --- src/de/ctdo/crashtest/game/TheGame.java | 7 + src/de/ctdo/crashtest/mpd/MPDController.java | 131 +++++++++++-------- 2 files changed, 87 insertions(+), 51 deletions(-) diff --git a/src/de/ctdo/crashtest/game/TheGame.java b/src/de/ctdo/crashtest/game/TheGame.java index 2664f68..0f3e7f5 100644 --- a/src/de/ctdo/crashtest/game/TheGame.java +++ b/src/de/ctdo/crashtest/game/TheGame.java @@ -20,6 +20,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent private Thread discoThread; private boolean shouldStopDisco; private boolean gemActivated = false; + private int gemCounter = 0; private boolean startedHurrySound = false; private char lastInput; private boolean sproing; @@ -239,6 +240,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent */ @Override public void timerTick(int tsecondsLeft) { + if(gemCounter>0) gemCounter--; guiControl.setCountDown(tsecondsLeft); if(tsecondsLeft == 0) { ircClient.say("timer expired"); @@ -421,6 +423,9 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent // Yes, it makes no sense, but I want have it anyway. For more Spass am Geraet! private void handleGemCommand() { if (!gemActivated) { + + if(gemCounter > 1200) return; + java.util.Random random = new java.util.Random(); int scry = random.nextInt(100); if (scry >= 99) { @@ -430,6 +435,8 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent } else { ircClient.say("Gem Activated"); } + + gemCounter+=100; } else { ircClient.say("Gem Deactivated"); } diff --git a/src/de/ctdo/crashtest/mpd/MPDController.java b/src/de/ctdo/crashtest/mpd/MPDController.java index 7ae6a25..838957f 100644 --- a/src/de/ctdo/crashtest/mpd/MPDController.java +++ b/src/de/ctdo/crashtest/mpd/MPDController.java @@ -12,6 +12,7 @@ import org.bff.javampd.exception.MPDPlaylistException; import org.bff.javampd.objects.MPDSong; import java.net.UnknownHostException; +import java.sql.SQLOutput; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -43,47 +44,75 @@ public class MPDController implements IMPDController { System.out.println("playSong: " + artist + " - " + title); if(mpd != null) { - /*Runnable r = new Runnable() { + Runnable r = new Runnable() { @Override public void run() { - synchronized (mpd) { */ + synchronized (mpd) { addToPlayListIfNeeded(artist, title); - try { - Thread.sleep(200); - - MPDPlaylist playlist = mpd.getMPDPlaylist(); - - for(MPDSong song: playlist.getSongList()) { - - if(song.getArtist() != null && song.getTitle() != null) { - if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) && - song.getTitle().toLowerCase().equals(title.toLowerCase())) { - - MPDPlayer player = mpd.getMPDPlayer(); - player.stop(); - - player.playId(song); - break; - } - } - } - - } catch (MPDConnectionException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDPlayerException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (InterruptedException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } - /*} + doPlaySong(artist,title); + } } }; - new Thread(r).start(); */ + //new Thread(r).start(); + r.run(); } } + private void doPlaySong(final String artist, final String title) { + try { + MPDPlaylist playlist = mpd.getMPDPlaylist(); + MPDPlayer player = mpd.getMPDPlayer(); + + int tries = 3; + + while(tries>0) { + for(MPDSong song: playlist.getSongList()) { + if(checkSong(song, artist, title)) { + System.out.println("MPD: stopping, playing, go..."); + player.stop(); + player.playId(song); + break; + } else { + System.out.println("MPD: track title or artist is null or not correct," + song.toString()); + } + } + + // now check, if it is playing + + MPDSong song = player.getCurrentSong(); + + if(checkSong(song, artist, title)) { + System.out.println("MPD: song is correctly playing"); + return; + } else { + System.out.println("MPD: whilst checking current song: track title or artist is null or not correct " + song.toString()); + } + + System.out.println("MPD: next try"); + tries--; + } + + Logger.sLog("MPD error: track not in playlist or not found... cannot play"); + + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlayerException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + + private boolean checkSong(final MPDSong song, final String artist, final String title) { + if(song.getArtist() != null && song.getTitle() != null) { + if(song.getArtist().getName().toLowerCase().contentEquals(artist.toLowerCase()) && + song.getTitle().toLowerCase().contentEquals(title.toLowerCase())) { + return true; + } + } + return false; + } + /** * Add a song to current mpd playlist * @param artist Artist of the track to play @@ -92,10 +121,10 @@ public class MPDController implements IMPDController { @Override public void addToPlayList(final String artist, final String title) { if(mpd != null) { - /*Runnable r = new Runnable() { + Runnable r = new Runnable() { @Override public void run() { - synchronized (mpd) { */ + synchronized (mpd) { MPDDatabase db = mpd.getMPDDatabase(); MPDPlaylist playlist = mpd.getMPDPlaylist(); @@ -115,26 +144,25 @@ public class MPDController implements IMPDController { } catch (MPDPlaylistException e) { Logger.sLog("MPD error: " + e.getMessage()); } - /*} + } } }; - new Thread(r).start(); */ + //new Thread(r).start(); + r.run(); } } @Override public void skipRandomStart() { if(mpd != null) { - /*Runnable r = new Runnable() { + Runnable r = new Runnable() { @Override public void run() { - synchronized (mpd) {*/ + synchronized (mpd) { MPDPlayer player = mpd.getMPDPlayer(); try { - Thread.sleep(500); - int length; MPDSong song = player.getCurrentSong(); if(song!= null) { @@ -146,13 +174,13 @@ public class MPDController implements IMPDController { e.printStackTrace(); } catch (MPDPlayerException e) { e.printStackTrace(); - } catch (InterruptedException ignored) { } - /*} + } } }; - new Thread(r).start(); */ + //new Thread(r).start(); + r.run(); } } @@ -163,10 +191,10 @@ public class MPDController implements IMPDController { @Override public void setVolume(final int volume) { if(mpd != null) { - /*Runnable r = new Runnable() { + Runnable r = new Runnable() { @Override public void run() { - synchronized (mpd) {*/ + synchronized (mpd) { try { mpd.getMPDPlayer().setVolume(volume); } catch (MPDConnectionException e) { @@ -174,11 +202,11 @@ public class MPDController implements IMPDController { } catch (MPDPlayerException e) { Logger.sLog("MPD error: " + e.getMessage()); } - /*} + } } }; - - new Thread(r).start();*/ + //new Thread(r).start(); + r.run(); } } @@ -188,10 +216,10 @@ public class MPDController implements IMPDController { @Override public void clearPlaylist() { if(mpd != null) { - /*Runnable r = new Runnable() { + Runnable r = new Runnable() { @Override public void run() { - synchronized (mpd) {*/ + synchronized (mpd) { try { MPDPlaylist playlist = mpd.getMPDPlaylist(); playlist.clearPlaylist(); @@ -204,11 +232,12 @@ public class MPDController implements IMPDController { } catch (MPDPlayerException e) { Logger.sLog("MPD error: " + e.getMessage()); } - /*} + } } }; - new Thread(r).start(); */ + //new Thread(r).start(); + r.run(); } }