added playlist controls to mpdcontroller

This commit is contained in:
Lucas Pleß 2012-06-10 21:55:33 +02:00
parent 2bbd040f4d
commit 535a66a216
2 changed files with 87 additions and 23 deletions

View File

@ -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);
}

View File

@ -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<MPDSong> tracks = new ArrayList<MPDSong>(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<MPDSong> tracks = new ArrayList<MPDSong>(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<MPDSong> tracks = new ArrayList<MPDSong>(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());
}
}
}