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/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 @@ - + + + + + + +