Player can now auto repeat a single song.
This commit is contained in:
parent
6d33c0a6a2
commit
ab0680b5ee
@ -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,7 +48,10 @@ 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.
|
||||||
playSong(playlist.getNext(currentSong));
|
if(repeatMode)
|
||||||
|
playSong(currentSong);
|
||||||
|
else
|
||||||
|
playSong(playlist.getNext(currentSong));
|
||||||
} catch (InterruptedException | StartPlayingException e) {
|
} catch (InterruptedException | StartPlayingException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -81,44 +85,48 @@ public class GStreamerPlayer implements IPlayer{
|
|||||||
playSong(playlist.getActive());
|
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.
|
* Set a source Song file and start playing it via GStreamer.
|
||||||
* @param inputSong Song to play.
|
* @param inputSong Song to play.
|
||||||
*/
|
*/
|
||||||
public void playSong(Optional<Song> inputSong) throws StartPlayingException {
|
public void playSong(Optional<Song> inputSong) throws StartPlayingException {
|
||||||
if (playBin.getState() == State.PLAYING)
|
|
||||||
stop();
|
|
||||||
if(inputSong.isPresent()) {
|
if(inputSong.isPresent()) {
|
||||||
if(playBin.getState() == State.PAUSED && inputSong.get() == currentSong) {
|
playSong(inputSong.get());
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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; }
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user