From ab0680b5ee9abb0e3f78cb11e1259762061adb60 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 29 Mar 2016 16:04:41 +0100 Subject: [PATCH] Player can now auto repeat a single song. --- .../musicplayer/player/GStreamerPlayer.java | 78 +++++++++++-------- src/main/java/musicplayer/player/IPlayer.java | 2 + 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/main/java/musicplayer/player/GStreamerPlayer.java b/src/main/java/musicplayer/player/GStreamerPlayer.java index 8968091..13b0f8b 100644 --- a/src/main/java/musicplayer/player/GStreamerPlayer.java +++ b/src/main/java/musicplayer/player/GStreamerPlayer.java @@ -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 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; } } diff --git a/src/main/java/musicplayer/player/IPlayer.java b/src/main/java/musicplayer/player/IPlayer.java index 83b4ae8..28d14fa 100644 --- a/src/main/java/musicplayer/player/IPlayer.java +++ b/src/main/java/musicplayer/player/IPlayer.java @@ -19,4 +19,6 @@ public interface IPlayer { int getVolume(); void seek(int position); boolean isPlaying(); + void setRepeat(boolean repeatMode); + boolean isRepeating(); }