diff --git a/pom.xml b/pom.xml
index e45147d..61abbd7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,6 +14,11 @@
5.10.2
+
+ org.openjfx
+ javafx-media
+ 21.0.4
+
org.openjfx
javafx-controls
diff --git a/src/main/java/sbu/javafx/Launcher.java b/src/main/java/sbu/javafx/Launcher.java
index c288d10..15b4450 100644
--- a/src/main/java/sbu/javafx/Launcher.java
+++ b/src/main/java/sbu/javafx/Launcher.java
@@ -4,5 +4,7 @@ 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..0ac846c 100644
--- a/src/main/java/sbu/javafx/MusicApp.java
+++ b/src/main/java/sbu/javafx/MusicApp.java
@@ -16,7 +16,6 @@ public class MusicApp extends Application {
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Music Player");
stage.setScene(scene);
- //TODO: add icon to the stage
stage.show();
}
diff --git a/src/main/java/sbu/javafx/MusicController.java b/src/main/java/sbu/javafx/MusicController.java
index 5da27a9..408db67 100644
--- a/src/main/java/sbu/javafx/MusicController.java
+++ b/src/main/java/sbu/javafx/MusicController.java
@@ -1,31 +1,160 @@
package sbu.javafx;
+import javafx.collections.FXCollections;
+import javafx.collections.ObservableList;
import javafx.fxml.FXML;
-import javafx.event.ActionEvent;
-import javafx.scene.control.Label;
-import javafx.scene.input.MouseEvent;
-import javafx.scene.layout.VBox;
+import javafx.fxml.FXMLLoader;
+import javafx.scene.Parent;
+import javafx.scene.Scene;
+import javafx.scene.control.*;
+import javafx.scene.control.cell.PropertyValueFactory;
+import javafx.scene.media.Media;
+import javafx.scene.media.MediaPlayer;
+import javafx.stage.Stage;
+
+import java.io.IOException;
public class MusicController {
- // TODO: Define your UI components using the @FXML annotation.
+ @FXML private Label nowPlayingLabel;
+ @FXML private TableView songsTable;
+ @FXML private TableColumn titleCol;
+ @FXML private TableColumn artistCol;
+ @FXML private TableColumn durationCol;
- //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
+ // There is no connection between Song and Button so we should use void
+ @FXML private TableColumn addCol;
+ @FXML private TableColumn playCol;
+
+ private Song nowPlayingSong = null;
+ private final ObservableList playlist = FXCollections.observableArrayList();
+ private MediaPlayer mediaPlayer;
+
+ @FXML
+ public void initialize() {
+ // connect song name,artist name,duration to song class
+ titleCol.setCellValueFactory(new PropertyValueFactory<>("title"));
+ artistCol.setCellValueFactory(new PropertyValueFactory<>("artistName"));
+ durationCol.setCellValueFactory(new PropertyValueFactory<>("duration"));
+
+ // Add button
+ addCol.setCellFactory(param -> new TableCell<>() {
+ private final Button btn = new Button("Add");
+
+ {
+ btn.getStyleClass().add("add-button");
+
+ btn.setOnAction(event -> {
+ Song song = getTableView().getItems().get(getIndex());
+ addToPlaylist(song);
+ });
+ }
+
+ @Override
+ protected void updateItem(Void item, boolean empty) {
+ super.updateItem(item, empty);
+
+ if (empty) {
+ setGraphic(null);
+ setText(null);
+ } else {
+ Song song = getTableView().getItems().get(getIndex());
+
+ if (song.isAdded()) {
+ setGraphic(null);
+ setText("Added ✓");
+ setStyle("-fx-text-fill: #2ecc71; -fx-font-weight: bold;");
+ } else {
+ setGraphic(btn);
+ setText(null);
+ setStyle("");
+ }
+ }
+ }
+ });
+
+ // Play button cell
+ playCol.setCellFactory(param -> new TableCell<>() {
+ private final Button btn = new Button("Play");
+ {
+ btn.setOnAction(e -> {
+ nowPlayingSong = getTableView().getItems().get(getIndex());
+ handlePlayAction();
+ });
+ }
+ @Override
+ protected void updateItem(Void item, boolean empty) {
+ super.updateItem(item, empty);
+ setGraphic(empty ? null : btn);
+ }
+ });
+
+ ObservableList songList = FXCollections.observableArrayList(
+ new Song("Vasiat2", "Naaji", "5:26", "/Musics/Naaji.mp3"),
+ new Song("SogandBe", "Unknown", "1:55", "/Musics/SogandBe.mp3"),
+ new Song("Telo", "Hossein", "3:07", "/Musics/Telo.mp3")
+ );
+ songsTable.setItems(songList);
+
+ }
@FXML
public void handlePlayAction() {
- //Update labels, change button icons, etc.
+ if (nowPlayingSong == null) return;
+
+ try {
+ if (mediaPlayer != null) {
+ mediaPlayer.stop();
+ mediaPlayer.dispose();
+ }
+
+ String filePath = nowPlayingSong.getFilePath();
+ var resource = getClass().getResource(filePath);
+
+ if (resource == null) {
+ System.out.println("Error: File not found in resource path! Path checked: " + filePath);
+ return;
+ }
+
+ String mediaUrl = resource.toExternalForm();
+
+ Media media = new Media(mediaUrl);
+ mediaPlayer = new MediaPlayer(media);
+
+ nowPlayingLabel.setText(nowPlayingSong.getTitle() + " - " + nowPlayingSong.getArtistName());
+ mediaPlayer.play();
+
+ } catch (Exception e) {
+ System.out.println("Error while playing audio file: ");
+ e.printStackTrace();
+ }
}
- //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.
+ public void addToPlaylist(Song song) {
+ if (!playlist.contains(song)) {
+ playlist.add(song);
+ song.setAdded(true);
+ songsTable.refresh();
+ }
}
- // 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.
+ try {
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("Playlist.fxml"));
+ Parent root = loader.load();
+
+ PlaylistController controller = loader.getController();
+ controller.setPlaylist(playlist);
+
+ Stage stage = new Stage();
+ stage.setTitle("Playlist");
+ Scene scene = new Scene(root);
+ stage.setScene(scene);
+ stage.show();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
-}
+}
\ 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..22f8f7d
--- /dev/null
+++ b/src/main/java/sbu/javafx/PlaylistController.java
@@ -0,0 +1,31 @@
+package sbu.javafx;
+
+import javafx.collections.ObservableList;
+import javafx.fxml.FXML;
+import javafx.scene.control.TableColumn;
+import javafx.scene.control.TableView;
+import javafx.scene.control.cell.PropertyValueFactory;
+
+
+public class PlaylistController {
+
+ @FXML public TableView playListTable;
+ @FXML public TableColumn titleCol;
+ @FXML public TableColumn artistCol;
+ @FXML public TableColumn durationCol;
+
+ private ObservableList playlist;
+
+ @FXML
+ public void initialize() {
+
+ titleCol.setCellValueFactory(new PropertyValueFactory<>("title"));
+ artistCol.setCellValueFactory(new PropertyValueFactory<>("artistName"));
+ durationCol.setCellValueFactory(new PropertyValueFactory<>("duration"));
+ }
+
+ public void setPlaylist(ObservableList playlist) {
+ this.playlist = playlist;
+ playListTable.setItems(this.playlist);
+ }
+}
diff --git a/src/main/java/sbu/javafx/Song.java b/src/main/java/sbu/javafx/Song.java
new file mode 100644
index 0000000..b69be34
--- /dev/null
+++ b/src/main/java/sbu/javafx/Song.java
@@ -0,0 +1,42 @@
+package sbu.javafx;
+
+public class Song {
+
+ private String Title;
+ private String ArtistName;
+ private String Duration;
+ private boolean added;
+ private String FilePath;
+
+ public Song(String title, String artistName, String duration, String filePath) {
+ this.Title = title;
+ this.ArtistName = artistName;
+ this.Duration = duration;
+ this.added = false;
+ this.FilePath = filePath;
+ }
+
+ public String getTitle() {
+ return Title;
+ }
+
+ public String getArtistName() {
+ return ArtistName;
+ }
+
+ public String getDuration() {
+ return Duration;
+ }
+
+ public boolean isAdded() {
+ return added;
+ }
+
+ public void setAdded(boolean added) {
+ this.added = added;
+ }
+
+ public String getFilePath() {
+ return FilePath;
+ }
+}
diff --git a/src/main/resources/Musics/Naaji.mp3 b/src/main/resources/Musics/Naaji.mp3
new file mode 100644
index 0000000..64e0ba5
Binary files /dev/null and b/src/main/resources/Musics/Naaji.mp3 differ
diff --git a/src/main/resources/Musics/SogandBe.mp3 b/src/main/resources/Musics/SogandBe.mp3
new file mode 100644
index 0000000..866834d
Binary files /dev/null and b/src/main/resources/Musics/SogandBe.mp3 differ
diff --git a/src/main/resources/Musics/Telo.mp3 b/src/main/resources/Musics/Telo.mp3
new file mode 100644
index 0000000..e394579
Binary files /dev/null and b/src/main/resources/Musics/Telo.mp3 differ
diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml
index 306c8d3..279b776 100644
--- a/src/main/resources/sbu/javafx/App-view.fxml
+++ b/src/main/resources/sbu/javafx/App-view.fxml
@@ -1,8 +1,38 @@
-
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/resources/sbu/javafx/Playlist.fxml b/src/main/resources/sbu/javafx/Playlist.fxml
new file mode 100644
index 0000000..594e663
--- /dev/null
+++ b/src/main/resources/sbu/javafx/Playlist.fxml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file