Implemented dark mode, time slider, and volume slider, and fixed the playlist.
This commit is contained in:
@@ -14,6 +14,7 @@ public class MusicApp extends Application {
|
||||
{
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
|
||||
stage.setTitle("Music Player");
|
||||
stage.setScene(scene);
|
||||
|
||||
|
||||
@@ -2,17 +2,71 @@ package sbu.javafx;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.media.Media;
|
||||
import javafx.scene.media.MediaPlayer;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.control.Slider;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.util.Duration;
|
||||
import javafx.scene.control.ToggleButton;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public class MusicController {
|
||||
|
||||
ArrayList<String> playls = new ArrayList<>() ;
|
||||
@FXML
|
||||
private ListView<String> playlistview ;
|
||||
@FXML
|
||||
private Label nowPlayingLabel ;
|
||||
@FXML
|
||||
private Slider timeSlider ;
|
||||
@FXML
|
||||
private Label timeLabel ;
|
||||
@FXML
|
||||
private Slider volumeSlider ;
|
||||
@FXML
|
||||
private ToggleButton themeButton ;
|
||||
@FXML
|
||||
public void toggleTheme() {
|
||||
|
||||
Scene scene = themeButton.getScene();
|
||||
|
||||
scene.getStylesheets().clear();
|
||||
|
||||
if(themeButton.isSelected()) {
|
||||
|
||||
scene.getStylesheets().add(
|
||||
getClass()
|
||||
.getResource("dark-theme.css")
|
||||
.toExternalForm()
|
||||
);
|
||||
|
||||
themeButton.setText("Dark Mode");
|
||||
}
|
||||
else {
|
||||
|
||||
scene.getStylesheets().add(
|
||||
getClass()
|
||||
.getResource("light-theme.css")
|
||||
.toExternalForm()
|
||||
);
|
||||
|
||||
themeButton.setText("Light Mode");
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -26,12 +80,14 @@ public class MusicController {
|
||||
if (!IsPlay)
|
||||
{
|
||||
btn.setText("stop");
|
||||
|
||||
IsPlay = true ;
|
||||
|
||||
}
|
||||
else if (IsPlay)
|
||||
{
|
||||
btn.setText("play");
|
||||
|
||||
IsPlay = false ;
|
||||
}
|
||||
}
|
||||
@@ -45,12 +101,14 @@ public class MusicController {
|
||||
if (!IsPlay) {
|
||||
songPath = "/1.mp3";
|
||||
Play( button);
|
||||
nowPlayingLabel.setText("Rahe Meykhaneh");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
mediaPlayer.stop();
|
||||
Play( button);
|
||||
nowPlayingLabel.setText("Stop");
|
||||
|
||||
|
||||
|
||||
@@ -62,12 +120,14 @@ public class MusicController {
|
||||
if (!IsPlay) {
|
||||
songPath = "/2.mp3";
|
||||
Play( button);
|
||||
nowPlayingLabel.setText("Saghi Bia");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
mediaPlayer.stop();
|
||||
Play( button);
|
||||
nowPlayingLabel.setText("Stop");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -78,11 +138,14 @@ public class MusicController {
|
||||
songPath = "/3.mp3";
|
||||
Play( button);
|
||||
|
||||
nowPlayingLabel.setText("Mara Be Hich Bedady");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
mediaPlayer.stop();
|
||||
Play(button);
|
||||
nowPlayingLabel.setText("Stop");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -93,19 +156,91 @@ public class MusicController {
|
||||
);
|
||||
|
||||
mediaPlayer = new MediaPlayer(media);
|
||||
mediaPlayer.setVolume(
|
||||
volumeSlider.getValue() / 100.0
|
||||
);
|
||||
volumeSlider.valueProperty().addListener(
|
||||
(obs, oldVal, newVal) -> {
|
||||
|
||||
if(mediaPlayer != null) {
|
||||
|
||||
mediaPlayer.setVolume(
|
||||
newVal.doubleValue() / 100.0
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
);
|
||||
mediaPlayer.setOnReady(() -> {
|
||||
|
||||
timeSlider.setMax(mediaPlayer.getTotalDuration().toSeconds());
|
||||
});
|
||||
mediaPlayer.currentTimeProperty().addListener(
|
||||
(obs, oldTime, newTime) -> {
|
||||
|
||||
timeSlider.setValue(
|
||||
newTime.toSeconds()
|
||||
);
|
||||
timeLabel.setText(
|
||||
(int)newTime.toMinutes()
|
||||
+ ":"
|
||||
+ (int)(newTime.toSeconds() % 60)
|
||||
);
|
||||
|
||||
}
|
||||
);
|
||||
timeSlider.setOnMouseReleased(event2 -> {
|
||||
|
||||
mediaPlayer.seek(
|
||||
Duration.seconds(
|
||||
timeSlider.getValue()
|
||||
)
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
mediaPlayer.play();
|
||||
}
|
||||
|
||||
//TODO: Logic to add a specific song to the user's playlist.
|
||||
|
||||
public void addToPlaylist(String songName) {
|
||||
public void addToPlaylist( ActionEvent event) {
|
||||
//Add the song to a list or update a ListView.
|
||||
Button button =
|
||||
(Button) event.getSource();
|
||||
if(!playls.contains(button.getUserData().toString())) {
|
||||
playls.add(button.getUserData().toString());
|
||||
}
|
||||
System.out.println(playls);
|
||||
|
||||
}
|
||||
|
||||
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
|
||||
|
||||
public void openPlaylistWindow() {
|
||||
@FXML
|
||||
public void openPlaylistWindow () throws IOException {
|
||||
//Create a new stage and show the playlist UI.
|
||||
System.out.println("Button Clicked");
|
||||
System.out.println(
|
||||
getClass().getResource("App-view2.fxml")
|
||||
);
|
||||
FXMLLoader loader = new FXMLLoader(
|
||||
getClass().getResource("App-view2.fxml")
|
||||
);
|
||||
Parent root = loader.load();
|
||||
MusicController controller = loader.getController();
|
||||
|
||||
controller.playlistview.getItems().addAll(playls);
|
||||
|
||||
Stage stage = new Stage() ;
|
||||
|
||||
|
||||
stage.setTitle("Playlist");
|
||||
|
||||
stage.setScene(new Scene(root));
|
||||
|
||||
stage.show();
|
||||
|
||||
|
||||
playlistview.getItems().addAll(playls) ;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.Slider?>
|
||||
<?import javafx.scene.control.ToggleButton?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.shape.Rectangle?>
|
||||
<?import javafx.scene.text.Font?>
|
||||
|
||||
<VBox alignment="CENTER" xmlns="http://javafx.com/javafx/26" xmlns:fx="http://javafx.com/fxml/1"
|
||||
fx:controller="sbu.javafx.MusicController">
|
||||
<VBox alignment="CENTER" xmlns="http://javafx.com/javafx/26" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sbu.javafx.MusicController">
|
||||
<children>
|
||||
<Label alignment="CENTER" prefHeight="23.0" prefWidth="576.0" text="Music Player">
|
||||
<font>
|
||||
@@ -40,7 +41,7 @@
|
||||
<Label layoutX="275.0" layoutY="23.0" text="06:19" />
|
||||
|
||||
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#1f93ff00" height="48.0" layoutX="7.0" layoutY="8.0" stroke="BLACK" strokeType="INSIDE" width="567.0" />
|
||||
<Button layoutX="366.0" layoutY="18.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="86.0" text="Add To PlayList">
|
||||
<Button fx:id="4" layoutX="366.0" layoutY="18.0" mnemonicParsing="false" onAction="#addToPlaylist" prefHeight="26.0" prefWidth="86.0" text="Add To PlayList" userData="Rahe Meykhaneh --- M.Shajarian">
|
||||
<font>
|
||||
<Font size="10.0" />
|
||||
</font>
|
||||
@@ -66,7 +67,7 @@
|
||||
<Label layoutX="275.0" layoutY="23.0" text="01:52" />
|
||||
|
||||
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#1f93ff00" height="48.0" layoutX="7.0" layoutY="8.0" stroke="BLACK" strokeType="INSIDE" width="567.0" />
|
||||
<Button layoutX="366.0" layoutY="19.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="86.0" text="Add To PlayList">
|
||||
<Button fx:id="5" layoutX="366.0" layoutY="19.0" mnemonicParsing="false" onAction="#addToPlaylist" prefHeight="26.0" prefWidth="86.0" text="Add To PlayList" userData="Saghi Bia --- M.Shajarian">
|
||||
<font>
|
||||
<Font size="10.0" />
|
||||
</font>
|
||||
@@ -89,7 +90,7 @@
|
||||
|
||||
<Label layoutX="275.0" layoutY="23.0" text="16:59" />
|
||||
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#1f93ff00" height="48.0" layoutX="7.0" layoutY="8.0" stroke="BLACK" strokeType="INSIDE" width="567.0" />
|
||||
<Button layoutX="365.0" layoutY="18.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="86.0" text="Add To PlayList">
|
||||
<Button fx:id="6" layoutX="365.0" layoutY="18.0" mnemonicParsing="false" onAction="#addToPlaylist" prefHeight="26.0" prefWidth="86.0" text="Add To PlayList" userData="Mara Be Hich Bedady --- M.Shajarian">
|
||||
<font>
|
||||
<Font size="10.0" />
|
||||
</font>
|
||||
@@ -110,6 +111,27 @@
|
||||
<children>
|
||||
<Label layoutX="32.0" layoutY="50.0" text="Now Play :" />
|
||||
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#ffffff00" height="64.0" layoutX="8.0" layoutY="27.0" stroke="BLACK" strokeType="INSIDE" width="567.0" />
|
||||
<Button fx:id="show" layoutX="367.0" layoutY="46.0" mnemonicParsing="false" onAction="#openPlaylistWindow" prefHeight="26.0" prefWidth="190.0" text="Show PlayList" />
|
||||
<Label fx:id="nowPlayingLabel" alignment="CENTER" layoutX="99.0" layoutY="51.0" prefHeight="18.0" prefWidth="126.0" text="Now" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
</children>
|
||||
</HBox>
|
||||
<HBox prefHeight="110.0" prefWidth="578.0">
|
||||
<children>
|
||||
<AnchorPane prefHeight="102.0" prefWidth="578.0">
|
||||
<children>
|
||||
<Label fx:id="timeLabel" layoutX="142.0" layoutY="59.0" prefHeight="18.0" prefWidth="42.0" text="t" />
|
||||
<Label layoutX="30.0" layoutY="59.0" prefHeight="18.0" prefWidth="42.0" text="Time" />
|
||||
<Label layoutX="243.0" layoutY="40.0" prefHeight="18.0" prefWidth="49.0" text="Volume" />
|
||||
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e1e2e300" height="100.0" layoutX="7.0" stroke="BLACK" strokeType="INSIDE" width="567.0" />
|
||||
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#e6eaed00" height="74.0" layoutX="11.0" layoutY="12.0" stroke="BLACK" strokeType="INSIDE" width="190.0" />
|
||||
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#ffffff00" height="74.0" layoutX="205.0" layoutY="12.0" stroke="BLACK" strokeType="INSIDE" width="126.0" />
|
||||
<ToggleButton fx:id="themeButton" layoutX="365.0" layoutY="36.0" mnemonicParsing="false" onAction="#toggleTheme" prefHeight="26.0" prefWidth="190.0" text="Change Theme" />
|
||||
<Slider fx:id="timeSlider" layoutX="28.0" layoutY="42.0" prefHeight="14.0" prefWidth="156.0" />
|
||||
|
||||
<Slider fx:id="volumeSlider" layoutX="215.0" layoutY="19.0" orientation="VERTICAL" prefHeight="58.0" prefWidth="14.0" />
|
||||
|
||||
</children>
|
||||
</AnchorPane>
|
||||
</children>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.ListView?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.text.Font?>
|
||||
|
||||
|
||||
<VBox
|
||||
xmlns="http://javafx.com/javafx/26"
|
||||
xmlns:fx="http://javafx.com/fxml/1"
|
||||
fx:controller="sbu.javafx.MusicController"
|
||||
|
||||
maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" >
|
||||
<children>
|
||||
<Label alignment="CENTER" prefHeight="83.0" prefWidth="600.0" text="Play List">
|
||||
<font>
|
||||
<Font name="B Davat" size="31.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<HBox prefHeight="342.0" prefWidth="600.0">
|
||||
<children>
|
||||
<AnchorPane prefHeight="271.0" prefWidth="600.0">
|
||||
<children>
|
||||
<ListView fx:id="playlistview" layoutX="200.0" layoutY="9.0" prefHeight="298.0" prefWidth="200.0" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
</children>
|
||||
</HBox>
|
||||
</children>
|
||||
</VBox>
|
||||
@@ -0,0 +1,20 @@
|
||||
.root {
|
||||
-fx-background-color: #1e1e1e;
|
||||
}
|
||||
|
||||
.label {
|
||||
-fx-text-fill: white;
|
||||
}
|
||||
|
||||
.button {
|
||||
-fx-background-color: #333333;
|
||||
-fx-text-fill: white;
|
||||
}
|
||||
|
||||
.list-view {
|
||||
-fx-background-color: #2b2b2b;
|
||||
}
|
||||
|
||||
.slider {
|
||||
-fx-control-inner-background: #333333;
|
||||
}
|
||||
Reference in New Issue
Block a user