Fixed hard freeze that occurred if gstreamer never started to actually play a song after calling the Player play method.

This commit is contained in:
neviyn 2016-03-10 16:56:05 +00:00
parent 9164dff257
commit daedca9bf6
3 changed files with 38 additions and 12 deletions

View File

@ -49,7 +49,7 @@ class Player {
* Set a source Song file and start playing it via GStreamer.
* @param inputSong Song to play.
*/
public void playSong(Optional<Song> inputSong){
public void playSong(Optional<Song> inputSong) throws StartPlayingException {
if (playBin.getState() == State.PLAYING)
stop();
if(inputSong.isPresent()) {
@ -71,12 +71,17 @@ class Player {
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
callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds());
}
else{
throw new StartPlayingException(inputSong.get());
}
}
}
/**

View File

@ -162,12 +162,21 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
playlistTableModel.setPlayingRow(index);
}
private void playSong(Optional<Song> song){
try {
player.playSong(song);
} catch (StartPlayingException e) {
JOptionPane.showMessageDialog(mainPanel, "Failed to play " + song.get().toString() + ".\n" +
"If file path contains non-ASCII characters, please restart the application with UTF-8 encoding.");
}
}
/**
* Get the next song in the playlist and dispatch it to the Player to be played.
*/
public void playNextSong() {
SwingUtilities.invokeLater(() -> seekBar.setValue(0));
player.playSong(playlistTableModel.nextSong(player.getCurrentSong()));
playSong(playlistTableModel.nextSong(player.getCurrentSong()));
}
/**
@ -175,7 +184,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
*/
public void playPreviousSong() {
SwingUtilities.invokeLater(() -> seekBar.setValue(0));
player.playSong(playlistTableModel.previous(player.getCurrentSong()));
playSong(playlistTableModel.previous(player.getCurrentSong()));
}
/**
@ -355,9 +364,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
playButton.addActionListener(e -> {
if (playList.getRowCount() > 0) {
if (playList.getSelectedRowCount() > 0)
player.playSong(playlistTableModel.getSong(playList.getSelectedRow()));
playSong(playlistTableModel.getSong(playList.getSelectedRow()));
else
player.playSong(playlistTableModel.getFirst());
playSong(playlistTableModel.getFirst());
}
});
toolBar.add(playButton);
@ -545,9 +554,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
else{
if (playList.getRowCount() > 0) {
if (playList.getSelectedRowCount() > 0)
player.playSong(playlistTableModel.getSong(playList.getSelectedRow()));
playSong(playlistTableModel.getSong(playList.getSelectedRow()));
else
player.playSong(playlistTableModel.getFirst());
playSong(playlistTableModel.getFirst());
}
}
break;

View File

@ -0,0 +1,12 @@
package musicplayer;
import musicplayer.model.Song;
public class StartPlayingException extends Exception {
private Song song;
public StartPlayingException(Song song){
this.song = song;
}
}