From 0765e1c017ce4e3ea5f9916c1e0c4be470b6b420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Fri, 22 Jun 2012 01:32:01 +0200 Subject: [PATCH] bugfixing in mpd --- src/de/ctdo/crashtest/mpd/MPDController.java | 166 ++++++++++--------- 1 file changed, 84 insertions(+), 82 deletions(-) diff --git a/src/de/ctdo/crashtest/mpd/MPDController.java b/src/de/ctdo/crashtest/mpd/MPDController.java index 50ce4d7..ed0213d 100644 --- a/src/de/ctdo/crashtest/mpd/MPDController.java +++ b/src/de/ctdo/crashtest/mpd/MPDController.java @@ -21,7 +21,6 @@ import java.util.Random; */ public class MPDController implements IMPDController { private MPD mpd; - private final Object lockObject = new Object(); public MPDController(String host) { try { @@ -47,39 +46,41 @@ public class MPDController implements IMPDController { Runnable r = new Runnable() { @Override public void run() { - addToPlayListIfNeeded(artist, title); - try { - Thread.sleep(500); - } catch (InterruptedException e) { } + synchronized (mpd) { + addToPlayListIfNeeded(artist, title); - try { - MPDPlaylist playlist = mpd.getMPDPlaylist(); + try { + Thread.sleep(200); - for(MPDSong song: playlist.getSongList()) { + MPDPlaylist playlist = mpd.getMPDPlaylist(); - if(song.getArtist() != null && song.getTitle() != null) { - if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) && - song.getTitle().toLowerCase().equals(title.toLowerCase())) { + for(MPDSong song: playlist.getSongList()) { - MPDPlayer player = mpd.getMPDPlayer(); - player.stop(); - player.playId(song); - break; + 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 (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()); + } } - } - }; + } + }; - synchronized (lockObject) { - new Thread(r).start(); - } + new Thread(r).start(); } } @@ -94,32 +95,31 @@ public class MPDController implements IMPDController { Runnable r = new Runnable() { @Override public void run() { + synchronized (mpd) { + 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(); - } + new Thread(r).start(); } } @@ -129,27 +129,30 @@ public class MPDController implements IMPDController { Runnable r = new Runnable() { @Override public void run() { - MPDPlayer player = mpd.getMPDPlayer(); + synchronized (mpd) { + 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 { + Thread.sleep(500); + + int length; + MPDSong song = player.getCurrentSong(); + if(song!= null) { + length = song.getLength(); + int skip = new Random().nextInt(length/2)+10; + player.seek(skip); + } + } catch (MPDConnectionException e) { + e.printStackTrace(); + } catch (MPDPlayerException e) { + e.printStackTrace(); + } catch (InterruptedException ignored) { } - } catch (MPDConnectionException e) { - e.printStackTrace(); - } catch (MPDPlayerException e) { - e.printStackTrace(); } } }; - synchronized (lockObject) { - new Thread(r).start(); - } + new Thread(r).start(); } } @@ -163,20 +166,19 @@ public class MPDController implements IMPDController { 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 (mpd) { + 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(); - } + new Thread(r).start(); } } @@ -189,24 +191,24 @@ public class MPDController implements IMPDController { 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 (mpd) { + 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(); - } + new Thread(r).start(); } }