Add MediaPlayer
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<junit.version>5.10.2</junit.version> </properties>
|
<junit.version>5.10.2</junit.version> </properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -24,8 +24,15 @@
|
|||||||
<artifactId>javafx-fxml</artifactId>
|
<artifactId>javafx-fxml</artifactId>
|
||||||
<version>21</version>
|
<version>21</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
|
||||||
<dependency>
|
<groupId>org.openjfx</groupId>
|
||||||
|
<artifactId>javafx-media</artifactId>
|
||||||
|
<version>21</version>
|
||||||
|
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
package sbu.javafx;
|
package sbu.javafx;
|
||||||
|
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.event.ActionEvent;
|
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
import javafx.scene.input.MouseEvent;
|
import javafx.scene.media.Media;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.media.MediaPlayer;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@@ -19,12 +18,16 @@ 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 ListView<String> lvSongs;
|
||||||
@FXML private Label lblStatus;
|
@FXML private Label lblStatus;
|
||||||
|
@FXML private Button btnPlayStop;
|
||||||
|
@FXML private Button btnViewPlaylist;
|
||||||
|
|
||||||
|
private MediaPlayer mediaPlayer;
|
||||||
|
|
||||||
private ArrayList<String> playlist = new ArrayList<>();
|
private ArrayList<String> playlist = new ArrayList<>();
|
||||||
|
|
||||||
private String[] songsArray = {"Arcade Duncan Laurence 3.07",
|
private String[] songsArray = {"Arcade Duncan Laurence 3.07",
|
||||||
"How i love you Engelbert 4.20",
|
"How i love you Engelbert 4.20",
|
||||||
"Memmories Conan Gray 4.09",
|
"Memories Conan Gray 4.09",
|
||||||
"Night Changes One Direction 3.47",
|
"Night Changes One Direction 3.47",
|
||||||
"Careless Whisper George Michael 5.00"
|
"Careless Whisper George Michael 5.00"
|
||||||
};
|
};
|
||||||
@@ -55,7 +58,55 @@ public class MusicController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
lblStatus.setText(" is playing : " + songName(lvSongs.getSelectionModel().getSelectedIndex()));
|
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.
|
||||||
@@ -74,7 +125,7 @@ public class MusicController {
|
|||||||
playlist.add(selectedSong);
|
playlist.add(selectedSong);
|
||||||
lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " added to playList");
|
lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " added to playList");
|
||||||
}
|
}
|
||||||
else lblStatus.setText(songName(lvSongs.getSelectionModel().getSelectedIndex()) + " is already exists");
|
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.
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<?import javafx.scene.layout.HBox?>
|
<?import javafx.scene.layout.HBox?>
|
||||||
|
|
||||||
<VBox alignment="CENTER" spacing="15.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sbu.javafx.MusicController">
|
<VBox alignment="CENTER" spacing="15.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sbu.javafx.MusicController">
|
||||||
<Label fx:id="lblStatus" text="Musiv Player" style="-fx-font-size: 18px; -fx-font-weight: bold;"/>
|
<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"/>
|
<ListView fx:id="lvSongs" prefHeight="200.0" prefWidth="350.0"/>
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user