Compare commits

..
3 Commits
Author SHA1 Message Date
bitahajati f47d3ec826 Add MediaPlayer 2026-05-26 17:17:26 +03:30
bitahajati 2842cd414c Complete fxml & Cotroller class 2026-05-24 23:49:54 +03:30
bitahajati c3205138cb Complete class Launcher & MusicApp 2026-05-24 16:06:03 +03:30
13 changed files with 190 additions and 17 deletions
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
+7
View File
@@ -24,6 +24,13 @@
<artifactId>javafx-fxml</artifactId> <artifactId>javafx-fxml</artifactId>
<version>21</version> <version>21</version>
</dependency> </dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>21</version>
</dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
+2 -1
View File
@@ -4,5 +4,6 @@ import javafx.application.Application;
public class Launcher{ public class Launcher{
public static void main(String[] args) { public static void main(String[] args) {
// TODO: Launch the JavaFX application by calling Application.launch(Main.class) } Application.launch(MusicApp.class);
}
} }
-1
View File
@@ -16,7 +16,6 @@ public class MusicApp extends Application {
Scene scene = new Scene(fxmlLoader.load()); Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Music Player"); stage.setTitle("Music Player");
stage.setScene(scene); stage.setScene(scene);
//TODO: add icon to the stage
stage.show(); stage.show();
} }
+129 -9
View File
@@ -1,31 +1,151 @@
package sbu.javafx; package sbu.javafx;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent; import javafx.scene.control.ListView;
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.io.IOException;
import java.util.ArrayList;
public class MusicController { public class MusicController {
// TODO: Define your UI components using the @FXML annotation. // TODO: Define your UI components using the @FXML annotation.
@FXML private ListView<String> lvSongs;
@FXML private Label lblStatus;
@FXML private Button btnPlayStop;
@FXML private Button btnViewPlaylist;
private MediaPlayer mediaPlayer;
private ArrayList<String> playlist = new ArrayList<>();
private String[] songsArray = {"Arcade Duncan Laurence 3.07",
"How i love you Engelbert 4.20",
"Memories Conan Gray 4.09",
"Night Changes One Direction 3.47",
"Careless Whisper George Michael 5.00"
};
@FXML public void initialize() { lvSongs.getItems().setAll(songsArray); }
//TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing. //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
public String songName(int a)
{
String songName = "";
if (a == 0) songName = "Arcade";
else if (a == 1) songName = "How i love you";
else if (a == 2) songName = "Memmories";
else if (a == 3) songName = "Night Changes";
else songName = "Careless Whisper";
return songName;
}
@FXML @FXML
public void handlePlayAction() { public void handlePlayAction()
//Update labels, change button icons, etc. {
String selectedSong = lvSongs.getSelectionModel().getSelectedItem();
if (selectedSong == null)
{
lblStatus.setText("Please choose a song");
return;
}
String filePath = "src/main/resources/sbu.javafx/songs/" + songName(lvSongs.getSelectionModel().getSelectedIndex()) + ".mp3";
File file = new File(filePath);
boolean isPlayingOrPaused = (mediaPlayer != null &&
(mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING ||
mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED));
if (isPlayingOrPaused)
{
if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING)
{
mediaPlayer.pause();
btnPlayStop.setText("Paused ⏸");
lblStatus.setText("Playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex()));
return;
}
else if (mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED)
{
mediaPlayer.play();
btnPlayStop.setText("Play ▶");
lblStatus.setText("Paused");
return;
}
}
if (mediaPlayer != null)
{
mediaPlayer.dispose();
mediaPlayer = null;
}
try {
Media media = new Media(file.toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setOnEndOfMedia(new Runnable() {
@Override
public void run() {
mediaPlayer.stop();
btnPlayStop.setText("Play ▶");
}
});
mediaPlayer.play();
btnPlayStop.setText("Paused ⏸");
lblStatus.setText("Playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex()));
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
//TODO: Logic to add a specific song to the user's playlist. //TODO: Logic to add a specific song to the user's playlist.
public void addToPlaylist(String songName) { public void addToPlaylist()
//Add the song to a list or update a ListView. {
String selectedSong = lvSongs.getSelectionModel().getSelectedItem();
if (selectedSong == null)
{
lblStatus.setText("Please choose a song");
return;
}
if (!playlist.contains(selectedSong))
{
playlist.add(selectedSong);
lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " added to playList");
}
else lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " already exists");
} }
// TODO:This method should open a new window or change the current scene to display the "Playlist" page. // TODO:This method should open a new window or change the current scene to display the "Playlist" page.
public void openPlaylistWindow() { public void openPlaylistWindow()
//Create a new stage and show the playlist UI. {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Playlist-view.fxml"));
Scene scene = null;
try {
scene = new Scene(loader.load());
} catch (IOException e) {
throw new RuntimeException(e);
}
PlayListController controller = loader.getController();
controller.setPlaylist(playlist);
Stage stage = new Stage();
stage.setTitle("Playlist");
stage.setScene(scene);
stage.show();
} }
} }
@@ -0,0 +1,14 @@
package sbu.javafx;
import java.util.ArrayList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
public class PlayListController
{
@FXML private ListView<String> lvPlaylist;
public void setPlaylist(ArrayList<String> playlist)
{
lvPlaylist.getItems().addAll(playlist);
}
}
+18 -2
View File
@@ -1,8 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/fxml" alignment="CENTER" spacing="10"> <?import javafx.scene.layout.HBox?>
<!-- TODO: Add your code here! --> <VBox alignment="CENTER" spacing="15.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sbu.javafx.MusicController">
<Label fx:id="lblStatus" text="Music Player" style="-fx-font-size: 18px; -fx-font-weight: bold;"/>
<ListView fx:id="lvSongs" prefHeight="200.0" prefWidth="350.0"/>
<HBox spacing="10" alignment="CENTER">
<Button fx:id="btnPlayStop" text="PLay ▶" onAction="#handlePlayAction" prefWidth="300"/>
</HBox>
<VBox spacing="10"/>
<Button text="Add to playlist " onAction="#addToPlaylist" prefWidth="300"/>
<VBox spacing="10"/>
<Button fx:id="btnViewPlaylist" text="Show Playlist" onAction="#openPlaylistWindow" prefWidth="300"/>
</VBox> </VBox>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="TOP_CENTER" spacing="10.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sbu.javafx.PlayListController">
<Label text="Your PlayList : " style="-fx-font-size: 20px; -fx-font-weight: bold;"/>
<ListView fx:id="lvPlaylist" prefHeight="300.0" prefWidth="300.0"/>
</VBox>
Binary file not shown.
Binary file not shown.