forked from AdvancedProgramming1404/HW-07-JavaFX
Compare commits
1
Commits
develop
...
783b24c82a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
783b24c82a |
@@ -4,6 +4,7 @@ import javafx.application.Application;
|
|||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.image.Image;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
public class Main extends Application {
|
public class Main extends Application {
|
||||||
@@ -19,7 +20,7 @@ public class Main extends Application {
|
|||||||
new Song("Blinding Lights", "The Weeknd","/musics/The-Weeknd-Blinding-Lights-128.mp3"),
|
new Song("Blinding Lights", "The Weeknd","/musics/The-Weeknd-Blinding-Lights-128.mp3"),
|
||||||
new Song("Comme des enfants", "Coert De Pirate","/musics/Coeur-De-Pirate-Comme-des-enfants-128.mp3"),
|
new Song("Comme des enfants", "Coert De Pirate","/musics/Coeur-De-Pirate-Comme-des-enfants-128.mp3"),
|
||||||
new Song("Slow Down", "GRAE", "/musics/GRAE-Slow-Down-128.mp3"),
|
new Song("Slow Down", "GRAE", "/musics/GRAE-Slow-Down-128.mp3"),
|
||||||
new Song("Cigarettes out the Window", "TV Girl", "/music/TV-Girl-Cigarettes-out-the-Window-128.mp3"),
|
new Song("Cigarettes out the Window", "TV Girl", "/musics/TV-Girl-Cigarettes-out-the-Window-128.mp3"),
|
||||||
new Song("deja vu", "Olivia Rodirgo", "/musics/Olivia-Rodrigo-deja-vu-320.mp3"),
|
new Song("deja vu", "Olivia Rodirgo", "/musics/Olivia-Rodrigo-deja-vu-320.mp3"),
|
||||||
new Song("The Line", "Twoenty One Pilots", "/musics/Twenty-One-Pilots-The-Line-128.mp3"),
|
new Song("The Line", "Twoenty One Pilots", "/musics/Twenty-One-Pilots-The-Line-128.mp3"),
|
||||||
new Song("Bones", "Imagine Dragons", "/musics/Imagine-Dragons-Bones-128.mp3"),
|
new Song("Bones", "Imagine Dragons", "/musics/Imagine-Dragons-Bones-128.mp3"),
|
||||||
@@ -37,6 +38,9 @@ public class Main extends Application {
|
|||||||
|
|
||||||
primaryStage.setTitle("Music Player");
|
primaryStage.setTitle("Music Player");
|
||||||
primaryStage.setScene(scene);
|
primaryStage.setScene(scene);
|
||||||
|
primaryStage.getIcons().add(
|
||||||
|
new Image(getClass().getResourceAsStream("/image/icon.png"))
|
||||||
|
);
|
||||||
primaryStage.setResizable(false);
|
primaryStage.setResizable(false);
|
||||||
primaryStage.show();
|
primaryStage.show();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package sbu.javafx;
|
|||||||
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.image.Image;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -13,9 +15,38 @@ public class MusicApp extends Application {
|
|||||||
public void start(Stage stage) throws IOException
|
public void start(Stage stage) throws IOException
|
||||||
{
|
{
|
||||||
FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
|
FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
|
||||||
Scene scene = new Scene(fxmlLoader.load());
|
Parent root = fxmlLoader.load();
|
||||||
|
|
||||||
|
MusicController controller = fxmlLoader.getController();
|
||||||
|
|
||||||
|
controller.musicsListView.getItems().addAll(
|
||||||
|
new Song("Blinding Lights", "The Weeknd","/musics/The-Weeknd-Blinding-Lights-128.mp3"),
|
||||||
|
new Song("Comme des enfants", "Coert De Pirate","/musics/Coeur-De-Pirate-Comme-des-enfants-128.mp3"),
|
||||||
|
new Song("Slow Down", "GRAE", "/musics/GRAE-Slow-Down-128.mp3"),
|
||||||
|
new Song("Cigarettes out the Window", "TV Girl", "/musics/TV-Girl-Cigarettes-out-the-Window-128.mp3"),
|
||||||
|
new Song("deja vu", "Olivia Rodirgo", "/musics/Olivia-Rodrigo-deja-vu-320.mp3"),
|
||||||
|
new Song("The Line", "Twoenty One Pilots", "/musics/Twenty-One-Pilots-The-Line-128.mp3"),
|
||||||
|
new Song("Bones", "Imagine Dragons", "/musics/Imagine-Dragons-Bones-128.mp3"),
|
||||||
|
new Song("Bunker", "Balthazar", "/musics/Balthazar-Bunker-128.mp3"),
|
||||||
|
new Song("As It Was", "Harry Styles", "/musics/Harry-Styles-As-It-Was-320.mp3")
|
||||||
|
);
|
||||||
|
|
||||||
|
controller.setupListView(controller.musicsListView);
|
||||||
|
controller.setupListView(controller.playlistListView);
|
||||||
|
|
||||||
|
Scene scene = new Scene(root, 500, 600);
|
||||||
|
|
||||||
|
scene.getStylesheets().add(
|
||||||
|
getClass().getResource("/css/dark.css").toExternalForm()
|
||||||
|
);
|
||||||
|
|
||||||
stage.setTitle("Music Player");
|
stage.setTitle("Music Player");
|
||||||
|
stage.getIcons().add(
|
||||||
|
new Image(getClass().getResourceAsStream("/image/icon.png"))
|
||||||
|
);
|
||||||
stage.setScene(scene);
|
stage.setScene(scene);
|
||||||
|
stage.setResizable(false);
|
||||||
|
|
||||||
//TODO: add icon to the stage
|
//TODO: add icon to the stage
|
||||||
stage.show();
|
stage.show();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,12 @@ public class MusicController {
|
|||||||
@FXML
|
@FXML
|
||||||
Button themeToggleBtn;
|
Button themeToggleBtn;
|
||||||
@FXML
|
@FXML
|
||||||
|
Button nextBtn;
|
||||||
|
@FXML
|
||||||
|
Button preveBtn;
|
||||||
|
@FXML
|
||||||
|
Button repeatBtn;
|
||||||
|
@FXML
|
||||||
private Label messageLabel;
|
private Label messageLabel;
|
||||||
private boolean isDarkMode=true;
|
private boolean isDarkMode=true;
|
||||||
private boolean isPlaying=false;
|
private boolean isPlaying=false;
|
||||||
@@ -76,6 +82,46 @@ public class MusicController {
|
|||||||
|
|
||||||
playPauseBtn.setGraphic(playIcon);
|
playPauseBtn.setGraphic(playIcon);
|
||||||
|
|
||||||
|
nextBtn.setOnAction(e ->{
|
||||||
|
|
||||||
|
player.pause();
|
||||||
|
|
||||||
|
if(isOpen) {
|
||||||
|
currentSong=playlistListView.getItems().get(findIndex(playlistListView,currentSong)+1);
|
||||||
|
setPlayer(currentSong).play();
|
||||||
|
}else{
|
||||||
|
currentSong=musicsListView.getItems().get(findIndex(musicsListView,currentSong)+1);
|
||||||
|
setPlayer(currentSong).play();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
preveBtn.setOnAction(e ->{
|
||||||
|
|
||||||
|
player.pause();
|
||||||
|
|
||||||
|
if(isOpen) {
|
||||||
|
currentSong=playlistListView.getItems().get(findIndex(playlistListView,currentSong)-1);
|
||||||
|
setPlayer(currentSong).play();
|
||||||
|
}else{
|
||||||
|
currentSong=musicsListView.getItems().get(findIndex(musicsListView,currentSong)-1);
|
||||||
|
setPlayer(currentSong).play();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
repeatBtn.setOnAction(e->{
|
||||||
|
if(!isRepeat){
|
||||||
|
repeatBtn.setStyle(
|
||||||
|
"-fx-border-color: blue;" +
|
||||||
|
"-fx-border-width: 2px;" +
|
||||||
|
"-fx-border-radius: 100em;"
|
||||||
|
);
|
||||||
|
isRepeat=true;
|
||||||
|
}else{
|
||||||
|
repeatBtn.setStyle("-fx-border-color: transparent;");
|
||||||
|
isRepeat=false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setupListView(ListView<Song> listView) {
|
public void setupListView(ListView<Song> listView) {
|
||||||
@@ -131,11 +177,11 @@ public class MusicController {
|
|||||||
@FXML
|
@FXML
|
||||||
public void toggleTheme(ActionEvent event) {
|
public void toggleTheme(ActionEvent event) {
|
||||||
if (isDarkMode) {
|
if (isDarkMode) {
|
||||||
themeToggleBtn.setText("Light");
|
themeToggleBtn.setText("Dark");
|
||||||
changeTheme();
|
changeTheme();
|
||||||
isDarkMode = false;
|
isDarkMode = false;
|
||||||
} else {
|
} else {
|
||||||
themeToggleBtn.setText("Dark");
|
themeToggleBtn.setText("Light");
|
||||||
changeTheme();
|
changeTheme();
|
||||||
isDarkMode = true;
|
isDarkMode = true;
|
||||||
}
|
}
|
||||||
@@ -162,41 +208,63 @@ public class MusicController {
|
|||||||
//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.
|
||||||
|
|
||||||
|
|
||||||
@FXML
|
public MediaPlayer setPlayer(Song song) {//این تابع برای ست کردن مدیای در حال پخشه
|
||||||
public void handlePlayAction() {
|
|
||||||
//Update labels, change button icons, etc.
|
|
||||||
}
|
|
||||||
|
|
||||||
public void play(ActionEvent event, Song song){
|
|
||||||
|
|
||||||
currentSong=song;
|
|
||||||
nowPlayingLabel.setText(song.getName());
|
nowPlayingLabel.setText(song.getName());
|
||||||
|
|
||||||
if (!isPlaying)
|
|
||||||
playPause(event);
|
|
||||||
|
|
||||||
// کد پخش موسیقی
|
|
||||||
path = getClass()
|
path = getClass()
|
||||||
.getResource(song.getPath())
|
.getResource(song.getPath())
|
||||||
.toExternalForm();
|
.toExternalForm();
|
||||||
media = new Media(path);
|
media = new Media(path);
|
||||||
player = new MediaPlayer(media);
|
player = new MediaPlayer(media);
|
||||||
|
|
||||||
|
//این برا اینه که تموم شد بره بعدی
|
||||||
|
player.setOnEndOfMedia(() -> {
|
||||||
|
if(isRepeat){
|
||||||
|
player.seek(player.getStartTime());
|
||||||
player.play();
|
player.play();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
nextBtn.fire();
|
||||||
|
});
|
||||||
|
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void play(ActionEvent event, Song song){
|
||||||
|
|
||||||
|
currentSong = song;
|
||||||
|
|
||||||
|
if(player != null){
|
||||||
|
player.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
setPlayer(currentSong);
|
||||||
|
|
||||||
|
player.play();
|
||||||
|
|
||||||
|
isPlaying = true;
|
||||||
|
|
||||||
|
playPauseBtn.setGraphic(pauseIcon);
|
||||||
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void playPause(ActionEvent a){
|
public void playPause(ActionEvent a){
|
||||||
|
|
||||||
|
if(player == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(isPlaying){
|
if(isPlaying){
|
||||||
playPauseBtn.setGraphic(playIcon);
|
playPauseBtn.setGraphic(playIcon);
|
||||||
|
|
||||||
|
player.pause();
|
||||||
isPlaying = false;
|
isPlaying = false;
|
||||||
|
|
||||||
//کد توقف
|
} else {
|
||||||
player.pause();
|
|
||||||
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
playPauseBtn.setGraphic(pauseIcon);
|
playPauseBtn.setGraphic(pauseIcon);
|
||||||
|
|
||||||
|
player.play();
|
||||||
isPlaying = true;
|
isPlaying = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,12 +304,12 @@ public class MusicController {
|
|||||||
|
|
||||||
public void openPlaylistWindow(ActionEvent e) {
|
public void openPlaylistWindow(ActionEvent e) {
|
||||||
|
|
||||||
if(isPlaying)
|
if(isPlaying) {
|
||||||
playPause(e);
|
playPause(e);
|
||||||
|
}
|
||||||
|
|
||||||
if(!isOpen){
|
if(!isOpen){
|
||||||
|
// باز کردن پلی لیست
|
||||||
// باز کردن پلیلیست
|
|
||||||
if(playlistListView.getItems().isEmpty()){
|
if(playlistListView.getItems().isEmpty()){
|
||||||
|
|
||||||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||||||
@@ -266,9 +334,7 @@ public class MusicController {
|
|||||||
playlistListView.setManaged(true);
|
playlistListView.setManaged(true);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// برگشت به صفحه اصلی
|
// برگشت به صفحه اصلی
|
||||||
|
|
||||||
isOpen = false;
|
isOpen = false;
|
||||||
|
|
||||||
openPlaylistBtn.setText("Open Playlist");
|
openPlaylistBtn.setText("Open Playlist");
|
||||||
@@ -280,6 +346,18 @@ public class MusicController {
|
|||||||
playlistListView.setManaged(false);
|
playlistListView.setManaged(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int findIndex(ListView<Song> listView, Song song){
|
||||||
|
for(int i = 0; i < listView.getItems().size(); i++){
|
||||||
|
|
||||||
|
if(listView.getItems().get(i).equals(song)){
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 3.7 KiB |
@@ -92,7 +92,7 @@
|
|||||||
</graphic>
|
</graphic>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button fx:id="repeat"
|
<Button fx:id="repeatBtn"
|
||||||
styleClass="circle-button">
|
styleClass="circle-button">
|
||||||
|
|
||||||
<graphic>
|
<graphic>
|
||||||
|
|||||||
Reference in New Issue
Block a user