Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.layout.GridPane;
- import javafx.scene.media.Media;
- import javafx.scene.media.MediaPlayer;
- import javafx.stage.FileChooser;
- import javafx.stage.Stage;
- public class JavaFXMediaPlayer extends Application {
- private Media media;
- private MediaPlayer player;
- private String songstatus = "DEFAULT_STATE";
- private Button
- bprev,
- bnext,
- bplaypause,
- bsongchooser;
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage stage) throws Exception {
- stage.setTitle("Media Player");
- bprev = new Button();
- bnext = new Button();
- bplaypause = new Button();
- bsongchooser = new Button();
- bprev.setText("Previous");
- bnext.setText("Next");
- bplaypause.setText("Play");
- bsongchooser.setText("Choose Song");
- bprev.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- prev();
- }
- });
- bnext.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- next();
- }
- });
- bplaypause.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- playpause();
- }
- });
- bsongchooser.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- FileChooser fchoose = new FileChooser();
- fchoose.setTitle("Choose a song");
- //Define file filters
- FileChooser.ExtensionFilter mp3filter = new FileChooser.ExtensionFilter("MP3 Files", "*.mp3");
- FileChooser.ExtensionFilter wavfilter = new FileChooser.ExtensionFilter("WAV Files", "*.wav");
- FileChooser.ExtensionFilter aacfilter = new FileChooser.ExtensionFilter("AAC Files", "*.aac");
- FileChooser.ExtensionFilter allfilter = new FileChooser.ExtensionFilter("Audio Files", "*.wav", "*.mp3", "*.aac");
- //Apply the file filters to the choose file dialog
- fchoose.getExtensionFilters().add(mp3filter);
- fchoose.getExtensionFilters().add(wavfilter);
- fchoose.getExtensionFilters().add(aacfilter);
- fchoose.getExtensionFilters().add(allfilter);
- //Open the dialog and set the song equal to the file chosen
- File song = fchoose.showOpenDialog(stage);
- //Use the song file to construct the media and player objects
- try {
- // StringBuilder builder = new StringBuilder(song.toURI().toString());
- // String ssong = "";
- //
- // builder.insert(5, "//");
- // ssong = builder.toString();
- System.out.println(song.toURI().toString());
- media = new Media(song.toURI().toString());
- player = new MediaPlayer(media);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- GridPane root = new GridPane();
- root.add(bprev, 1, 1);
- root.add(bnext, 1, 2);
- root.add(bplaypause, 1, 3);
- root.add(bsongchooser, 1, 4);
- stage.setScene(new Scene(root, 750, 750));
- stage.show();
- }
- public int playpause() {
- //checks the current state of the song and reacts appropriately
- if (songstatus.equals("PLAY") || songstatus.equals("DEFAULT_STATE")) {
- if (songstatus.equals("DEFAULT_STATE")) {
- songstatus = "PLAY";
- }
- bplaypause.setText("Pause");
- }
- else if (songstatus.equals("PLAY")) {
- player.pause();
- songstatus = "PAUSE";
- bplaypause.setText("Play");
- }
- else if (songstatus.equals("STOP")) {
- player.play();
- }
- else {
- Exception e = new Exception();
- e.printStackTrace();
- songstatus.equals("ERROR");
- return 1;
- }
- return 0;
- }
- public int stopSong() {
- player.stop();
- songstatus = "STOP";
- return 0;
- }
- public int next() {
- return 0;
- }
- public int prev() {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement