private void BtPlayPauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtPlayPauseActionPerformed if(musicPlayer.getState() == MusicPlayerState.PLAYING){ musicPlayer.pause(); } else{ try{ musicPlayer.play(); } catch(MediaException ex){ JOptionPane.showMessageDialog(this,"Error al reproducir el archivo: "+ex.getMessage(), "Error de reproducción", JOptionPane.ERROR_MESSAGE); //songModel.endSong(); //setStoppedView(); //setError(); } } }
private void playBGM(String soundFileName) { Media sound = new Media(new File(soundFileName).toURI().toString()); try { bgm = new MediaPlayer(sound); bgm.setVolume(bgmVolume); bgm.play(); }catch (MediaException e) { new CustomAlert(AlertType.ERROR, "Sound Manager Error").show(); } }
private void playSoundEffect(String soundFileName) { Media sound = new Media(new File(soundFileName).toURI().toString()); try { MediaPlayer soundEffect = new MediaPlayer(sound); soundEffect.setVolume(soundEffectVolumn); soundEffect.play(); soundPlayers.add(soundEffect); }catch (MediaException e) { new CustomAlert(AlertType.ERROR, "Sound Manager Error").show(); } }
public AudioPlayer(final Path path) throws MediaException { super(PLAY_ICON); this.getStyleClass().addAll("dynamic", "play-stop"); mediaPlayer = new MediaPlayer(new Media(path.toUri().toString())); this.setOnAction(event -> { if (playing) stop(); else play(); }); }
@Override protected GridDecisionOption buildOption(final Song song) { GridDecisionOption optionGrid = new GridDecisionOption(); Label label = new Label("Song"); label.getStyleClass().add("italic"); optionGrid.add(label, 0, 0, 2, 1); GridPane.setMargin(label, new Insets(0, 0, SPACING / 2, 0)); addToGrid(optionGrid, "Title", song.getTitle(), 1); String bitRate = song.getBitRate() + " kbit/s"; if (song.isVariableBitRate()) bitRate += " (variable)"; addToGrid(optionGrid, "Bit rate", bitRate, 2); addToGrid(optionGrid, "Artist", song.getArtistName(), 3); addToGrid(optionGrid, "Album", song.getAlbumTitle(), 4); // TODO: Show format (and maybe file extension) Path songPath = song.getPath(); if (songPath != null) { Button openDir = new Button("Open directory"); openDir.setOnAction(event -> directoryOpener.accept(songPath.getParent().toString())); // TODO: Preselect song file optionGrid.add(openDir, 0, 5, 2, 1); centerAndPad(openDir); try { AudioPlayer player = new AudioPlayer(songPath); optionGrid.add(player, 0, 6, 2, 1); centerAndPad(player); // XXX: Does in stop when continuing wizard? (no) } catch (MediaException e) { System.err.println("Failed to create play button for song: " + songPath); e.printStackTrace(); } } return optionGrid; }
/** * Method to convert the given media file into a {@link FriendlyMediaPlayer}. * @param mediaFile the path to the media. * @return the {@link FriendlyMediaPlayer} or null if invalid. */ public FriendlyMediaPlayer convert( String mediaFile ) { Media media = extractMedia( mediaFile ); if ( media == null ) { return null; } try { return new FriendlyMediaPlayer( media ); } catch ( MediaException exception ) { //should use digest, not tested for ease return null; } }
/** * Method to safely extract the {@link Media}, handling any failures appropriately. * @param mediaFile the media file path. * @return the {@link Media}, or null. */ private Media extractMedia( String mediaFile ) { if ( mediaFile == null ) { return null; } try { return new Media( new File( mediaFile ).toURI().toString() ); } catch ( MediaException exception ){ //should use digest return null; } }
/** * Precondition: PlayQueue contains tracks * Postcondition: Playback state reversed */ public static void toggle() { try { if (currentPlayback != null) { if (currentPlayback.getStatus() == MediaPlayer.Status.PLAYING) { pause(); } else { play(); } } } catch (MediaException e) { System.out.println("Invalid media type."); } }
/** * Precondition: There is a current track being played * Postcondition: Track is now paused */ public static void pause() { try { if(currentPlayback != null) { currentPlayback.pause(); } } catch (MediaException e) { System.out.println("Invalid media type."); } }
/** * Precondition: There is a current track not being played * Postcondition: Track is now playing */ public static void play() { try { if(currentPlayback != null) { currentPlayback.play(); System.out.println("Now playing: " + currentTrack); } } catch (MediaException e) { System.out.println("Invalid media type."); } }
/** * Precondition: There is a current track * Postcondition: Track is now paused at 0:00 */ public static void stopPlayback() { try { if(currentPlayback != null) { currentPlayback.stop(); } } catch (MediaException e) { System.out.println("Invalid media type."); } }
/** * Precondition: There is a current track * Postcondition: The next track in the queue is now playing */ public static void next() { try { if(currentPlayback != null && nextTrack != null) { server.onTrackChange(); setCurrentTrack(nextTrack); play(); } } catch (MediaException e) { System.out.println("Invalid media type."); } }
/** * Precondition: There is a current track * Postcondition: The previous track in the queue is now playing */ public static void prev() { try { if(currentPlayback != null && prevTrack != null) { setCurrentTrack(prevTrack); play(); } } catch (MediaException e) { System.out.println("Invalid media type."); } }
private void onMediaPlayerError(MediaException ex, MediaInfo info) { log("Reading Error : " + info.getDescription()); if (settings.debug.get() && (ex != null)) { log("Error Message : " + ex.getLocalizedMessage()); log(Arrays.toString(ex.getStackTrace())); } info.mediaStatusProperty().setValue(MediaStatus.MEDIA_ERROR); }
@Test(expected = MediaException.class) public void loadNotSound() throws IOException, InvalidMidiDataException{ SoundCache.getInstance().loadSound("notEffect", "midi/title.mid", SoundType.EFFECT); }
@Override public void start(Stage primaryStage) { File fileForMusic = new File("C:\\Users\\Eric\\Music\\New stuff\\Prism.mp3"); if (!fileForMusic.exists()) { System.out.println("File does not exist"); return; } System.out.println(fileForMusic.toURI().toString()); // Create the m Media song = new Media(fileForMusic.toURI().toString()); final MediaPlayer player = new MediaPlayer(song); player.setAutoPlay(true); player.setOnError(new Runnable() { @Override public void run() { System.out.println("ERROR"); MediaException e = player.getError(); e.printStackTrace(); } }); System.out.println(player.getStatus()); System.out.println(player); System.out.println(player.getMedia()); player.setOnReady(new Runnable() { @Override public void run() { System.out.println("Player running?"); player.play(); } }); // Add a mediaView, to display the media. Its necessary ! // This mediaView is added to a Pane MediaView mediaView = new MediaView(player); mediaView.setMediaPlayer(player); // Setup the Java FX Scene primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); root.getChildren().add(mediaView); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); }