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,12 +46,12 @@ public class MPDController implements IMPDController {
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) {
addToPlayListIfNeeded(artist, title); addToPlayListIfNeeded(artist, title);
try {
Thread.sleep(500);
} catch (InterruptedException e) { }
try { try {
Thread.sleep(200);
MPDPlaylist playlist = mpd.getMPDPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
for(MPDSong song: playlist.getSongList()) { for(MPDSong song: playlist.getSongList()) {
@ -63,6 +62,7 @@ public class MPDController implements IMPDController {
MPDPlayer player = mpd.getMPDPlayer(); MPDPlayer player = mpd.getMPDPlayer();
player.stop(); player.stop();
player.playId(song); player.playId(song);
break; break;
} }
@ -73,15 +73,16 @@ public class MPDController implements IMPDController {
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();
} }
} }
}
/** /**
* Add a song to current mpd playlist * Add a song to current mpd playlist
@ -94,7 +95,7 @@ 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(); MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
@ -115,13 +116,12 @@ public class MPDController implements IMPDController {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} }
} }
}
}; };
synchronized (lockObject) {
new Thread(r).start(); new Thread(r).start();
} }
} }
}
@Override @Override
public void skipRandomStart() { public void skipRandomStart() {
@ -129,29 +129,32 @@ public class MPDController implements IMPDController {
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) {
MPDPlayer player = mpd.getMPDPlayer(); MPDPlayer player = mpd.getMPDPlayer();
try { try {
Thread.sleep(500);
int length; int length;
MPDSong song = player.getCurrentSong(); MPDSong song = player.getCurrentSong();
if(song!= null) { if(song!= null) {
length = song.getLength(); length = song.getLength();
int skip = new Random().nextInt(length/2); int skip = new Random().nextInt(length/2)+10;
player.seek(skip); player.seek(skip);
} }
} catch (MPDConnectionException e) { } catch (MPDConnectionException e) {
e.printStackTrace(); e.printStackTrace();
} catch (MPDPlayerException e) { } catch (MPDPlayerException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException ignored) {
}
} }
} }
}; };
synchronized (lockObject) {
new Thread(r).start(); new Thread(r).start();
} }
} }
}
/** /**
* Sets the current mpd volume * Sets the current mpd volume
@ -163,7 +166,7 @@ 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) {
@ -172,13 +175,12 @@ public class MPDController implements IMPDController {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} }
} }
}
}; };
synchronized (lockObject) {
new Thread(r).start(); new Thread(r).start();
} }
} }
}
/** /**
* Clears the current mpd playlist * Clears the current mpd playlist
@ -189,6 +191,7 @@ 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 {
MPDPlaylist playlist = mpd.getMPDPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
playlist.clearPlaylist(); playlist.clearPlaylist();
@ -202,13 +205,12 @@ public class MPDController implements IMPDController {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} }
} }
}
}; };
synchronized (lockObject) {
new Thread(r).start(); new Thread(r).start();
} }
} }
}
private void addToPlayListIfNeeded(final String artist, final String title) { private void addToPlayListIfNeeded(final String artist, final String title) {
MPDDatabase db = mpd.getMPDDatabase(); MPDDatabase db = mpd.getMPDDatabase();