diff --git a/pom.xml b/pom.xml
index e45147d..644914f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,6 +25,12 @@
21
+
+ org.openjfx
+ javafx-media
+ 17.0.12
+
+
org.junit.jupiter
junit-jupiter-api
diff --git a/src/main/java/sbu/javafx/Main.java b/src/main/java/sbu/javafx/Main.java
index 8330187..bdbf4d9 100644
--- a/src/main/java/sbu/javafx/Main.java
+++ b/src/main/java/sbu/javafx/Main.java
@@ -4,6 +4,7 @@ import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
+import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends Application {
@@ -19,7 +20,7 @@ public class Main extends Application {
new Song("Blinding Lights", "The Weeknd","/musics/The-Weeknd-Blinding-Lights-128.mp3"),
new Song("Comme des enfants", "Coert De Pirate","/musics/Coeur-De-Pirate-Comme-des-enfants-128.mp3"),
new Song("Slow Down", "GRAE", "/musics/GRAE-Slow-Down-128.mp3"),
- new Song("Cigarettes out the Window", "TV Girl", "/music/TV-Girl-Cigarettes-out-the-Window-128.mp3"),
+ new Song("Cigarettes out the Window", "TV Girl", "/musics/TV-Girl-Cigarettes-out-the-Window-128.mp3"),
new Song("deja vu", "Olivia Rodirgo", "/musics/Olivia-Rodrigo-deja-vu-320.mp3"),
new Song("The Line", "Twoenty One Pilots", "/musics/Twenty-One-Pilots-The-Line-128.mp3"),
new Song("Bones", "Imagine Dragons", "/musics/Imagine-Dragons-Bones-128.mp3"),
@@ -37,6 +38,9 @@ public class Main extends Application {
primaryStage.setTitle("Music Player");
primaryStage.setScene(scene);
+ primaryStage.getIcons().add(
+ new Image(getClass().getResourceAsStream("/image/icon.png"))
+ );
primaryStage.setResizable(false);
primaryStage.show();
}
diff --git a/src/main/java/sbu/javafx/MusicApp.java b/src/main/java/sbu/javafx/MusicApp.java
index a50badc..819aebe 100644
--- a/src/main/java/sbu/javafx/MusicApp.java
+++ b/src/main/java/sbu/javafx/MusicApp.java
@@ -2,7 +2,9 @@ package sbu.javafx;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
+import javafx.scene.Parent;
import javafx.scene.Scene;
+import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.io.IOException;
@@ -13,9 +15,38 @@ public class MusicApp extends Application {
public void start(Stage stage) throws IOException
{
FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
- Scene scene = new Scene(fxmlLoader.load());
+ Parent root = fxmlLoader.load();
+
+ MusicController controller = fxmlLoader.getController();
+
+ controller.musicsListView.getItems().addAll(
+ new Song("Blinding Lights", "The Weeknd","/musics/The-Weeknd-Blinding-Lights-128.mp3"),
+ new Song("Comme des enfants", "Coert De Pirate","/musics/Coeur-De-Pirate-Comme-des-enfants-128.mp3"),
+ new Song("Slow Down", "GRAE", "/musics/GRAE-Slow-Down-128.mp3"),
+ new Song("Cigarettes out the Window", "TV Girl", "/musics/TV-Girl-Cigarettes-out-the-Window-128.mp3"),
+ new Song("deja vu", "Olivia Rodirgo", "/musics/Olivia-Rodrigo-deja-vu-320.mp3"),
+ new Song("The Line", "Twoenty One Pilots", "/musics/Twenty-One-Pilots-The-Line-128.mp3"),
+ new Song("Bones", "Imagine Dragons", "/musics/Imagine-Dragons-Bones-128.mp3"),
+ new Song("Bunker", "Balthazar", "/musics/Balthazar-Bunker-128.mp3"),
+ new Song("As It Was", "Harry Styles", "/musics/Harry-Styles-As-It-Was-320.mp3")
+ );
+
+ controller.setupListView(controller.musicsListView);
+ controller.setupListView(controller.playlistListView);
+
+ Scene scene = new Scene(root, 500, 600);
+
+ scene.getStylesheets().add(
+ getClass().getResource("/css/dark.css").toExternalForm()
+ );
+
stage.setTitle("Music Player");
+ stage.getIcons().add(
+ new Image(getClass().getResourceAsStream("/image/icon.png"))
+ );
stage.setScene(scene);
+ stage.setResizable(false);
+
//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 e2e24e2..a56a319 100644
--- a/src/main/java/sbu/javafx/MusicController.java
+++ b/src/main/java/sbu/javafx/MusicController.java
@@ -35,6 +35,12 @@ public class MusicController {
@FXML
Button themeToggleBtn;
@FXML
+ Button nextBtn;
+ @FXML
+ Button preveBtn;
+ @FXML
+ Button repeatBtn;
+ @FXML
private Label messageLabel;
private boolean isDarkMode=true;
private boolean isPlaying=false;
@@ -76,6 +82,46 @@ public class MusicController {
playPauseBtn.setGraphic(playIcon);
+ nextBtn.setOnAction(e ->{
+
+ player.pause();
+
+ if(isOpen) {
+ currentSong=playlistListView.getItems().get(findIndex(playlistListView,currentSong)+1);
+ setPlayer(currentSong).play();
+ }else{
+ currentSong=musicsListView.getItems().get(findIndex(musicsListView,currentSong)+1);
+ setPlayer(currentSong).play();
+ }
+ });
+
+ preveBtn.setOnAction(e ->{
+
+ player.pause();
+
+ if(isOpen) {
+ currentSong=playlistListView.getItems().get(findIndex(playlistListView,currentSong)-1);
+ setPlayer(currentSong).play();
+ }else{
+ currentSong=musicsListView.getItems().get(findIndex(musicsListView,currentSong)-1);
+ setPlayer(currentSong).play();
+ }
+ });
+
+ repeatBtn.setOnAction(e->{
+ if(!isRepeat){
+ repeatBtn.setStyle(
+ "-fx-border-color: blue;" +
+ "-fx-border-width: 2px;" +
+ "-fx-border-radius: 100em;"
+ );
+ isRepeat=true;
+ }else{
+ repeatBtn.setStyle("-fx-border-color: transparent;");
+ isRepeat=false;
+ }
+ });
+
}
public void setupListView(ListView listView) {
@@ -131,11 +177,11 @@ public class MusicController {
@FXML
public void toggleTheme(ActionEvent event) {
if (isDarkMode) {
- themeToggleBtn.setText("Light");
+ themeToggleBtn.setText("Dark");
changeTheme();
isDarkMode = false;
} else {
- themeToggleBtn.setText("Dark");
+ themeToggleBtn.setText("Light");
changeTheme();
isDarkMode = true;
}
@@ -162,42 +208,64 @@ public class MusicController {
//TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
- @FXML
- public void handlePlayAction() {
- //Update labels, change button icons, etc.
- }
+ public MediaPlayer setPlayer(Song song) {//این تابع برای ست کردن مدیای در حال پخشه
- public void play(ActionEvent event, Song song){
-
- currentSong=song;
nowPlayingLabel.setText(song.getName());
- if (!isPlaying)
- playPause(event);
-
- // کد پخش موسیقی
path = getClass()
.getResource(song.getPath())
.toExternalForm();
media = new Media(path);
player = new MediaPlayer(media);
+ //این برا اینه که تموم شد بره بعدی
+ player.setOnEndOfMedia(() -> {
+ if(isRepeat){
+ player.seek(player.getStartTime());
+ player.play();
+ }
+ else
+ nextBtn.fire();
+ });
+
+ return player;
+ }
+
+ public void play(ActionEvent event, Song song){
+
+ currentSong = song;
+
+ if(player != null){
+ player.stop();
+ }
+
+ setPlayer(currentSong);
+
player.play();
+
+ isPlaying = true;
+
+ playPauseBtn.setGraphic(pauseIcon);
}
@FXML
public void playPause(ActionEvent a){
+
+ if(player == null){
+ return;
+ }
+
if(isPlaying){
playPauseBtn.setGraphic(playIcon);
- isPlaying=false;
- //کد توقف
player.pause();
+ isPlaying = false;
- }
- else{
+ } else {
playPauseBtn.setGraphic(pauseIcon);
- isPlaying=true;
+
+ player.play();
+ isPlaying = true;
}
}
@@ -236,12 +304,12 @@ public class MusicController {
public void openPlaylistWindow(ActionEvent e) {
- if(isPlaying)
+ if(isPlaying) {
playPause(e);
+ }
if(!isOpen){
-
- // باز کردن پلیلیست
+ // باز کردن پلی لیست
if(playlistListView.getItems().isEmpty()){
Alert alert = new Alert(Alert.AlertType.WARNING);
@@ -266,9 +334,7 @@ public class MusicController {
playlistListView.setManaged(true);
} else {
-
// برگشت به صفحه اصلی
-
isOpen = false;
openPlaylistBtn.setText("Open Playlist");
@@ -280,6 +346,18 @@ public class MusicController {
playlistListView.setManaged(false);
}
}
+
+ public int findIndex(ListView listView, Song song){
+ for(int i = 0; i < listView.getItems().size(); i++){
+
+ if(listView.getItems().get(i).equals(song)){
+ return i;
+ }
+ }
+
+ return -1;
}
+}
+
diff --git a/src/main/resources/image/icon.png b/src/main/resources/image/icon.png
new file mode 100644
index 0000000..f5b8da9
Binary files /dev/null and b/src/main/resources/image/icon.png differ
diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml
index 91cf132..5d535ca 100644
--- a/src/main/resources/sbu/javafx/App-view.fxml
+++ b/src/main/resources/sbu/javafx/App-view.fxml
@@ -92,7 +92,7 @@
-