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 final PlayerCallbackInterface callbackInterface;
private int currentVolume = 100; private int currentVolume = 100;
private final IPlaylist playlist; private final IPlaylist playlist;
private boolean repeatMode = false; // Repeat the current song?
/** /**
* Manages GStreamer based playback operations. * Manages GStreamer based playback operations.
@ -47,6 +48,9 @@ public class GStreamerPlayer implements IPlayer{
Thread callbackThing = new Thread(() -> { Thread callbackThing = new Thread(() -> {
try { try {
Thread.sleep(1000); // Song is about to finish, wait so we don't cut it off early. Thread.sleep(1000); // Song is about to finish, wait so we don't cut it off early.
if(repeatMode)
playSong(currentSong);
else
playSong(playlist.getNext(currentSong)); playSong(playlist.getNext(currentSong));
} catch (InterruptedException | StartPlayingException e) { } catch (InterruptedException | StartPlayingException e) {
e.printStackTrace(); e.printStackTrace();
@ -81,21 +85,16 @@ public class GStreamerPlayer implements IPlayer{
playSong(playlist.getActive()); playSong(playlist.getActive());
} }
/** private void playSong(Song inputSong) throws StartPlayingException {
* 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) if (playBin.getState() == State.PLAYING)
stop(); stop();
if(inputSong.isPresent()) { if(playBin.getState() == State.PAUSED && inputSong == currentSong) {
if(playBin.getState() == State.PAUSED && inputSong.get() == currentSong) {
resume(); resume();
return; return;
} }
resetSeek(); resetSeek();
playBin.setState(State.READY); playBin.setState(State.READY);
currentSong = inputSong.get(); currentSong = inputSong;
playlist.setPlayingSong(currentSong); playlist.setPlayingSong(currentSong);
File songFile = currentSong.getSongFile(); File songFile = currentSong.getSongFile();
if (!songFile.exists()) { if (!songFile.exists()) {
@ -117,9 +116,18 @@ public class GStreamerPlayer implements IPlayer{
callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds()); callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds());
} }
else{ else{
throw new StartPlayingException(inputSong.get()); 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(inputSong.isPresent()) {
playSong(inputSong.get());
}
} }
/** /**
@ -211,4 +219,8 @@ public class GStreamerPlayer implements IPlayer{
public boolean isPlaying(){ public boolean isPlaying(){
return playBin.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(); int getVolume();
void seek(int position); void seek(int position);
boolean isPlaying(); boolean isPlaying();
void setRepeat(boolean repeatMode);
boolean isRepeating();
} }