From daedca9bf684b34e29b3e60a3e8ebde82045d4ae Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Thu, 10 Mar 2016 16:56:05 +0000 Subject: [PATCH] Fixed hard freeze that occurred if gstreamer never started to actually play a song after calling the Player play method. --- src/main/java/musicplayer/Player.java | 17 +++++++++------ src/main/java/musicplayer/PlayerGUI.java | 21 +++++++++++++------ .../musicplayer/StartPlayingException.java | 12 +++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 src/main/java/musicplayer/StartPlayingException.java diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index 1570b0b..d4747ba 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -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 inputSong){ + public void playSong(Optional inputSong) throws StartPlayingException { if (playBin.getState() == State.PLAYING) stop(); if(inputSong.isPresent()) { @@ -71,11 +71,16 @@ class Player { thread = new Thread(internalThread, "GSTThread"); setVolume(currentVolume); playBin.play(); - 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()); + 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()); + } } } diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index 1ca37cd..41fc35d 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -162,12 +162,21 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf playlistTableModel.setPlayingRow(index); } + private void playSong(Optional 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; diff --git a/src/main/java/musicplayer/StartPlayingException.java b/src/main/java/musicplayer/StartPlayingException.java new file mode 100644 index 0000000..3408877 --- /dev/null +++ b/src/main/java/musicplayer/StartPlayingException.java @@ -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; + } +}