From 535a66a216b38fd760d71198d50f951272c7d3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sun, 10 Jun 2012 21:55:33 +0200 Subject: [PATCH] added playlist controls to mpdcontroller --- src/de/ctdo/crashtest/mpd/IMPDController.java | 2 + src/de/ctdo/crashtest/mpd/MPDController.java | 108 ++++++++++++++---- 2 files changed, 87 insertions(+), 23 deletions(-) diff --git a/src/de/ctdo/crashtest/mpd/IMPDController.java b/src/de/ctdo/crashtest/mpd/IMPDController.java index 275b72c..1b5861e 100644 --- a/src/de/ctdo/crashtest/mpd/IMPDController.java +++ b/src/de/ctdo/crashtest/mpd/IMPDController.java @@ -7,4 +7,6 @@ package de.ctdo.crashtest.mpd; public interface IMPDController { void playSong(final String artist, final String title); void setVolume(final int volume); + void clearPlaylist(); + void addToPlayList(final String artist, final String title); } diff --git a/src/de/ctdo/crashtest/mpd/MPDController.java b/src/de/ctdo/crashtest/mpd/MPDController.java index 6d80daf..8eb38e5 100644 --- a/src/de/ctdo/crashtest/mpd/MPDController.java +++ b/src/de/ctdo/crashtest/mpd/MPDController.java @@ -20,6 +20,7 @@ import java.util.List; */ public class MPDController implements IMPDController { private MPD mpd; + private final Object lockObject = new Object(); public MPDController(String host) { try { @@ -46,7 +47,6 @@ public class MPDController implements IMPDController { addToPlayListIfNeeded(artist, title); try { - MPDPlaylist playlist = mpd.getMPDPlaylist(); for(MPDSong song: playlist.getSongList()) { @@ -71,36 +71,48 @@ public class MPDController implements IMPDController { } }; - synchronized (mpd) { + synchronized (lockObject) { new Thread(r).start(); } } } - - private void addToPlayListIfNeeded(final String artist, final String title) { - MPDDatabase db = mpd.getMPDDatabase(); - MPDPlaylist playlist = mpd.getMPDPlaylist(); - try { - List tracks = new ArrayList(db.findArtist(artist)); + /** + * Add a song to current mpd playlist + * @param artist Artist of the track to play + * @param title Title of the track to play + */ + @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(); - for(MPDSong song: tracks) { - if(song.getName() != null && - song.getName().toLowerCase().contains(title.toLowerCase())) { + try { + List tracks = new ArrayList(db.findArtist(artist)); - if(!playlist.getSongList().contains(song)) { - playlist.addSong(song); + 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()); } - 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()); + synchronized (lockObject) { + new Thread(r).start(); + } } } @@ -111,7 +123,6 @@ public class MPDController implements IMPDController { @Override public void setVolume(final int volume) { if(mpd != null) { - Runnable r = new Runnable() { @Override public void run() { @@ -125,9 +136,60 @@ public class MPDController implements IMPDController { } }; - synchronized (mpd) { + synchronized (lockObject) { new Thread(r).start(); } } } + + /** + * Clears the current mpd playlist + */ + @Override + public void clearPlaylist() { + if(mpd != null) { + Runnable r = new Runnable() { + @Override + public void run() { + try { + MPDPlaylist playlist = mpd.getMPDPlaylist(); + playlist.clearPlaylist(); + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlaylistException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + }; + + synchronized (lockObject) { + new Thread(r).start(); + } + } + } + + private void addToPlayListIfNeeded(final String artist, final String title) { + MPDDatabase db = mpd.getMPDDatabase(); + MPDPlaylist playlist = mpd.getMPDPlaylist(); + + 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; + } + } + } 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()); + } + } + }