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. * 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){ public void playSong(Optional<Song> inputSong) throws StartPlayingException {
if (playBin.getState() == State.PLAYING) if (playBin.getState() == State.PLAYING)
stop(); stop();
if(inputSong.isPresent()) { if(inputSong.isPresent()) {
@ -71,11 +71,16 @@ class Player {
thread = new Thread(internalThread, "GSTThread"); thread = new Thread(internalThread, "GSTThread");
setVolume(currentVolume); setVolume(currentVolume);
playBin.play(); playBin.play();
thread.start(); if(playBin.getState() == State.PLAYING) {
while (true) { thread.start();
if (playBin.queryDuration().getSeconds() > 0 || playBin.queryDuration().getNanoSeconds() > 0) break; while (true) {
} // Wait for song to actually be playing otherwise queryDuration is always zero if (playBin.queryDuration().getSeconds() > 0 || playBin.queryDuration().getNanoSeconds() > 0) break;
callbackInterface.setSeekBarDuration((int) playBin.queryDuration().toSeconds()); } // 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); 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. * Get the next song in the playlist and dispatch it to the Player to be played.
*/ */
public void playNextSong() { public void playNextSong() {
SwingUtilities.invokeLater(() -> seekBar.setValue(0)); 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() { public void playPreviousSong() {
SwingUtilities.invokeLater(() -> seekBar.setValue(0)); 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 -> { playButton.addActionListener(e -> {
if (playList.getRowCount() > 0) { if (playList.getRowCount() > 0) {
if (playList.getSelectedRowCount() > 0) if (playList.getSelectedRowCount() > 0)
player.playSong(playlistTableModel.getSong(playList.getSelectedRow())); playSong(playlistTableModel.getSong(playList.getSelectedRow()));
else else
player.playSong(playlistTableModel.getFirst()); playSong(playlistTableModel.getFirst());
} }
}); });
toolBar.add(playButton); toolBar.add(playButton);
@ -545,9 +554,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
else{ else{
if (playList.getRowCount() > 0) { if (playList.getRowCount() > 0) {
if (playList.getSelectedRowCount() > 0) if (playList.getSelectedRowCount() > 0)
player.playSong(playlistTableModel.getSong(playList.getSelectedRow())); playSong(playlistTableModel.getSong(playList.getSelectedRow()));
else else
player.playSong(playlistTableModel.getFirst()); playSong(playlistTableModel.getFirst());
} }
} }
break; 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;
}
}