removed threads from mpdcontroller

This commit is contained in:
Lucas Pleß 2012-06-15 09:33:15 +02:00
parent 28989f2511
commit da3aef9704
1 changed files with 49 additions and 82 deletions

View File

@ -41,39 +41,32 @@ public class MPDController implements IMPDController {
@Override @Override
public void playSong(final String artist, final String title) { public void playSong(final String artist, final String title) {
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
addToPlayListIfNeeded(artist, title);
try { addToPlayListIfNeeded(artist, title);
MPDPlaylist playlist = mpd.getMPDPlaylist();
for(MPDSong song: playlist.getSongList()) { try {
MPDPlaylist playlist = mpd.getMPDPlaylist();
if(song.getArtist() != null && song.getTitle() != null) { for(MPDSong song: playlist.getSongList()) {
if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) &&
song.getTitle().toLowerCase().equals(title.toLowerCase())) {
MPDPlayer player = mpd.getMPDPlayer(); if(song.getArtist() != null && song.getTitle() != null) {
player.stop(); if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) &&
player.playId(song); song.getTitle().toLowerCase().equals(title.toLowerCase())) {
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());
} }
} }
};
synchronized (lockObject) { } catch (MPDConnectionException e) {
new Thread(r).start(); Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
} }
@ -85,33 +78,25 @@ public class MPDController implements IMPDController {
@Override @Override
public void addToPlayList(final String artist, final String title) { public void addToPlayList(final String artist, final String title) {
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() {
@Override
public void run() {
MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist();
try { MPDDatabase db = mpd.getMPDDatabase();
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist)); MPDPlaylist playlist = mpd.getMPDPlaylist();
for(MPDSong song: tracks) { try {
if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) { List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
playlist.addSong(song);
break; for(MPDSong song: tracks) {
} if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) {
} playlist.addSong(song);
} catch (MPDConnectionException e) { break;
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());
synchronized (lockObject) { } catch (MPDDatabaseException e) {
new Thread(r).start(); Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage());
} }
} }
} }
@ -123,21 +108,12 @@ public class MPDController implements IMPDController {
@Override @Override
public void setVolume(final int volume) { public void setVolume(final int volume) {
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() { try {
@Override mpd.getMPDPlayer().setVolume(volume);
public void run() { } catch (MPDConnectionException e) {
try { Logger.sLog("MPD error: " + e.getMessage());
mpd.getMPDPlayer().setVolume(volume); } catch (MPDPlayerException e) {
} catch (MPDConnectionException e) { Logger.sLog("MPD error: " + e.getMessage());
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
} }
} }
} }
@ -148,26 +124,17 @@ public class MPDController implements IMPDController {
@Override @Override
public void clearPlaylist() { public void clearPlaylist() {
if(mpd != null) { if(mpd != null) {
Runnable r = new Runnable() { try {
@Override MPDPlaylist playlist = mpd.getMPDPlaylist();
public void run() { playlist.clearPlaylist();
try { mpd.getMPDPlayer().setRandom(false);
MPDPlaylist playlist = mpd.getMPDPlaylist(); mpd.getMPDPlayer().setXFade(1);
playlist.clearPlaylist(); } catch (MPDConnectionException e) {
mpd.getMPDPlayer().setRandom(false); Logger.sLog("MPD error: " + e.getMessage());
mpd.getMPDPlayer().setXFade(1); } catch (MPDPlaylistException e) {
} catch (MPDConnectionException e) { Logger.sLog("MPD error: " + e.getMessage());
Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDPlayerException e) {
} catch (MPDPlaylistException e) { Logger.sLog("MPD error: " + e.getMessage());
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
};
synchronized (lockObject) {
new Thread(r).start();
} }
} }
} }