Player can now auto repeat a single song.

This commit is contained in:
neviyn 2016-03-29 16:04:41 +01:00
parent 6d33c0a6a2
commit ab0680b5ee
2 changed files with 47 additions and 33 deletions

View File

@ -24,6 +24,7 @@ public class GStreamerPlayer implements IPlayer{
private final PlayerCallbackInterface callbackInterface;
private int currentVolume = 100;
private final IPlaylist playlist;
private boolean repeatMode = false; // Repeat the current song?
/**
* Manages GStreamer based playback operations.
@ -47,7 +48,10 @@ public class GStreamerPlayer implements IPlayer{
Thread callbackThing = new Thread(() -> {
try {
Thread.sleep(1000); // Song is about to finish, wait so we don't cut it off early.
playSong(playlist.getNext(currentSong));
if(repeatMode)
playSong(currentSong);
else
playSong(playlist.getNext(currentSong));
} catch (InterruptedException | StartPlayingException e) {
e.printStackTrace();
}
@ -81,44 +85,48 @@ public class GStreamerPlayer implements IPlayer{
playSong(playlist.getActive());
}
private void playSong(Song inputSong) throws StartPlayingException {
if (playBin.getState() == State.PLAYING)
stop();
if(playBin.getState() == State.PAUSED && inputSong == currentSong) {
resume();
return;
}
resetSeek();
playBin.setState(State.READY);
currentSong = inputSong;
playlist.setPlayingSong(currentSong);
File songFile = currentSong.getSongFile();
if (!songFile.exists()) {
playlist.delete(currentSong);
next();
return;
}
playBin.setURI(songFile.toURI());
internalThread = new InternalThread();
thread = new Thread(internalThread, "GSTThread");
setVolume(currentVolume);
playBin.play();
if(playBin.getState() == State.PLAYING) {
thread.start();
while (true) {
if (playBin.queryDuration().getSeconds() > 0 || playBin.queryDuration().getNanoSeconds() > 0) break;
} // Wait for song to actually be playing otherwise queryDuration is always zero
if(callbackInterface != null)
callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds());
}
else{
throw new StartPlayingException(inputSong);
}
}
/**
* Set a source Song file and start playing it via GStreamer.
* @param inputSong Song to play.
*/
public void playSong(Optional<Song> inputSong) throws StartPlayingException {
if (playBin.getState() == State.PLAYING)
stop();
if(inputSong.isPresent()) {
if(playBin.getState() == State.PAUSED && inputSong.get() == currentSong) {
resume();
return;
}
resetSeek();
playBin.setState(State.READY);
currentSong = inputSong.get();
playlist.setPlayingSong(currentSong);
File songFile = currentSong.getSongFile();
if (!songFile.exists()) {
playlist.delete(currentSong);
next();
return;
}
playBin.setURI(songFile.toURI());
internalThread = new InternalThread();
thread = new Thread(internalThread, "GSTThread");
setVolume(currentVolume);
playBin.play();
if(playBin.getState() == State.PLAYING) {
thread.start();
while (true) {
if (playBin.queryDuration().getSeconds() > 0 || playBin.queryDuration().getNanoSeconds() > 0) break;
} // Wait for song to actually be playing otherwise queryDuration is always zero
if(callbackInterface != null)
callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds());
}
else{
throw new StartPlayingException(inputSong.get());
}
playSong(inputSong.get());
}
}
@ -211,4 +219,8 @@ public class GStreamerPlayer implements IPlayer{
public boolean isPlaying(){
return playBin.isPlaying();
}
public void setRepeat(boolean repeatMode) { this.repeatMode = repeatMode; }
public boolean isRepeating(){ return repeatMode; }
}

View File

@ -19,4 +19,6 @@ public interface IPlayer {
int getVolume();
void seek(int position);
boolean isPlaying();
void setRepeat(boolean repeatMode);
boolean isRepeating();
}