extended mpd control to check if track is playing after starting new song

This commit is contained in:
Lucas Pleß 2012-06-28 16:45:01 +02:00
parent f35fc861c7
commit d50ee914ef
2 changed files with 87 additions and 51 deletions

View File

@ -20,6 +20,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
private Thread discoThread; private Thread discoThread;
private boolean shouldStopDisco; private boolean shouldStopDisco;
private boolean gemActivated = false; private boolean gemActivated = false;
private int gemCounter = 0;
private boolean startedHurrySound = false; private boolean startedHurrySound = false;
private char lastInput; private char lastInput;
private boolean sproing; private boolean sproing;
@ -239,6 +240,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
*/ */
@Override @Override
public void timerTick(int tsecondsLeft) { public void timerTick(int tsecondsLeft) {
if(gemCounter>0) gemCounter--;
guiControl.setCountDown(tsecondsLeft); guiControl.setCountDown(tsecondsLeft);
if(tsecondsLeft == 0) { if(tsecondsLeft == 0) {
ircClient.say("timer expired"); ircClient.say("timer expired");
@ -421,6 +423,9 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
// Yes, it makes no sense, but I want have it anyway. For more Spass am Geraet! // Yes, it makes no sense, but I want have it anyway. For more Spass am Geraet!
private void handleGemCommand() { private void handleGemCommand() {
if (!gemActivated) { if (!gemActivated) {
if(gemCounter > 1200) return;
java.util.Random random = new java.util.Random(); java.util.Random random = new java.util.Random();
int scry = random.nextInt(100); int scry = random.nextInt(100);
if (scry >= 99) { if (scry >= 99) {
@ -430,6 +435,8 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
} else { } else {
ircClient.say("Gem Activated"); ircClient.say("Gem Activated");
} }
gemCounter+=100;
} else { } else {
ircClient.say("Gem Deactivated"); ircClient.say("Gem Deactivated");
} }

View File

@ -12,6 +12,7 @@ import org.bff.javampd.exception.MPDPlaylistException;
import org.bff.javampd.objects.MPDSong; import org.bff.javampd.objects.MPDSong;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.sql.SQLOutput;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -43,47 +44,75 @@ public class MPDController implements IMPDController {
System.out.println("playSong: " + artist + " - " + title); System.out.println("playSong: " + artist + " - " + title);
if(mpd != null) { if(mpd != null) {
/*Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) { */ synchronized (mpd) {
addToPlayListIfNeeded(artist, title); addToPlayListIfNeeded(artist, title);
try { doPlaySong(artist,title);
Thread.sleep(200); }
MPDPlaylist playlist = mpd.getMPDPlaylist();
for(MPDSong song: playlist.getSongList()) {
if(song.getArtist() != null && song.getTitle() != null) {
if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) &&
song.getTitle().toLowerCase().equals(title.toLowerCase())) {
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());
} catch (InterruptedException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
/*}
} }
}; };
new Thread(r).start(); */ //new Thread(r).start();
r.run();
} }
} }
private void doPlaySong(final String artist, final String title) {
try {
MPDPlaylist playlist = mpd.getMPDPlaylist();
MPDPlayer player = mpd.getMPDPlayer();
int tries = 3;
while(tries>0) {
for(MPDSong song: playlist.getSongList()) {
if(checkSong(song, artist, title)) {
System.out.println("MPD: stopping, playing, go...");
player.stop();
player.playId(song);
break;
} else {
System.out.println("MPD: track title or artist is null or not correct," + song.toString());
}
}
// now check, if it is playing
MPDSong song = player.getCurrentSong();
if(checkSong(song, artist, title)) {
System.out.println("MPD: song is correctly playing");
return;
} else {
System.out.println("MPD: whilst checking current song: track title or artist is null or not correct " + song.toString());
}
System.out.println("MPD: next try");
tries--;
}
Logger.sLog("MPD error: track not in playlist or not found... cannot play");
} catch (MPDConnectionException e) {
Logger.sLog("MPD error: " + e.getMessage());
} catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage());
}
}
private boolean checkSong(final MPDSong song, final String artist, final String title) {
if(song.getArtist() != null && song.getTitle() != null) {
if(song.getArtist().getName().toLowerCase().contentEquals(artist.toLowerCase()) &&
song.getTitle().toLowerCase().contentEquals(title.toLowerCase())) {
return true;
}
}
return false;
}
/** /**
* Add a song to current mpd playlist * Add a song to current mpd playlist
* @param artist Artist of the track to play * @param artist Artist of the track to play
@ -92,10 +121,10 @@ 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() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) { */ synchronized (mpd) {
MPDDatabase db = mpd.getMPDDatabase(); MPDDatabase db = mpd.getMPDDatabase();
MPDPlaylist playlist = mpd.getMPDPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
@ -115,26 +144,25 @@ public class MPDController implements IMPDController {
} catch (MPDPlaylistException e) { } catch (MPDPlaylistException e) {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} }
/*} }
} }
}; };
new Thread(r).start(); */ //new Thread(r).start();
r.run();
} }
} }
@Override @Override
public void skipRandomStart() { public void skipRandomStart() {
if(mpd != null) { if(mpd != null) {
/*Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) {*/ 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) {
@ -146,13 +174,13 @@ public class MPDController implements IMPDController {
e.printStackTrace(); e.printStackTrace();
} catch (MPDPlayerException e) { } catch (MPDPlayerException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException ignored) {
} }
/*} }
} }
}; };
new Thread(r).start(); */ //new Thread(r).start();
r.run();
} }
} }
@ -163,10 +191,10 @@ 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() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) {*/ synchronized (mpd) {
try { try {
mpd.getMPDPlayer().setVolume(volume); mpd.getMPDPlayer().setVolume(volume);
} catch (MPDConnectionException e) { } catch (MPDConnectionException e) {
@ -174,11 +202,11 @@ public class MPDController implements IMPDController {
} catch (MPDPlayerException e) { } catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} }
/*} }
} }
}; };
//new Thread(r).start();
new Thread(r).start();*/ r.run();
} }
} }
@ -188,10 +216,10 @@ public class MPDController implements IMPDController {
@Override @Override
public void clearPlaylist() { public void clearPlaylist() {
if(mpd != null) { if(mpd != null) {
/*Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
synchronized (mpd) {*/ synchronized (mpd) {
try { try {
MPDPlaylist playlist = mpd.getMPDPlaylist(); MPDPlaylist playlist = mpd.getMPDPlaylist();
playlist.clearPlaylist(); playlist.clearPlaylist();
@ -204,11 +232,12 @@ public class MPDController implements IMPDController {
} catch (MPDPlayerException e) { } catch (MPDPlayerException e) {
Logger.sLog("MPD error: " + e.getMessage()); Logger.sLog("MPD error: " + e.getMessage());
} }
/*} }
} }
}; };
new Thread(r).start(); */ //new Thread(r).start();
r.run();
} }
} }