diff --git a/pom.xml b/pom.xml index e45147d..7079eb9 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ UTF-8 -5.10.2 + 5.10.2 @@ -24,8 +24,15 @@ javafx-fxml 21 + - + org.openjfx + javafx-media + 21 + + + + org.junit.jupiter junit-jupiter-api ${junit.version} diff --git a/src/main/java/sbu/javafx/MusicController.java b/src/main/java/sbu/javafx/MusicController.java index a9e1464..6fc1e26 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -1,16 +1,15 @@ package sbu.javafx; import javafx.fxml.FXML; -import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ListView; -import javafx.scene.input.MouseEvent; -import javafx.scene.layout.VBox; +import javafx.scene.media.Media; +import javafx.scene.media.MediaPlayer; import javafx.stage.Stage; - +import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -19,12 +18,16 @@ public class MusicController { // TODO: Define your UI components using the @FXML annotation. @FXML private ListView lvSongs; @FXML private Label lblStatus; + @FXML private Button btnPlayStop; + @FXML private Button btnViewPlaylist; + + private MediaPlayer mediaPlayer; private ArrayList playlist = new ArrayList<>(); private String[] songsArray = {"Arcade Duncan Laurence 3.07", "How i love you Engelbert 4.20", - "Memmories Conan Gray 4.09", + "Memories Conan Gray 4.09", "Night Changes One Direction 3.47", "Careless Whisper George Michael 5.00" }; @@ -55,7 +58,55 @@ public class MusicController { return; } - lblStatus.setText(" is playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex())); + String filePath = "src/main/resources/sbu.javafx/songs/" + songName(lvSongs.getSelectionModel().getSelectedIndex()) + ".mp3"; + File file = new File(filePath); + + boolean isPlayingOrPaused = (mediaPlayer != null && + (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING || + mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED)); + + if (isPlayingOrPaused) + { + if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) + { + mediaPlayer.pause(); + btnPlayStop.setText("Paused ⏸"); + lblStatus.setText("Playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex())); + return; + } + else if (mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED) + { + mediaPlayer.play(); + btnPlayStop.setText("Play ▶"); + lblStatus.setText("Paused"); + return; + } + } + + if (mediaPlayer != null) + { + mediaPlayer.dispose(); + mediaPlayer = null; + } + + try { + Media media = new Media(file.toURI().toString()); + mediaPlayer = new MediaPlayer(media); + + mediaPlayer.setOnEndOfMedia(new Runnable() { + @Override + public void run() { + mediaPlayer.stop(); + btnPlayStop.setText("Play ▶"); + } + }); + + mediaPlayer.play(); + btnPlayStop.setText("Paused ⏸"); + lblStatus.setText("Playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex())); + } catch (Exception e) { + throw new RuntimeException(e); + } } //TODO: Logic to add a specific song to the user's playlist. @@ -74,7 +125,7 @@ public class MusicController { playlist.add(selectedSong); lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " added to playList"); } - else lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " is already exists"); + else lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " already exists"); } // TODO:This method should open a new window or change the current scene to display the "Playlist" page. diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml index 3a1c59d..c1c0bf6 100644 --- a/src/main/resources/sbu/javafx/App-view.fxml +++ b/src/main/resources/sbu/javafx/App-view.fxml @@ -7,7 +7,7 @@ -