bugfixing in mpd

This commit is contained in:
Lucas Pleß 2012-06-22 01:32:01 +02:00
parent e416d44400
commit 0765e1c017
1 changed files with 84 additions and 82 deletions

View File

@ -21,7 +21,6 @@ import java.util.Random;
*/ */
public class MPDController implements IMPDController { public class MPDController implements IMPDController {
private MPD mpd; private MPD mpd;
private final Object lockObject = new Object();
public MPDController(String host) { public MPDController(String host) {
try { try {
@ -47,39 +46,41 @@ public class MPDController implements IMPDController {
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
addToPlayListIfNeeded(artist, title); synchronized (mpd) {
try { addToPlayListIfNeeded(artist, title);
Thread.sleep(500);
} catch (InterruptedException e) { }
try { try {
MPDPlaylist playlist = mpd.getMPDPlaylist(); Thread.sleep(200);
for(MPDSong song: playlist.getSongList()) { 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) { } catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) { } catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage()); 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() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) {
MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist();
MPDDatabase db = mpd.getMPDDatabase(); try {
MPDPlaylist playlist = mpd.getMPDPlaylist(); List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist));
try { for(MPDSong song: tracks) {
List<MPDSong> tracks = new ArrayList<MPDSong>(db.findArtist(artist)); if(song.getName() != null && song.getName().toLowerCase().contains(title.toLowerCase())) {
playlist.addSong(song);
for(MPDSong song: tracks) { break;
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() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
MPDPlayer player = mpd.getMPDPlayer(); synchronized (mpd) {
MPDPlayer player = mpd.getMPDPlayer();
try { try {
int length; Thread.sleep(500);
MPDSong song = player.getCurrentSong();
if(song!= null) { int length;
length = song.getLength(); MPDSong song = player.getCurrentSong();
int skip = new Random().nextInt(length/2); if(song!= null) {
player.seek(skip); 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() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) {
try { try {
mpd.getMPDPlayer().setVolume(volume); mpd.getMPDPlayer().setVolume(volume);
} catch (MPDConnectionException e) { } catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) { } catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage()); 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() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
try { synchronized (mpd) {
MPDPlaylist playlist = mpd.getMPDPlaylist(); try {
playlist.clearPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
mpd.getMPDPlayer().setRandom(false); playlist.clearPlaylist();
mpd.getMPDPlayer().setXFade(1); mpd.getMPDPlayer().setRandom(false);
} catch (MPDConnectionException e) { mpd.getMPDPlayer().setXFade(1);
Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDConnectionException e) {
} catch (MPDPlaylistException e) { Logger.sLog("MPD error: " + e.getMessage());
Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDPlaylistException e) {
} catch (MPDPlayerException 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();
new Thread(r).start();
}
} }
} }