From 2a40f0971e3a8a591be771624dbd7ffbc609a650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Tue, 19 Jun 2012 21:37:01 +0200 Subject: [PATCH] changed mpd back to thread and fixed playlist handling --- src/de/ctdo/crashtest/game/TheGame.java | 11 +- src/de/ctdo/crashtest/mpd/MPDController.java | 204 ++++++++++++------- 2 files changed, 131 insertions(+), 84 deletions(-) diff --git a/src/de/ctdo/crashtest/game/TheGame.java b/src/de/ctdo/crashtest/game/TheGame.java index d0d8ff0..7518e19 100644 --- a/src/de/ctdo/crashtest/game/TheGame.java +++ b/src/de/ctdo/crashtest/game/TheGame.java @@ -65,7 +65,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent case ENTERED_ROOM: relaisboard.setRelais(7, false); // disable light barrier over relais mpdController.playSong("crashtest", "entered_room"); - mpdController.setVolume(70); bunti.setLampel(false,false,false); bunti.setPar56(20,0,100); @@ -79,7 +78,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent relaisboard.setRelais(6, true); // enable third green circle mpdController.playSong("crashtest", "table_game_one"); - mpdController.setVolume(70); bunti.setLampel(true,false,false); bunti.setPar56(20,0,100); @@ -87,7 +85,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent break; case TABLE_GAME_TWO: mpdController.playSong("crashtest", "table_game_two"); - mpdController.setVolume(67); bunti.setLampel(false,true,false); bunti.setPar56(100,0,100); @@ -96,7 +93,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent break; case TABLE_GAME_THREE: mpdController.playSong("crashtest", "table_game_three"); - mpdController.setVolume(63); bunti.setLampel(false,true,false); bunti.setPar56(255,35,0); @@ -105,7 +101,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent break; case TABLE_GAME_FOUR: mpdController.playSong("crashtest", "table_game_four"); - mpdController.setVolume(60); bunti.setLampel(false,true,false); bunti.setPar56(200,100,0); @@ -114,7 +109,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent break; case TABLE_GAME_FIVE: mpdController.playSong("crashtest", "table_game_five"); - mpdController.setVolume(57); bunti.setLampel(false,true,false); bunti.setPar56(150,150,0); @@ -123,7 +117,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent break; case TABLE_GAME_SIX: mpdController.playSong("crashtest", "table_game_six"); - mpdController.setVolume(53); bunti.setLampel(false,true,false); bunti.setPar56(100,200,0); @@ -132,7 +125,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent break; case TABLE_GAME_DONE: mpdController.playSong("crashtest", "table_game_done"); - mpdController.setVolume(50); bunti.setLampel(false,false,true); bunti.setPar56(100,255,0); @@ -160,7 +152,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent case ROKET_STARTED: mpdController.playSong("crashtest", "roket_started"); mpdController.addToPlayList("crashtest", "roket_started2"); - mpdController.setVolume(50); bunti.setLampel(false,true,false); bunti.setPar56(0, 255, 0); @@ -176,7 +167,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent break; case ROKET_DONE: mpdController.playSong("crashtest", "roket_done"); - mpdController.setVolume(60); + //mpdController.setVolume(60); bunti.setLampel(false,false,true); bunti.setPar56(255, 255, 255); diff --git a/src/de/ctdo/crashtest/mpd/MPDController.java b/src/de/ctdo/crashtest/mpd/MPDController.java index e48cefa..50ce4d7 100644 --- a/src/de/ctdo/crashtest/mpd/MPDController.java +++ b/src/de/ctdo/crashtest/mpd/MPDController.java @@ -21,6 +21,7 @@ import java.util.Random; */ public class MPDController implements IMPDController { private MPD mpd; + private final Object lockObject = new Object(); public MPDController(String host) { try { @@ -41,33 +42,44 @@ public class MPDController implements IMPDController { @Override public void playSong(final String artist, final String title) { System.out.println("playSong: " + artist + " - " + title); + if(mpd != null) { + Runnable r = new Runnable() { + @Override + public void run() { + addToPlayListIfNeeded(artist, title); + try { + Thread.sleep(500); + } catch (InterruptedException e) { } - addToPlayListIfNeeded(artist, title); + try { + MPDPlaylist playlist = mpd.getMPDPlaylist(); - try { - MPDPlaylist playlist = mpd.getMPDPlaylist(); + for(MPDSong song: playlist.getSongList()) { - 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())) { - 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; + 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 (MPDConnectionException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDPlayerException e) { - Logger.sLog("MPD error: " + e.getMessage()); + synchronized (lockObject) { + new Thread(r).start(); } - } } @@ -79,25 +91,34 @@ public class MPDController implements IMPDController { @Override public void addToPlayList(final String artist, final String title) { if(mpd != null) { + Runnable r = new Runnable() { + @Override + public void run() { - MPDDatabase db = mpd.getMPDDatabase(); - MPDPlaylist playlist = mpd.getMPDPlaylist(); + MPDDatabase db = mpd.getMPDDatabase(); + MPDPlaylist playlist = mpd.getMPDPlaylist(); - try { - List tracks = new ArrayList(db.findArtist(artist)); + try { + List tracks = new ArrayList(db.findArtist(artist)); - for(MPDSong song: tracks) { - if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) { - playlist.addSong(song); - break; + for(MPDSong song: tracks) { + if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) { + playlist.addSong(song); + break; + } + } + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDDatabaseException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlaylistException e) { + Logger.sLog("MPD error: " + e.getMessage()); } } - } catch (MPDConnectionException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDDatabaseException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDPlaylistException e) { - Logger.sLog("MPD error: " + e.getMessage()); + }; + + synchronized (lockObject) { + new Thread(r).start(); } } } @@ -105,20 +126,29 @@ public class MPDController implements IMPDController { @Override public void skipRandomStart() { if(mpd != null) { - MPDPlayer player = mpd.getMPDPlayer(); + Runnable r = new Runnable() { + @Override + public void run() { + MPDPlayer player = mpd.getMPDPlayer(); - try { - int length; - MPDSong song = player.getCurrentSong(); - if(song!= null) { - length = song.getLength(); - int skip = new Random().nextInt(length/2); - player.seek(skip); + try { + int length; + MPDSong song = player.getCurrentSong(); + if(song!= null) { + length = song.getLength(); + int skip = new Random().nextInt(length/2); + player.seek(skip); + } + } catch (MPDConnectionException e) { + e.printStackTrace(); + } catch (MPDPlayerException e) { + e.printStackTrace(); + } } - } catch (MPDConnectionException e) { - e.printStackTrace(); - } catch (MPDPlayerException e) { - e.printStackTrace(); + }; + + synchronized (lockObject) { + new Thread(r).start(); } } } @@ -130,12 +160,22 @@ public class MPDController implements IMPDController { @Override public void setVolume(final int volume) { if(mpd != null) { - try { - mpd.getMPDPlayer().setVolume(volume); - } catch (MPDConnectionException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDPlayerException e) { - Logger.sLog("MPD error: " + e.getMessage()); + Runnable r = new Runnable() { + @Override + public void run() { + + try { + mpd.getMPDPlayer().setVolume(volume); + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlayerException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + }; + + synchronized (lockObject) { + new Thread(r).start(); } } } @@ -146,17 +186,26 @@ public class MPDController implements IMPDController { @Override public void clearPlaylist() { if(mpd != null) { - try { - MPDPlaylist playlist = mpd.getMPDPlaylist(); - playlist.clearPlaylist(); - mpd.getMPDPlayer().setRandom(false); - mpd.getMPDPlayer().setXFade(1); - } catch (MPDConnectionException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDPlaylistException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDPlayerException e) { - Logger.sLog("MPD error: " + e.getMessage()); + Runnable r = new Runnable() { + @Override + public void run() { + try { + MPDPlaylist playlist = mpd.getMPDPlaylist(); + playlist.clearPlaylist(); + mpd.getMPDPlayer().setRandom(false); + mpd.getMPDPlayer().setXFade(1); + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlaylistException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlayerException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + }; + + synchronized (lockObject) { + new Thread(r).start(); } } } @@ -165,23 +214,30 @@ public class MPDController implements IMPDController { MPDDatabase db = mpd.getMPDDatabase(); MPDPlaylist playlist = mpd.getMPDPlaylist(); - try { - List tracks = new ArrayList(db.findArtist(artist)); + int count; - for(MPDSong song: tracks) { - if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) { - if(!playlist.getSongList().contains(song)) { - playlist.addSong(song); + for(count = 0; count < 3; count++) { + + try { + List tracks = new ArrayList(db.findArtist(artist)); + + for(MPDSong song: tracks) { + if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) { + if(!playlist.getSongList().contains(song)) { + playlist.addSong(song); + } + break; } - break; } + + return; + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDDatabaseException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlaylistException e) { + Logger.sLog("MPD error: " + e.getMessage()); } - } catch (MPDConnectionException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDDatabaseException e) { - Logger.sLog("MPD error: " + e.getMessage()); - } catch (MPDPlaylistException e) { - Logger.sLog("MPD error: " + e.getMessage()); } }