diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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/Launcher.java b/src/main/java/sbu/javafx/Launcher.java index c288d10..a3ffd1f 100644 --- a/src/main/java/sbu/javafx/Launcher.java +++ b/src/main/java/sbu/javafx/Launcher.java @@ -4,5 +4,6 @@ import javafx.application.Application; public class Launcher{ public static void main(String[] args) { - // TODO: Launch the JavaFX application by calling Application.launch(Main.class) } + Application.launch(MusicApp.class); + } } diff --git a/src/main/java/sbu/javafx/MusicApp.java b/src/main/java/sbu/javafx/MusicApp.java index a50badc..20c731a 100644 --- a/src/main/java/sbu/javafx/MusicApp.java +++ b/src/main/java/sbu/javafx/MusicApp.java @@ -4,7 +4,8 @@ import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; - +import javafx.scene.image.Image; +import java.awt.*; import java.io.IOException; public class MusicApp extends Application { @@ -14,9 +15,16 @@ public class MusicApp extends Application { { FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml")); Scene scene = new Scene(fxmlLoader.load()); + + String lightStyle = getClass().getResource("light.css").toExternalForm(); + scene.getStylesheets().add(lightStyle); + stage.setTitle("Music Player"); stage.setScene(scene); - //TODO: add icon to the stage + + Image icon = new Image(getClass().getResourceAsStream("icon.png")); + stage.getIcons().add(icon); + stage.show(); } diff --git a/src/main/java/sbu/javafx/MusicController.java b/src/main/java/sbu/javafx/MusicController.java index 5da27a9..ce8d4cc 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -1,31 +1,179 @@ 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.input.MouseEvent; -import javafx.scene.layout.VBox; +import javafx.scene.control.ListView; +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; 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 btnTheme; + + private int previousSongIndex = -1; + + private MediaPlayer mediaPlayer; + + private ArrayList playlist = new ArrayList<>(); + + private String[] songsArray = {"Arcade Duncan Laurence 3.07", + "How i love you Engelbert 4.20", + "Memories Conan Gray 4.09", + "Night Changes One Direction 3.47", + "Careless Whisper George Michael 5.00" + }; + + @FXML public void initialize() { lvSongs.getItems().setAll(songsArray); } //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing. + public String songName(int a) + { + String songName = ""; + if (a == 0) songName = "Arcade"; + else if (a == 1) songName = "How i love you"; + else if (a == 2) songName = "Memories"; + else if (a == 3) songName = "Night Changes"; + else songName = "Careless Whisper"; + + return songName; + } + @FXML - public void handlePlayAction() { - //Update labels, change button icons, etc. + public void handlePlayAction() + { + String selectedSong = lvSongs.getSelectionModel().getSelectedItem(); + int newSongIndex = lvSongs.getSelectionModel().getSelectedIndex(); + boolean isNewSong = (newSongIndex != previousSongIndex); + + if (selectedSong == null) + { + lblStatus.setText("Please choose a song"); + return; + } + + if (isNewSong) + { + if (mediaPlayer != null) + { + mediaPlayer.dispose(); + mediaPlayer = null; + } + + String filePath = "src/main/resources/sbu/javafx/songs/" + songName(lvSongs.getSelectionModel().getSelectedIndex()) + ".mp3"; + File file = new File(filePath); + + 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 ▶"); + lblStatus.setText("Finished"); + } + }); + + mediaPlayer.play(); + btnPlayStop.setText("Paused ⏸"); + lblStatus.setText("Playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex())); + previousSongIndex = newSongIndex; + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + else + { + if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) + { + mediaPlayer.pause(); + btnPlayStop.setText("Play ▶"); + lblStatus.setText("Paused"); + } + else if (mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED) + { + mediaPlayer.play(); + btnPlayStop.setText("Pause ⏸"); + lblStatus.setText("Playing: " + songName(lvSongs.getSelectionModel().getSelectedIndex())); + } + else + { + mediaPlayer.play(); + btnPlayStop.setText("Pause ⏸"); + lblStatus.setText("Playing: " + songName(lvSongs.getSelectionModel().getSelectedIndex())); + } + } } //TODO: Logic to add a specific song to the user's playlist. - public void addToPlaylist(String songName) { - //Add the song to a list or update a ListView. + @FXML + public void addToPlaylist() + { + String selectedSong = lvSongs.getSelectionModel().getSelectedItem(); + if (selectedSong == null) + { + lblStatus.setText("Please choose a song"); + return; + } + + if (!playlist.contains(selectedSong)) + { + playlist.add(selectedSong); + lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " added to playList"); + } + 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. - public void openPlaylistWindow() { - //Create a new stage and show the playlist UI. + public void openPlaylistWindow() + { + FXMLLoader loader = new FXMLLoader(getClass().getResource("Playlist-view.fxml")); + Scene scene = null; + try { + scene = new Scene(loader.load()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + PlayListController controller = loader.getController(); + controller.setPlaylist(playlist); + + Stage stage = new Stage(); + stage.setTitle("Playlist"); + stage.setScene(scene); + stage.show(); } -} + + @FXML + private void toggleTheme() + { + if (!btnTheme.getText().contains("Light")) + { + btnTheme.getScene().getStylesheets().clear(); + btnTheme.getScene().getStylesheets().add(getClass().getResource("style.css").toExternalForm()); + btnTheme.setText("Light Mode"); + } + else + { + btnTheme.getScene().getStylesheets().clear(); + btnTheme.getScene().getStylesheets().add(getClass().getResource("light.css").toExternalForm()); + btnTheme.setText("Dark Mode"); + } + } +} \ No newline at end of file diff --git a/src/main/java/sbu/javafx/PlayListController.java b/src/main/java/sbu/javafx/PlayListController.java new file mode 100644 index 0000000..4d5602b --- /dev/null +++ b/src/main/java/sbu/javafx/PlayListController.java @@ -0,0 +1,14 @@ +package sbu.javafx; + +import java.util.ArrayList; +import javafx.fxml.FXML; +import javafx.scene.control.ListView; + +public class PlayListController +{ + @FXML private ListView lvPlaylist; + public void setPlaylist(ArrayList playlist) + { + lvPlaylist.getItems().addAll(playlist); + } +} \ No newline at end of file diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml index 306c8d3..c3a0f6d 100644 --- a/src/main/resources/sbu/javafx/App-view.fxml +++ b/src/main/resources/sbu/javafx/App-view.fxml @@ -1,8 +1,30 @@ + + + - + - + + + + + - +