Advertisement
lucidpineapple

withoutbuilder

Dec 15th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4.  
  5. import javafx.application.Application;
  6. import javafx.event.ActionEvent;
  7. import javafx.event.EventHandler;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.layout.GridPane;
  11. import javafx.scene.media.Media;
  12. import javafx.scene.media.MediaPlayer;
  13. import javafx.stage.FileChooser;
  14. import javafx.stage.Stage;
  15.  
  16. public class JavaFXMediaPlayer extends Application {
  17.    
  18.     private Media media;
  19.     private MediaPlayer player;
  20.     private String songstatus = "DEFAULT_STATE";
  21.    
  22.     private Button
  23.     bprev,
  24.     bnext,
  25.     bplaypause,
  26.     bsongchooser;
  27.    
  28.     public static void main(String[] args) {
  29.         launch(args);
  30.     }
  31.    
  32.     @Override
  33.     public void start(Stage stage) throws Exception {
  34.         stage.setTitle("Media Player");
  35.        
  36.         bprev = new Button();
  37.         bnext = new Button();
  38.         bplaypause = new Button();
  39.         bsongchooser = new Button();
  40.        
  41.         bprev.setText("Previous");
  42.         bnext.setText("Next");
  43.         bplaypause.setText("Play");
  44.         bsongchooser.setText("Choose Song");
  45.        
  46.         bprev.setOnAction(new EventHandler<ActionEvent>() {
  47.             @Override
  48.             public void handle(ActionEvent event) {
  49.                 prev();
  50.             }
  51.         });
  52.        
  53.         bnext.setOnAction(new EventHandler<ActionEvent>() {
  54.             @Override
  55.             public void handle(ActionEvent event) {
  56.                 next();
  57.             }
  58.         });
  59.        
  60.         bplaypause.setOnAction(new EventHandler<ActionEvent>() {
  61.             @Override
  62.             public void handle(ActionEvent event) {
  63.                 playpause();
  64.             }
  65.         });
  66.        
  67.         bsongchooser.setOnAction(new EventHandler<ActionEvent>() {
  68.             @Override
  69.             public void handle(ActionEvent event) {
  70.                 FileChooser fchoose = new FileChooser();
  71.                
  72.                 fchoose.setTitle("Choose a song");
  73.                
  74.                 //Define file filters
  75.                 FileChooser.ExtensionFilter mp3filter = new FileChooser.ExtensionFilter("MP3 Files", "*.mp3");
  76.                 FileChooser.ExtensionFilter wavfilter = new FileChooser.ExtensionFilter("WAV Files", "*.wav");
  77.                 FileChooser.ExtensionFilter aacfilter = new FileChooser.ExtensionFilter("AAC Files", "*.aac");
  78.                 FileChooser.ExtensionFilter allfilter = new FileChooser.ExtensionFilter("Audio Files", "*.wav", "*.mp3", "*.aac");
  79.                
  80.                 //Apply the file filters to the choose file dialog
  81.                 fchoose.getExtensionFilters().add(mp3filter);
  82.                 fchoose.getExtensionFilters().add(wavfilter);
  83.                 fchoose.getExtensionFilters().add(aacfilter);
  84.                 fchoose.getExtensionFilters().add(allfilter);
  85.                
  86.                 //Open the dialog and set the song equal to the file chosen
  87.                 File song = fchoose.showOpenDialog(stage);
  88.                
  89.                 //Use the song file to construct the media and player objects
  90.                 try {
  91. //                  StringBuilder builder = new StringBuilder(song.toURI().toString());
  92. //                  String ssong = "";
  93. //                 
  94. //                  builder.insert(5, "//");
  95. //                  ssong = builder.toString();
  96.                    
  97.                     System.out.println(song.toURI().toString());
  98.                     media = new Media(song.toURI().toString());
  99.                     player = new MediaPlayer(media);
  100.                 }
  101.                 catch (Exception e) {
  102.                     e.printStackTrace();
  103.                 }
  104.             }
  105.         });
  106.        
  107.         GridPane root = new GridPane();
  108.        
  109.         root.add(bprev, 1, 1);
  110.         root.add(bnext, 1, 2);
  111.         root.add(bplaypause, 1, 3);
  112.         root.add(bsongchooser, 1, 4);
  113.        
  114.         stage.setScene(new Scene(root, 750, 750));
  115.         stage.show();
  116.     }
  117.    
  118.     public int playpause() {
  119.        
  120.         //checks the current state of the song and reacts appropriately
  121.         if (songstatus.equals("PLAY") || songstatus.equals("DEFAULT_STATE")) {
  122.             if (songstatus.equals("DEFAULT_STATE")) {
  123.                 songstatus = "PLAY";
  124.             }
  125.             bplaypause.setText("Pause");
  126.         }
  127.         else if (songstatus.equals("PLAY")) {
  128.             player.pause();
  129.             songstatus = "PAUSE";
  130.             bplaypause.setText("Play");
  131.         }
  132.         else if (songstatus.equals("STOP")) {
  133.             player.play();
  134.         }
  135.         else {
  136.             Exception e = new Exception();
  137.             e.printStackTrace();
  138.             songstatus.equals("ERROR");
  139.             return 1;
  140.         }
  141.        
  142.         return 0;
  143.     }
  144.    
  145.     public int stopSong() {
  146.         player.stop();
  147.         songstatus = "STOP";
  148.         return 0;
  149.     }
  150.    
  151.     public int next() {
  152.         return 0;
  153.     }
  154.    
  155.     public int prev() {
  156.         return 0;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement