Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a61eb66924 | ||
|
|
20cc34564d | ||
|
|
8ba5069e88 | ||
|
|
6eb462eebc |
Generated
+1
-1
@@ -8,7 +8,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="23" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -19,6 +19,7 @@
|
||||
<artifactId>javafx-controls</artifactId>
|
||||
<version>21</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-fxml</artifactId>
|
||||
@@ -31,12 +32,20 @@
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency> </dependencies>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-media</artifactId>
|
||||
<version>21</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -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) }
|
||||
// TODO: Launch the JavaFX application by calling Application.launch(Main.class)
|
||||
Application.launch(MusicApp.class);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.image.Image;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -14,9 +15,13 @@ public class MusicApp extends Application {
|
||||
{
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
|
||||
stage.setTitle("Music Player");
|
||||
stage.setResizable(false);
|
||||
stage.setScene(scene);
|
||||
//TODO: add icon to the stage
|
||||
Image icon = new Image(getClass().getResourceAsStream("MusicPlayerIcon.png"));
|
||||
stage.getIcons().add(icon);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,30 +2,207 @@ package sbu.javafx;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.image.Image;
|
||||
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 java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MusicController {
|
||||
private List<Song> allSongs;
|
||||
private List<Song> playList;
|
||||
private Song currentlyPlaying;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private boolean isPlaying = false;
|
||||
private Button currentPlayButton;
|
||||
@FXML private Label nowPlayingLabel;
|
||||
@FXML private Button song01;
|
||||
@FXML private Button song02;
|
||||
@FXML private Button song03;
|
||||
@FXML private Button song01P;
|
||||
@FXML private Button song02P;
|
||||
@FXML private Button song03P;
|
||||
|
||||
// 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
|
||||
public void handlePlayAction() {
|
||||
public void initialize() {
|
||||
|
||||
allSongs = new ArrayList<>();
|
||||
playList = new ArrayList<>();
|
||||
|
||||
allSongs.add(new Song("Sing", "Travis", "3:48", "E:\\JavaProjects\\HW-07-JavaFX\\src\\main\\resources\\sbu\\songs\\Sing.mp3"));
|
||||
allSongs.add(new Song("Akhare Ghesse", "Dang Show", "3:18", "E:\\JavaProjects\\HW-07-JavaFX\\src\\main\\resources\\sbu\\songs\\Akhare-Ghesse.mp3"));
|
||||
allSongs.add(new Song("Married Life", "Michael Giacchino", "4:10", "E:\\JavaProjects\\HW-07-JavaFX\\src\\main\\resources\\sbu\\songs\\Married-Life.mp3"));
|
||||
|
||||
nowPlayingLabel.setText("");
|
||||
|
||||
addToPlaylist();
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void handlePlayAction(ActionEvent event) {
|
||||
//Update labels, change button icons, etc.
|
||||
Button clickedButton = (Button) event.getSource();
|
||||
Song songToPlay = null;
|
||||
|
||||
if (clickedButton == song01) {
|
||||
songToPlay = allSongs.get(0);
|
||||
} else if (clickedButton == song02) {
|
||||
songToPlay = allSongs.get(1);
|
||||
} else if (clickedButton == song03) {
|
||||
songToPlay = allSongs.get(2);
|
||||
}
|
||||
|
||||
if (songToPlay != null) {
|
||||
|
||||
if (currentlyPlaying != songToPlay) {
|
||||
playNewSong(songToPlay, clickedButton);
|
||||
} else {
|
||||
// toggle play/pause
|
||||
togglePlayPause(clickedButton);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//TODO: Logic to add a specific song to the user's playlist.
|
||||
|
||||
public void addToPlaylist(String songName) {
|
||||
@FXML
|
||||
public void addToPlaylist() {
|
||||
//Add the song to a list or update a ListView.
|
||||
song01P.setOnAction(event -> {
|
||||
playList.add(allSongs.get(0));
|
||||
});
|
||||
|
||||
song02P.setOnAction(event -> {
|
||||
playList.add(allSongs.get(1));
|
||||
});
|
||||
|
||||
song03P.setOnAction(event -> {
|
||||
playList.add(allSongs.get(2));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void playNewSong(Song song, Button playButton) {
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.stop();
|
||||
mediaPlayer.dispose();
|
||||
// Reset the previous play button text if it exists
|
||||
if (currentPlayButton != null && currentPlayButton != playButton) {
|
||||
currentPlayButton.setText("Play");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
File audioFile = new File(song.getFilePath());
|
||||
if (!audioFile.exists()) {
|
||||
nowPlayingLabel.setText("File not found: " + song.getSongName());
|
||||
return;
|
||||
}
|
||||
|
||||
Media media = new Media(audioFile.toURI().toString());
|
||||
mediaPlayer = new MediaPlayer(media);
|
||||
|
||||
mediaPlayer.setOnReady(() -> {
|
||||
currentlyPlaying = song;
|
||||
nowPlayingLabel.setText(song.getSongName() + " - " + song.getArtistName());
|
||||
isPlaying = true;
|
||||
playButton.setText("Playing");
|
||||
currentPlayButton = playButton;
|
||||
mediaPlayer.play();
|
||||
});
|
||||
|
||||
mediaPlayer.setOnEndOfMedia(() -> {
|
||||
isPlaying = false;
|
||||
if (currentPlayButton != null) {
|
||||
currentPlayButton.setText("Play");
|
||||
}
|
||||
nowPlayingLabel.setText("Finished: " + song.getSongName());
|
||||
});
|
||||
|
||||
mediaPlayer.setOnError(() -> {
|
||||
System.err.println("Media error: " + mediaPlayer.getError());
|
||||
nowPlayingLabel.setText("Error playing: " + song.getSongName());
|
||||
playButton.setText("Play");
|
||||
isPlaying = false;
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
nowPlayingLabel.setText("Cannot play: " + song.getSongName());
|
||||
}
|
||||
}
|
||||
|
||||
private void togglePlayPause(Button playButton) {
|
||||
if (mediaPlayer != null) {
|
||||
if (isPlaying) {
|
||||
mediaPlayer.pause();
|
||||
isPlaying = false;
|
||||
playButton.setText("Play");
|
||||
} else {
|
||||
mediaPlayer.play();
|
||||
isPlaying = true;
|
||||
playButton.setText("Playing");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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 {
|
||||
Stage playlistStage = new Stage();
|
||||
playlistStage.setTitle("Playlist");
|
||||
|
||||
VBox playlistLayout = new VBox(10);
|
||||
playlistLayout.setStyle("-fx-padding: 20; -fx-alignment: center; -fx-background-color: rgba(52, 152, 219, 0.68);");
|
||||
|
||||
Label titleLabel = new Label("Your Playlist");
|
||||
titleLabel.setStyle("-fx-font-size: 18px; -fx-font-weight: bold;");
|
||||
|
||||
ListView<String> playlistListView = new ListView<>();
|
||||
|
||||
if (playList != null) {
|
||||
for (Song song : playList) {
|
||||
playlistListView.getItems().add(song.getSongName() + " - " + song.getArtistName() + " (" + song.getDuration() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
Label emptyLabel = new Label("Your playlist is empty. Add any song you like!");
|
||||
emptyLabel.setStyle("-fx-text-fill: gray; -fx-font-weight: bold");
|
||||
|
||||
if (playList == null || playList.isEmpty()) {
|
||||
playlistLayout.getChildren().addAll(titleLabel, emptyLabel);
|
||||
} else {
|
||||
playlistLayout.getChildren().addAll(titleLabel, playlistListView);
|
||||
}
|
||||
|
||||
// Set up the scene and show the stage
|
||||
Scene playlistScene = new Scene(playlistLayout, 300, 200);
|
||||
playlistStage.setScene(playlistScene);
|
||||
playlistStage.setResizable(false);
|
||||
playlistStage.getIcons().add(new Image(getClass().getResourceAsStream("MusicPlayerIcon.png")));
|
||||
playlistStage.show();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Error opening playlist window: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package sbu.javafx;
|
||||
|
||||
import java.sql.Time;
|
||||
|
||||
public class Song {
|
||||
private String songName;
|
||||
private String artistName;
|
||||
private String duration;
|
||||
private String filePath;
|
||||
|
||||
public Song(String songName, String artistName, String duration, String filePath) {
|
||||
this.songName = songName;
|
||||
this.artistName = artistName;
|
||||
this.duration = duration;
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
//getters and setters
|
||||
public String getSongName() {
|
||||
return songName;
|
||||
}
|
||||
|
||||
public String getArtistName() {
|
||||
return artistName;
|
||||
}
|
||||
|
||||
public String getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<VBox xmlns="http://javafx.com/fxml" alignment="CENTER" spacing="10">
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
|
||||
<VBox alignment="CENTER" spacing="10" xmlns:fx="http://javafx.com/fxml" fx:controller="sbu.javafx.MusicController">
|
||||
<padding>
|
||||
<Insets topRightBottomLeft="10"/>
|
||||
</padding>
|
||||
|
||||
<!-- TODO: Add your code here! -->
|
||||
|
||||
<HBox alignment="CENTER" spacing="25" HBox.hgrow="ALWAYS" styleClass="main-box">
|
||||
|
||||
<VBox prefWidth="125" styleClass="column">
|
||||
<Label text="Song Title" styleClass="title"/>
|
||||
<Label text="Sing" styleClass="info"/>
|
||||
<Label text="Akhare Ghesse" styleClass="info"/>
|
||||
<Label text="Married Life" styleClass="info"/>
|
||||
</VBox>
|
||||
|
||||
<VBox prefWidth="150" styleClass="column">
|
||||
<Label text="Artist Name" styleClass="title"/>
|
||||
<Label text="Travis" styleClass="info"/>
|
||||
<Label text="Dang Show" styleClass="info"/>
|
||||
<Label text="Michael Giacchino" styleClass="info"/>
|
||||
</VBox>
|
||||
|
||||
<VBox prefWidth="75" styleClass="column">
|
||||
<Label text="Duration" styleClass="title"/>
|
||||
<Label text="3:48" styleClass="info"/>
|
||||
<Label text="3:18" styleClass="info"/>
|
||||
<Label text="4:10" styleClass="info"/>
|
||||
</VBox>
|
||||
|
||||
<VBox prefWidth="75" styleClass="column">
|
||||
<Label text="Play" styleClass="title"/>
|
||||
<Button text="Play" onAction="#handlePlayAction" fx:id="song01" styleClass="play-btn"/>
|
||||
<Button text="Play" onAction="#handlePlayAction" fx:id="song02" styleClass="play-btn"/>
|
||||
<Button text="Play" onAction="#handlePlayAction" fx:id="song03" styleClass="play-btn"/>
|
||||
</VBox>
|
||||
|
||||
<VBox prefWidth="125" styleClass="column">
|
||||
<Label text="Add to Playlist" styleClass="title"/>
|
||||
<Button text="Add to Playlist" fx:id="song01P" styleClass="add-btn"/>
|
||||
<Button text="Add to Playlist" fx:id="song02P" styleClass="add-btn"/>
|
||||
<Button text="Add to Playlist" fx:id="song03P" styleClass="add-btn"/>
|
||||
</VBox>
|
||||
|
||||
</HBox>
|
||||
|
||||
<HBox spacing="15" alignment="CENTER" HBox.hgrow="ALWAYS" styleClass="play-box">
|
||||
<Label text="Now Playing:" styleClass="title0"/>
|
||||
<Label text="" fx:id="nowPlayingLabel" styleClass="nowPlaying" HBox.hgrow="ALWAYS" maxWidth="Infinity"/>
|
||||
<Button text="Open Playlist" onAction="#openPlaylistWindow" styleClass="openP-btn"/>
|
||||
</HBox>
|
||||
|
||||
</VBox>
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 327 KiB |
@@ -0,0 +1,90 @@
|
||||
.root {
|
||||
-fx-background-color: #fafafa;
|
||||
}
|
||||
|
||||
.main-box {
|
||||
-fx-border-color: rgba(89, 86, 86, 0.66);
|
||||
-fx-border-width: 2px;
|
||||
-fx-border-style: solid;
|
||||
-fx-border-radius: 5px;
|
||||
-fx-padding: 5px;
|
||||
}
|
||||
|
||||
.title {
|
||||
-fx-font-size: 18px;
|
||||
-fx-font-weight: bold;
|
||||
-fx-text-fill: #323232;
|
||||
}
|
||||
|
||||
.info {
|
||||
-fx-font-size: 16px;
|
||||
-fx-font-weight: normal;
|
||||
-fx-text-fill: #000000;
|
||||
}
|
||||
|
||||
.title0 {
|
||||
-fx-font-size: 18px;
|
||||
-fx-font-weight: bold;
|
||||
-fx-text-fill: #595656;
|
||||
}
|
||||
|
||||
.nowPlaying {
|
||||
-fx-font-size: 16px;
|
||||
-fx-text-fill: #595656;
|
||||
}
|
||||
|
||||
.play-btn {
|
||||
-fx-background-color: #1b4781;
|
||||
-fx-text-fill: white;
|
||||
-fx-font-size: 12px;
|
||||
-fx-font-weight: bold;
|
||||
-fx-background-radius: 6;
|
||||
-fx-cursor: hand;
|
||||
}
|
||||
|
||||
.play-btn:hover {
|
||||
-fx-background-color: #b379d7;
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
-fx-background-color: #1b4781;
|
||||
-fx-text-fill: white;
|
||||
-fx-font-size: 12px;
|
||||
-fx-font-weight: bold;
|
||||
-fx-background-radius: 6;
|
||||
-fx-cursor: hand;
|
||||
}
|
||||
|
||||
.add-btn:hover {
|
||||
-fx-background-color: #b379d7;
|
||||
}
|
||||
|
||||
.openP-btn {
|
||||
-fx-background-color: #1b4781;
|
||||
-fx-text-fill: white;
|
||||
-fx-font-size: 16px;
|
||||
-fx-font-weight: bold;
|
||||
-fx-padding: 8 16;
|
||||
-fx-background-radius: 6;
|
||||
-fx-cursor: hand;
|
||||
}
|
||||
|
||||
.openP-btn:hover {
|
||||
-fx-background-color: #b379d7;
|
||||
}
|
||||
|
||||
.column {
|
||||
-fx-spacing: 12;
|
||||
-fx-alignment: CENTER;
|
||||
}
|
||||
|
||||
.play-box {
|
||||
-fx-border-color: rgba(52, 152, 219, 0.46);
|
||||
-fx-border-width: 2px;
|
||||
-fx-border-style: solid;
|
||||
-fx-border-radius: 5px;
|
||||
-fx-padding: 5px;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user