implement MusicController.java
This commit is contained in:
@@ -1,31 +1,160 @@
|
||||
package sbu.javafx;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.control.cell.PropertyValueFactory;
|
||||
import javafx.scene.media.Media;
|
||||
import javafx.scene.media.MediaPlayer;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MusicController {
|
||||
|
||||
// TODO: Define your UI components using the @FXML annotation.
|
||||
@FXML private Label nowPlayingLabel;
|
||||
@FXML private TableView<Song> songsTable;
|
||||
@FXML private TableColumn<Song, String> titleCol;
|
||||
@FXML private TableColumn<Song, String> artistCol;
|
||||
@FXML private TableColumn<Song, String> durationCol;
|
||||
|
||||
//TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing.
|
||||
// There is no connection between Song and Button so we should use void
|
||||
@FXML private TableColumn<Song, Void> addCol;
|
||||
@FXML private TableColumn<Song, Void> playCol;
|
||||
|
||||
private Song nowPlayingSong = null;
|
||||
private final ObservableList<Song> playlist = FXCollections.observableArrayList();
|
||||
private MediaPlayer mediaPlayer;
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
// connect song name,artist name,duration to song class
|
||||
titleCol.setCellValueFactory(new PropertyValueFactory<>("title"));
|
||||
artistCol.setCellValueFactory(new PropertyValueFactory<>("artistName"));
|
||||
durationCol.setCellValueFactory(new PropertyValueFactory<>("duration"));
|
||||
|
||||
// Add button
|
||||
addCol.setCellFactory(param -> new TableCell<>() {
|
||||
private final Button btn = new Button("Add");
|
||||
|
||||
{
|
||||
btn.getStyleClass().add("add-button");
|
||||
|
||||
btn.setOnAction(event -> {
|
||||
Song song = getTableView().getItems().get(getIndex());
|
||||
addToPlaylist(song);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateItem(Void item, boolean empty) {
|
||||
super.updateItem(item, empty);
|
||||
|
||||
if (empty) {
|
||||
setGraphic(null);
|
||||
setText(null);
|
||||
} else {
|
||||
Song song = getTableView().getItems().get(getIndex());
|
||||
|
||||
if (song.isAdded()) {
|
||||
setGraphic(null);
|
||||
setText("Added ✓");
|
||||
setStyle("-fx-text-fill: #2ecc71; -fx-font-weight: bold;");
|
||||
} else {
|
||||
setGraphic(btn);
|
||||
setText(null);
|
||||
setStyle("");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Play button cell
|
||||
playCol.setCellFactory(param -> new TableCell<>() {
|
||||
private final Button btn = new Button("Play");
|
||||
{
|
||||
btn.setOnAction(e -> {
|
||||
nowPlayingSong = getTableView().getItems().get(getIndex());
|
||||
handlePlayAction();
|
||||
});
|
||||
}
|
||||
@Override
|
||||
protected void updateItem(Void item, boolean empty) {
|
||||
super.updateItem(item, empty);
|
||||
setGraphic(empty ? null : btn);
|
||||
}
|
||||
});
|
||||
|
||||
ObservableList<Song> songList = FXCollections.observableArrayList(
|
||||
new Song("Vasiat2", "Naaji", "5:26", "/Musics/Naaji.mp3"),
|
||||
new Song("SogandBe", "Unknown", "1:55", "/Musics/SogandBe.mp3"),
|
||||
new Song("Telo", "Hossein", "3:07", "/Musics/Telo.mp3")
|
||||
);
|
||||
songsTable.setItems(songList);
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void handlePlayAction() {
|
||||
//Update labels, change button icons, etc.
|
||||
if (nowPlayingSong == null) return;
|
||||
|
||||
try {
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.stop();
|
||||
mediaPlayer.dispose();
|
||||
}
|
||||
|
||||
String filePath = nowPlayingSong.getFilePath();
|
||||
var resource = getClass().getResource(filePath);
|
||||
|
||||
if (resource == null) {
|
||||
System.out.println("Error: File not found in resource path! Path checked: " + filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
String mediaUrl = resource.toExternalForm();
|
||||
|
||||
Media media = new Media(mediaUrl);
|
||||
mediaPlayer = new MediaPlayer(media);
|
||||
|
||||
nowPlayingLabel.setText(nowPlayingSong.getTitle() + " - " + nowPlayingSong.getArtistName());
|
||||
mediaPlayer.play();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error while playing audio file: ");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Logic to add a specific song to the user's playlist.
|
||||
|
||||
public void addToPlaylist(String songName) {
|
||||
//Add the song to a list or update a ListView.
|
||||
public void addToPlaylist(Song song) {
|
||||
if (!playlist.contains(song)) {
|
||||
playlist.add(song);
|
||||
song.setAdded(true);
|
||||
songsTable.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
|
||||
|
||||
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 controller = loader.getController();
|
||||
controller.setPlaylist(playlist);
|
||||
|
||||
Stage stage = new Stage();
|
||||
stage.setTitle("Playlist");
|
||||
Scene scene = new Scene(root);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user