diff --git a/pom.xml b/pom.xml index e45147d..5a781a9 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,13 @@ junit-jupiter-engine ${junit.version} test - + + + org.openjfx + javafx-media + 21 + + diff --git a/src/main/java/sbu/javafx/Launcher.java b/src/main/java/sbu/javafx/Launcher.java index c288d10..57e8d02 100644 --- a/src/main/java/sbu/javafx/Launcher.java +++ b/src/main/java/sbu/javafx/Launcher.java @@ -2,7 +2,9 @@ package sbu.javafx; import javafx.application.Application; -public class Launcher{ +public class Launcher { public static void main(String[] args) { - // TODO: Launch the JavaFX application by calling Application.launch(Main.class) } -} + + Application.launch(MusicApp.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/sbu/javafx/MusicApp.java b/src/main/java/sbu/javafx/MusicApp.java index a50badc..2124df7 100644 --- a/src/main/java/sbu/javafx/MusicApp.java +++ b/src/main/java/sbu/javafx/MusicApp.java @@ -3,21 +3,40 @@ package sbu.javafx; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; +import javafx.scene.image.Image; import javafx.stage.Stage; import java.io.IOException; +import java.util.Objects; public class MusicApp extends Application { @Override - public void start(Stage stage) throws IOException - { + public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml")); Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("Music Player"); + + + try { + + var iconUrl = MusicApp.class.getResource("/icon.png"); + if (iconUrl != null) { + stage.getIcons().add(new Image(iconUrl.toExternalForm())); + } else { + System.out.println("فایل آیکون در پوشه resources پیدا نشد!"); + } + } catch (Exception e) { + e.printStackTrace(); + } + try { + Image icon = new Image(Objects.requireNonNull(MusicApp.class.getResourceAsStream("/icon.png"))); + stage.getIcons().add(icon); + } catch (Exception e) { + System.out.println("Icon not found. Please add icon.png to resources."); + } + 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..25e2672 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -1,31 +1,97 @@ package sbu.javafx; -import javafx.fxml.FXML; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.Scene; +import javafx.scene.control.Button; import javafx.scene.control.Label; -import javafx.scene.input.MouseEvent; +import javafx.scene.control.ListView; import javafx.scene.layout.VBox; +import javafx.stage.Stage; +import javafx.scene.media.Media; +import javafx.scene.media.MediaPlayer; +import javafx.util.Duration; public class MusicController { - // TODO: Define your UI components using the @FXML annotation. - - //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing. + @FXML private Label statusLabel; + private static final ObservableList playlist = FXCollections.observableArrayList(); + private MediaPlayer mediaPlayer; @FXML - public void handlePlayAction() { - //Update labels, change button icons, etc. + public void handlePlayAction(ActionEvent event) { + + Button clickedButton = (Button) event.getSource(); + String songName = (String) clickedButton.getUserData(); + statusLabel.setText("Now Playing: " + songName); + + if (mediaPlayer != null) { + mediaPlayer.stop(); + } + + // پیدا کردن فایل آهنگ و پخش آن + String fileName = mapSongToFileName(songName); + try { + // نکته: فایل‌ها باید در پوشه resources باشند + String path = getClass().getResource("/" + fileName).toExternalForm(); + Media media = new Media(path); + mediaPlayer = new MediaPlayer(media); + + // فراخوانی متد تنظیم تایمر + setupTimer(); + + mediaPlayer.play(); + } catch (Exception e) { + statusLabel.setText("خطا در پخش فایل"); + 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. + private void setupTimer() { + mediaPlayer.setOnReady(() -> { + Duration duration = mediaPlayer.getTotalDuration(); + String time = String.format("%d:%02d", + (int) duration.toMinutes(), + (int) duration.toSeconds() % 60); + + + String songName = statusLabel.getText().replace("Now Playing: ", ""); + statusLabel.setText(songName + " (" + time + ")"); + }); } - // TODO:This method should open a new window or change the current scene to display the "Playlist" page. + @FXML + public void handleAddAction(ActionEvent event) { + Button clickedButton = (Button) event.getSource(); + String songName = (String) clickedButton.getUserData(); - public void openPlaylistWindow() { - //Create a new stage and show the playlist UI. + if (songName != null) { + playlist.add(songName); + statusLabel.setText("وضعیت: " + songName + " به لیست اضافه شد."); + } } -} + + @FXML + public void openPlaylistWindow(ActionEvent event) { + Stage playlistStage = new Stage(); + ListView listView = new ListView<>(playlist); + listView.setStyle("-fx-background-color: #1e1e2e; -fx-control-inner-background: #1e1e2e; -fx-text-fill: white;"); + + VBox root = new VBox(listView); + root.setStyle("-fx-background-color: #1e1e2e; -fx-padding: 10;"); + + playlistStage.setScene(new Scene(root, 300, 400)); + playlistStage.setTitle("لیست پخش من"); + playlistStage.show(); + } + + private String mapSongToFileName(String songName) { + if (songName == null) return "mp3.mp3"; + if (songName.contains("اول")) return "mp3.mp3"; + if (songName.contains("دوم")) return "mp4.mp3"; + return "mp5.mp3"; + } +} \ No newline at end of file diff --git a/src/main/resources/frozen_bg.jpg b/src/main/resources/frozen_bg.jpg new file mode 100644 index 0000000..755e9dc Binary files /dev/null and b/src/main/resources/frozen_bg.jpg differ diff --git a/src/main/resources/icon.png b/src/main/resources/icon.png new file mode 100644 index 0000000..719d119 Binary files /dev/null and b/src/main/resources/icon.png differ diff --git a/src/main/resources/mp3.mp3 b/src/main/resources/mp3.mp3 new file mode 100644 index 0000000..06731e9 Binary files /dev/null and b/src/main/resources/mp3.mp3 differ diff --git a/src/main/resources/mp4.mp3 b/src/main/resources/mp4.mp3 new file mode 100644 index 0000000..e1b4383 Binary files /dev/null and b/src/main/resources/mp4.mp3 differ diff --git a/src/main/resources/mp5.mp3 b/src/main/resources/mp5.mp3 new file mode 100644 index 0000000..7cdf011 Binary files /dev/null and b/src/main/resources/mp5.mp3 differ diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml index 306c8d3..9437514 100644 --- a/src/main/resources/sbu/javafx/App-view.fxml +++ b/src/main/resources/sbu/javafx/App-view.fxml @@ -1,8 +1,63 @@ + + + - - + - + - + + +