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/pom.xml b/pom.xml index e45147d..51c63f2 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,11 @@ javafx-fxml 21 + + org.openjfx + javafx-media + 21 + org.junit.jupiter diff --git a/src/main/java/sbu/javafx/Launcher.java b/src/main/java/sbu/javafx/Launcher.java index c288d10..a3ffd1f 100644 --- a/src/main/java/sbu/javafx/Launcher.java +++ b/src/main/java/sbu/javafx/Launcher.java @@ -4,5 +4,6 @@ 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..6c54b15 100644 --- a/src/main/java/sbu/javafx/MusicApp.java +++ b/src/main/java/sbu/javafx/MusicApp.java @@ -2,21 +2,27 @@ package sbu.javafx; import javafx.application.Application; import javafx.fxml.FXMLLoader; +import javafx.scene.image.Image; import javafx.scene.Scene; import javafx.stage.Stage; +import java.awt.*; import java.io.IOException; +import java.util.Objects; public class MusicApp extends Application { @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("main.fxml")); Scene scene = new Scene(fxmlLoader.load()); + Image icon = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/icon.png"))); + + stage.getIcons().add(icon); 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..d67a9b1 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -1,31 +1,143 @@ package sbu.javafx; import javafx.fxml.FXML; -import javafx.event.ActionEvent; +import javafx.scene.control.Button; import javafx.scene.control.Label; -import javafx.scene.input.MouseEvent; -import javafx.scene.layout.VBox; +import javafx.scene.control.ListView; +import javafx.scene.media.Media; +import javafx.scene.media.MediaPlayer; + +import java.net.URL; 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 Button pausePlay; @FXML - public void handlePlayAction() { - //Update labels, change button icons, etc. + private ListView songListView; + + @FXML + private ListView playlist; + + @FXML + private Label nowPlayingLabel; + + private MediaPlayer mediaPlayer; + private Song currentSong; + private boolean isPlaying = false; + + @FXML + public void initialize() { + loadSongs(); } - //TODO: Logic to add a specific song to the user's playlist. + // Adds songs to ListView + private void loadSongs() { + // Songs paths (used URL so the songs can stay in the resources folder so in case of building to jar the songs remain in the app) + URL comeAsYouAre = getClass().getResource("/songs/comeAsYouAre.mp3"); + URL childIWillHurtYou = getClass().getResource("/songs/childIWillHurtYou.mp3"); + URL lucky = getClass().getResource("/songs/lucky.mp3"); + URL fitterHappier = getClass().getResource("/songs/fitterHappier.mp3"); - public void addToPlaylist(String songName) { - //Add the song to a list or update a ListView. + songListView.getItems().addAll( + new Song("Come As You Are", "Nirvana", "03:38", comeAsYouAre), + new Song("Child I Will Hurt You", "Crystal Castles", "03:33", childIWillHurtYou), + new Song("Lucky", "Radiohead","04:18", lucky), + new Song("Fitter Happier","Radiohead","01:57",fitterHappier) + ); } - // TODO:This method should open a new window or change the current scene to display the "Playlist" page. + private void playSelectedSong(Song song) { + try { + if (mediaPlayer != null) { + mediaPlayer.stop(); + mediaPlayer.dispose(); + } - public void openPlaylistWindow() { - //Create a new stage and show the playlist UI. + currentSong = song; + + mediaPlayer = new MediaPlayer( + new Media(song.getUrl().toString()) + ); + + nowPlayingLabel.setText("Now Playing: " + song.getTitle() + " - " + song.getArtist()); + pausePlay.setText("Pause"); + isPlaying = true; + mediaPlayer.play(); + + } catch (Exception e) { + nowPlayingLabel.setText("Error: Cannot play file - " + e.getMessage()); + } } -} + + @FXML + public void togglePlayPause() { + if (mediaPlayer == null) return; + + if (isPlaying) { + mediaPlayer.pause(); + pausePlay.setText("Play"); + isPlaying = false; + } else { + mediaPlayer.play(); + isPlaying = true; + pausePlay.setText("Pause"); + } + } + + @FXML + public void addCurrentToPlaylist() { + if (currentSong != null) { + boolean alreadyExists = false; + for (Song song : playlist.getItems()) { + if (song.getUrl().toString().equals(currentSong.getUrl().toString())) { + alreadyExists = true; + break; + } + } + + if (!alreadyExists) { + playlist.getItems().add(currentSong); + System.out.println("Added to playlist : " + currentSong.getTitle()); + nowPlayingLabel.setText("Now Playing : " + currentSong.getTitle() + " - Added to playlist!"); + } else { + System.out.println("Song already in playlist"); + nowPlayingLabel.setText("Now Playing : " + currentSong.getTitle() + " - Already in playlist!"); + } + } + } + + @FXML + public void clearPlaylist() { + playlist.getItems().clear(); + System.out.println("Playlist cleared"); + } + + @FXML + public void removeSelectedFromPlaylist() { + Song selectedSong = playlist.getSelectionModel().getSelectedItem(); + if (selectedSong != null) { + playlist.getItems().remove(selectedSong); + System.out.println("Removed from playlist : " + selectedSong.getTitle()); + } + } + + @FXML + public void handleSongClick() { + // Get the selected song from the song ListView + Song selectedSong = songListView.getSelectionModel().getSelectedItem(); + if (selectedSong != null) { + playSelectedSong(selectedSong); + } + } + + @FXML + public void handleSongClickPlaylist(){ + // Get the selected song from the playlist ListView + Song selectedSong = playlist.getSelectionModel().getSelectedItem(); + if (selectedSong != null) { + playSelectedSong(selectedSong); + } + } +} \ No newline at end of file diff --git a/src/main/java/sbu/javafx/Song.java b/src/main/java/sbu/javafx/Song.java new file mode 100644 index 0000000..9a40d90 --- /dev/null +++ b/src/main/java/sbu/javafx/Song.java @@ -0,0 +1,27 @@ +package sbu.javafx; + +import java.net.URL; + +public class Song { + private String title; + private String artist; + private String duration; + private URL url; + + public Song(String title, String artist, String duration, URL url) { + this.title = title; + this.artist = artist; + this.duration = duration; + this.url = url; + } + + public String getTitle() { return title; } + public String getArtist() { return artist; } + public String getDuration() { return duration; } + public URL getUrl() { return url; } + + @Override + public String toString() { + return getTitle() + " - " + getArtist() + " (" + getDuration() + ")"; + } +} \ No newline at end of file diff --git a/src/main/resources/icon.png b/src/main/resources/icon.png new file mode 100644 index 0000000..939d46d Binary files /dev/null and b/src/main/resources/icon.png differ diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml deleted file mode 100644 index 306c8d3..0000000 --- a/src/main/resources/sbu/javafx/App-view.fxml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/src/main/resources/sbu/javafx/main.fxml b/src/main/resources/sbu/javafx/main.fxml new file mode 100644 index 0000000..29e6a82 --- /dev/null +++ b/src/main/resources/sbu/javafx/main.fxml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + +