diff --git a/.idea/misc.xml b/.idea/misc.xml index 001e756..0c04b52 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file 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/musicIcon.png b/musicIcon.png new file mode 100644 index 0000000..4d75811 Binary files /dev/null and b/musicIcon.png differ diff --git a/pom.xml b/pom.xml index eba2d41..0a1a46c 100644 --- a/pom.xml +++ b/pom.xml @@ -17,12 +17,26 @@ org.openjfx javafx-controls - 17.0.6 + 21 org.openjfx javafx-fxml - 17.0.6 + 21 + + + + + org.openjfx + javafx-media + 21 + + + + + de.codecentric.centerdevice + javafxsvg + 1.3.0 diff --git a/songs/song1.mp3 b/songs/song1.mp3 new file mode 100644 index 0000000..1d6f881 Binary files /dev/null and b/songs/song1.mp3 differ diff --git a/songs/song2.mp3 b/songs/song2.mp3 new file mode 100644 index 0000000..7f4d978 Binary files /dev/null and b/songs/song2.mp3 differ diff --git a/songs/song3.mp3 b/songs/song3.mp3 new file mode 100644 index 0000000..c8b333e Binary files /dev/null and b/songs/song3.mp3 differ diff --git a/songs/song4.mp3 b/songs/song4.mp3 new file mode 100644 index 0000000..6546916 Binary files /dev/null and b/songs/song4.mp3 differ diff --git a/src/main/java/sbu/javafx/Launcher.java b/src/main/java/sbu/javafx/Launcher.java index c288d10..178b53c 100644 --- a/src/main/java/sbu/javafx/Launcher.java +++ b/src/main/java/sbu/javafx/Launcher.java @@ -4,5 +4,8 @@ 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); + } } +//the icon is not displayed + diff --git a/src/main/java/sbu/javafx/MusicApp.java b/src/main/java/sbu/javafx/MusicApp.java index a50badc..e52808c 100644 --- a/src/main/java/sbu/javafx/MusicApp.java +++ b/src/main/java/sbu/javafx/MusicApp.java @@ -1,22 +1,45 @@ package sbu.javafx; +import de.codecentric.centerdevice.javafxsvg.SvgImageLoaderFactory; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import javafx.scene.image.Image; public class MusicApp extends Application { @Override public void start(Stage stage) throws IOException { +// SvgImageLoaderFactory.install(); +// InputStream iconStream = getClass().getResourceAsStream("icon.svg"); +// if (iconStream != null) +// { +// Image icon = new Image(iconStream); +// stage.getIcons().add(icon); +// System.out.println("yesss"); +// } else +// { +// System.out.println("not found"); +// } FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml")); Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("Music Player"); stage.setScene(scene); + //TODO: add icon to the stage +// InputStream iconStream = new FileInputStream("musicIcon.png"); +// if (iconStream != null) +// { +// stage.getIcons().add(new Image(iconStream)); +// } else { +// System.out.println("not found"); +// } stage.show(); } diff --git a/src/main/java/sbu/javafx/MusicController.java b/src/main/java/sbu/javafx/MusicController.java index 5da27a9..e77c081 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -2,30 +2,182 @@ package sbu.javafx; import javafx.fxml.FXML; import javafx.event.ActionEvent; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +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.media.Media; +import javafx.scene.media.MediaPlayer; +import javafx.stage.Stage; + + +import javax.imageio.IIOException; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; public class MusicController { - // TODO: Define your UI components using the @FXML annotation. + @FXML private Label nowPlayingSong; + @FXML private Button playBtn1; + @FXML private Button playBtn2; + @FXML private Button playBtn3; + @FXML private Button playBtn4; + @FXML private Button add1; + @FXML private Button add2; + @FXML private Button add3; + @FXML private Button add4; + + private List playlist = new ArrayList<>(); + private Song currentSong = null; + private MediaPlayer mediaPlayer; + private Song song1 = new Song("The Comfort of a Laugh Track", "Roar", "04:52", "songs/song1.mp3"); + private Song song2 = new Song("Perfect Day", "Lou Reed", "03:44", "songs/song2.mp3"); + private Song song3 = new Song("Wish You Were Here", "Pink Floyd", "05:34", "songs/song3.mp3"); + private Song song4 = new Song("No Surprises", "Radiohead", "03:49", "songs/song4.mp3"); - //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 void handlePlayAction(ActionEvent event) { + + Button clickedBotton = (Button) event.getSource(); + resetPlayButtons(); + String buttonId = clickedBotton.getId(); + switch (buttonId) + { + case "playBtn1": + playSong(song1); + playBtn1.setText("Playing"); + playBtn1.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;"); + break; + + case "playBtn2": + playSong(song2); + playBtn2.setText("Playing"); + playBtn2.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;"); + break; + + case "playBtn3": + playSong(song3); + playBtn3.setText("Playing"); + playBtn3.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;"); + break; + + case "playBtn4": + playSong(song4); + playBtn4.setText("Playing"); + playBtn4.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;"); + break; + } + + } - //TODO: Logic to add a specific song to the user's playlist. + @FXML + public void addToPlaylist(ActionEvent event) { - public void addToPlaylist(String songName) { - //Add the song to a list or update a ListView. + Button clickedButton = (Button) event.getSource(); + String btnId = clickedButton.getId(); + Song selectedSong = null; + + switch (btnId) + { + case "add1": + selectedSong = song1; + break; + case "add2": + selectedSong = song2; + break; + case "add3": + selectedSong = song3; + break; + case "add4": + selectedSong = song4; + break; + } + if (selectedSong != null) + { + playlist.add(selectedSong); + } } - // TODO:This method should open a new window or change the current scene to display the "Playlist" page. - + @FXML 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 playlistController = loader.getController(); + playlistController.setPlaylist(playlist, this); + + Stage playlistStage = new Stage(); + playlistStage.setTitle("Playlist"); + playlistStage.setScene(new Scene(root, 500, 400)); + playlistStage.show(); + } catch (IOException e) + { + e.printStackTrace(); + } } + + + + + public void playSong(Song song) { + if (mediaPlayer != null) + { + mediaPlayer.stop(); + mediaPlayer.dispose(); + } + + try { + File musicFile = new File(song.getFilePath()); + if (!musicFile.exists()) { + nowPlayingSong.setText("Error: File not found - " + song.getFilePath()); + System.out.println("Not found: " + musicFile.getAbsolutePath()); + return; + } + Media media = new Media(musicFile.toURI().toString()); + mediaPlayer = new MediaPlayer(media); + mediaPlayer.setOnEndOfMedia(new Runnable() { + @Override + public void run() { + resetPlayButtons(); + nowPlayingSong.setStyle("-fx-font-weight: bold; -fx-font-size: 20px;"); + nowPlayingSong.setText("-"); + if (mediaPlayer != null) + { + mediaPlayer.dispose(); + mediaPlayer = null; + } + } + }); + mediaPlayer.play(); + currentSong = song; + nowPlayingSong.setStyle("-fx-font-size: 20px;"); + nowPlayingSong.setText(song.getTitle() + " - " + song.getArtist()); + } catch (Exception e) { + nowPlayingSong.setText("Playback error"); + e.printStackTrace(); + } + } + + public void resetPlayButtons() + { + playBtn1.setText("Play"); + playBtn1.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;"); + playBtn2.setText("Play"); + playBtn2.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;"); + playBtn3.setText("Play"); + playBtn3.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;"); + playBtn4.setText("Play"); + playBtn4.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;"); + } + } + diff --git a/src/main/java/sbu/javafx/PlaylistController.java b/src/main/java/sbu/javafx/PlaylistController.java new file mode 100644 index 0000000..6c58e56 --- /dev/null +++ b/src/main/java/sbu/javafx/PlaylistController.java @@ -0,0 +1,67 @@ +package sbu.javafx; + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; +import javafx.scene.control.Button; +import javafx.scene.control.Label; + + + +import java.util.ArrayList; +import java.util.List; + +public class PlaylistController { + + @FXML + private VBox songContainer; + + private ArrayList playlist; + private MusicController musicController; + + public void setPlaylist(List playlist, MusicController musicController) + { + this.playlist = new ArrayList<> (playlist); + this.musicController = musicController; + buildRows(); + } + + public void buildRows() + { + songContainer.getChildren().clear(); + + for (Song s : playlist) + { + HBox hbox = new HBox(20); //spacing = 20 + hbox.setAlignment(javafx.geometry.Pos.CENTER); + Label nameLabel = new Label(s.getDisplayName()); + nameLabel.setPrefWidth(250); + + Label durationLabel = new Label(s.getDuration()); + + Button playBtn = new Button("Play"); + playBtn.setPrefWidth(80); + + playBtn.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent actionEvent) { + if (musicController != null) { + musicController.playSong(s); + } + } + }); + + hbox.getChildren().addAll(nameLabel, durationLabel, playBtn); + songContainer.getChildren().add(hbox); + } + + if (playlist.isEmpty()) { + Label emptyLabel = new Label("Playlist is empty. Add some songs from the main page."); + emptyLabel.setStyle("-fx-text-fill: gray;"); + songContainer.getChildren().add(emptyLabel); + } + + } +} diff --git a/src/main/java/sbu/javafx/Song.java b/src/main/java/sbu/javafx/Song.java new file mode 100644 index 0000000..54298cc --- /dev/null +++ b/src/main/java/sbu/javafx/Song.java @@ -0,0 +1,53 @@ +package sbu.javafx; + +public class Song { + private String title; + private String artist; + private String duration; + private String filePath; + + public Song(String title, String artist, String duration, String filePath) + { + this.filePath = filePath; + this.artist = artist; + this.duration = duration; + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } + + public String getDuration() { + return duration; + } + + public void setDuration(String duration) { + this.duration = duration; + } + + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } + + public String getDisplayName() + { + return getTitle()+" - " + getArtist(); + } +} diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml index 306c8d3..7d552d0 100644 --- a/src/main/resources/sbu/javafx/App-view.fxml +++ b/src/main/resources/sbu/javafx/App-view.fxml @@ -1,8 +1,76 @@ - + + + + + + +