APP MP3Player API for JavaFX

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
Hi,

The interest on the thread "Playing MP3 with javafx.scene.media.MediaPlayer" leads to the API MP3Player which allows you to play the MP3 Music as
  • an Album
  • a Playing List
  • a MP3 piece
The API is as following:

pic_1.png

An Album is a directory (+ subdirectories) that contains all MP3 music file with the suffix .mp3. Example: Directory C:/JoeApp/sound/mp3 has the following MP3 files:

album.png

A Play List is a file that contains a list of MP3 files. Example: file zoo.txt contains the following MP3 entries
Code:
http://static1.grsites.com/archive/sounds/animals/animals135.mp3
http://www.vibrationdata.com/macaw.mp3
http://www.freeinfosociety.com/media/sounds/320.mp3
http://www.freeinfosociety.com/media/sounds/287.mp3
http://soundjax.com/reddo/39239%5EHORSE0.mp3
http://www.freeinfosociety.com/media/sounds/280.mp3
http://www.freeinfosociety.com/media/sounds/4939.mp3
http://www.freeinfosociety.com/media/sounds/4931.mp3
http://www.freeinfosociety.com/media/sounds/326.mp3
How to use the API M3Player ? The following source file JoeMP3Player.java shows you how to work with MP3Player

Java:
// Java
import java.io.*;
import java.util.*;
import java.nio.file.*;
import  java.util.stream.*;
// Joe Nartca (C)
public class JoeMP3Player extends Application {
  public void stop( ) {
    Platform.exit();
    System.exit(0);
  }
  public void start(Stage stage) throws Exception {
    stage.setTitle("Joe's MP3Player with Joe's MP3Player API");
    Image love = new Image(getClass().getResourceAsStream("love.gif"));
    stage.getIcons().add(love);
    // get the passing parameter 
    List<String> params = getParameters().getRaw();
    String pList = params.size() > 0? params.get(0) : "zoo.txt";  // Album or PlayList (default)
    mp3Player = new MP3Player(pList);    // Instantiated
    MAX = mp3Player.getListSize();  // number of files in the PlayList
    pos = 0;  // set starting at 0
    //
    ProgressBar pBar =  mp3Player.getPlayingProgress( ); // get the ProgressBar as the Playing Progress
    pBar.setMaxWidth(Double.MAX_VALUE);  // preset the pBar size
    Label lab = new Label(pList);
    // PlayAll
    all = new Button("ALBUM");
    all.setOnAction(a -> {
      if (mp3Player.isIdle()) { // idle?
         lab.setText("Playing Album");
         mp3Player.playAll( );  // playAll for the Album or PlayList
         setColor(all);
      }
    });
    // Play
    play = new Button("PLAY");
    play.setOnAction(a -> {
      if (mp3Player.isIdle()) {
        mp3Player.play(pos++); // play the MP3 file at position pos
        lab.setText(mp3Player.getPlayName()); // extract the MP3 Title
        if (pos >= MAX) pos = 0;
        setColor(play);
      }
    });
    // stop
    halt = new Button("STOP");
    halt.setOnAction(a -> {
      mp3Player.stop();  // Stop the Player
      setColor(halt);
    });
    // skip
    skip = new Button("SKIP");
    skip.setOnAction(a -> {
      mp3Player.skip();  // skip to next MP3 file
      lab.setText(mp3Player.getPlayName());
      pos = mp3Player.getPlayingIndex()+1; // get the netx of the actual playing index
      setColor(skip);
    });
    // pause
    pause = new Button("PAUSE");
    pause.setOnAction(a -> {
      mp3Player.pause();     // make a pause
      setColor(pause);
    });
    // pause
    resume = new Button("RESUME");
    resume.setOnAction(a -> {
      mp3Player.play();      // resume the playing
      setColor(resume);
    });
    HBox hb1 = new HBox(10);
    hb1.setAlignment(Pos.CENTER);
    hb1.setPadding(new Insets(20, 5, 5, 5));
    hb1.getChildren().addAll(pBar);  
    HBox.setHgrow(pBar, Priority.ALWAYS);
    //
    HBox hb2 = new HBox(10);
    hb2.setAlignment(Pos.CENTER);
    hb2.setPadding(new Insets(20, 5, 5, 5));
    hb2.getChildren().addAll(all, play, skip, pause, resume, halt);
    //
    VBox vb = new VBox();
    vb.setAlignment(Pos.CENTER);
    vb.setPadding(new Insets(20, 5, 5, 5));
    vb.getChildren().setAll(lab, hb1, hb2);
    //
    Scene scene = new Scene(vb, 550, 160);
    if ((new File("css.css")).exists()) scene.getStylesheets().add("css.css");
    stage.setScene(scene);
    stage.show();
  }
  private void setColor(Button but) {
    all.setStyle(color);
    play.setStyle(color);
    skip.setStyle(color);
    pause.setStyle(color);
    resume.setStyle(color);
    halt.setStyle(color);
    but.setStyle("-fx-background-color: bisque;");
  }
  private int MAX, pos;
  private MP3Player mp3Player;
  private Button all, play, skip, pause, resume, halt;
  private String color = "-fx-background-color: linear-gradient(#f0ff35, #a9ff00),"+
                         "radial-gradient(center 50% -40%, radius 200%, #b8ee36 45%, #80c800 50%);";
the css.css file

CSS:
.root {
  -fx-font-size: 11pt;
  -fx-base: silver;
}
.button {
    -fx-background-color:
        linear-gradient(#f0ff35, #a9ff00),
        radial-gradient(center 50% -40%, radius 200%, #b8ee36 45%, #80c800 50%);
    -fx-background-radius: 6, 5;
    -fx-background-insets: 0, 1;
    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 1 );
    -fx-text-fill: #395306;
}
.button:hover {
  -fx-background-color: bisque;
}
.text-field {
-fx-background-color: snow;
-fx-font-size: 14;
}
the icon: love.gif and if you've successfully downloaded the API and compiled the JoeMP3Player.java you get this app
mp3API.png

Here is the ZIP with the source of API MP3Player.java

Enjoy
Joe
 

Attachments

Sửa lần cuối:
  • Like
Reactions: quydtkt

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
NOTE: for the correctness the method isPlaying() is renamed to isIdle(). The source MP3Player.java of mp3.zip can be extended with the methods of JFX API MediaPlayer. For example, you want to have 4 methods getBalance(), setBalance(), getVolume() and setVolume() of the API JFX MediaPlayer you could implement them into the MP3Player.java as following:
Java:
  ...
  /**
  getVolume Retrieves the audio playback volume. The default value is 1.0.
  @return double 
  */
  public double getVolume() {
    return player != null? player.getVolume():0;
  }
  /**
  setVolume: Sets the audio playback volume. Its effect will be clamped to the range [0.0, 1.0].
  @param value double 
  */
  public void setVolume(double value) {
    if (player != null) player.setVolume(value < 0?0:value > 1.0? 1.0:value);
  }
  /**
  getBalance (see MediaPlayer API for explanation)
  @return double 
  */
  public double getBalance() {
    return player != null? player.getBalance():0;
  }
  /**
  setBalance: Sets the audio balance. Its effect will be clamped to the range [-1.0, 1.0].
  @param value double 
  */
  public void setBalance(double value) {
    if (player != null) player.setBalance(value < -1.0? -1.0:value > 1.0? 1.0:value);
  }
  ...
Any other method of JFX MediaPlayer can be easily implemented in this similar way.
 
Sửa lần cuối:

quydtkt

Administrator
1/11/19
365
35
28
26
I will continue developing my software based on this source. Hope you allow
 

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
Hope you allow
Of course, be my guest. However I've enhanced the codes MP3Player with the 4 above mentioned methods PLUS the ProgressIndicator as an alternative to ProgressBar and the example JoeMP3Player, NewJoeMP3Player (with getProgressIndicator() method). The Source MP3Player can be downloaded as usually from HERE

JoeMP3Player.java
Java:
// JavaFX
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import javafx.stage.Stage;
import javafx.beans.value.ObservableValue;
// Java
import java.io.*;
import java.util.*;
import java.nio.file.*;
import  java.util.stream.*;
// Joe Nartca (C)
public class JoeMP3Player extends Application {
  public void stop( ) {
    Platform.exit();
    System.exit(0);
  }
  public void start(Stage stage) throws Exception {
    stage.setTitle("Joe's MP3Player with Joe's MP3Player API");
    Image love = new Image(getClass().getResourceAsStream("love.gif"));
    stage.getIcons().add(love);
    // get the passing parameter
    List<String> params = getParameters().getRaw();
    String pList = params.size() > 0? params.get(0) : "zoo.txt";
    mp3Player = new MP3Player(pList);
    ProgressBar pBar =  mp3Player.getProgressBar( );
    pBar.setMaxWidth(Double.MAX_VALUE);
    Label lab = new Label(pList);
    MAX = mp3Player.getListSize();
    pos = 0;
    //
    Label ilab = new Label("NEW Album or Play List or MP3 Piece");
    TextField txt = new TextField(System.getProperty("user.dir"));
    txt.setOnAction(a -> {
      String album = txt.getText();
      if (!album.isEmpty()) try {
        mp3Player.stop(); // stop the playing
        mp3Player.loadMP3List(album); // load new Album/List/Piece
        lab.setText("ALBUM: "+album.substring(album.lastIndexOf(File.separator)+1));
        setColor(null);
      } catch (Exception ex) {
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle("Poblem with \""+album+"\"");
        alert.setHeaderText("Error: "+ex.toString());
        alert.showAndWait();
      }
    });
    HBox hbox = new HBox(10);
    hbox.setAlignment(Pos.CENTER);
    hbox.getChildren().addAll(ilab, txt);
    // For the volume
    Label vLab = new Label("Volume");
    Slider vol = new Slider(0, 100, 50);
    vol.valueProperty().addListener((ObservableValue <? extends Number > observable,
                                     Number oldValue, Number newValue) -> {
       mp3Player.setVolume(newValue.doubleValue()/100); // set Volume
    });
    vol.setPrefSize(200, 20);
    vol.setShowTickLabels(true);
    vol.setShowTickMarks(true);
    vol.setMajorTickUnit(50);
    vol.setMinorTickCount(5);
    vol.setBlockIncrement(10);
    VBox vVol = new VBox();
    vVol.setAlignment(Pos.CENTER);
    vVol.getChildren().addAll(vLab, vol);
    // For the balancee
    Label bLab = new Label("Balance");
    Slider bal = new Slider(-100, 100, 0);
    bal.valueProperty().addListener((ObservableValue <? extends Number > observable,
                                     Number oldValue, Number newValue) -> {
       mp3Player.setBalance(newValue.doubleValue()/200); // set Balance
    });
    bal.setPrefSize(200, 20);
    bal.setShowTickLabels(true);
    bal.setShowTickMarks(true);
    bal.setMajorTickUnit(50);
    bal.setMinorTickCount(5);
    bal.setBlockIncrement(10);
    VBox vBal = new VBox();
    vBal.setAlignment(Pos.CENTER);
    vBal.getChildren().addAll(bLab, bal);
    //
    HBox hBox = new HBox(5);
    hBox.setAlignment(Pos.CENTER);
    hBox.getChildren().addAll(vBal, vVol);
    //
    VBox vb1 = new VBox(5);
    vb1.setAlignment(Pos.CENTER);
    vb1.setPadding(new Insets(10, 5, 5, 5));
    vb1.getChildren().addAll(lab, pBar);
    // PlayAll
    all = new Button("ALBUM");
    all.setOnAction(a -> {
      if (mp3Player.isIdle()) {
        mp3Player.playAll( );
        lab.setText("Playing ALBUM "+mp3Player.getPlayName());
        setColor(all);
      }
    });
    // Play
    play = new Button("PLAY");
    play.setOnAction(a -> {
      if (mp3Player.isIdle()) {
        mp3Player.play(pos++);
        lab.setText(mp3Player.getPlayName());
        if (pos >= MAX) pos = 0;
        setColor(play);
      }
    });
    // stop
    halt = new Button("STOP");
    halt.setOnAction(a -> {
      mp3Player.stop();
      setColor(halt);
    });
    // skip
    skip = new Button("SKIP");
    skip.setOnAction(a -> {
      mp3Player.skip();
      lab.setText(mp3Player.getPlayName());
      pos = mp3Player.getPlayingIndex()+1;
      setColor(skip);
    });
    // pause
    pause = new Button("PAUSE");
    pause.setOnAction(a -> {
      mp3Player.pause();
      setColor(pause);
    });
    // pause
    resume = new Button("RESUME");
    resume.setOnAction(a -> {
      mp3Player.play();
      setColor(resume);
    });
    //
    HBox hb2 = new HBox(10);
    hb2.setAlignment(Pos.CENTER);
    hb2.setPadding(new Insets(20, 5, 5, 5));
    hb2.getChildren().addAll(all, play, skip, pause, resume, halt);
    //
    VBox vb = new VBox();
    vb.setAlignment(Pos.CENTER);
    vb.setPadding(new Insets(5, 5, 5, 5));
    vb.getChildren().addAll(hbox, vb1, hBox, hb2);
    //
    Scene scene = new Scene(vb, 550, 240);
    if ((new File("css.css")).exists()) scene.getStylesheets().add("css.css");
    stage.setScene(scene);
    stage.show();
  }
  private void setColor(Button but) {
    all.setStyle(color);
    play.setStyle(color);
    skip.setStyle(color);
    pause.setStyle(color);
    resume.setStyle(color);
    halt.setStyle(color);
    if (but != null) but.setStyle("-fx-background-color: bisque;");
  }
  private int MAX, pos;
  private MP3Player mp3Player;
  private Button all, play, skip, pause, resume, halt;
  private String color = "-fx-background-color: linear-gradient(#f0ff35, #a9ff00),"+
                         "radial-gradient(center 50% -40%, radius 200%, #b8ee36 45%, #80c800 50%);";
}
App_MP3Player_NEW.png

NewJoeMP3Player.java
Java:
// JavaFX
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import javafx.stage.Stage;
import javafx.beans.value.ObservableValue;
// Java
import java.io.*;
import java.util.*;
import java.nio.file.*;
import  java.util.stream.*;
// Joe Nartca (C)
public class NewJoeMP3Player extends Application {
  public void stop( ) {
    Platform.exit();
    System.exit(0);
  }
  public void start(Stage stage) throws Exception {
    stage.setTitle("Joe's MP3Player with Joe's MP3Player API");
    Image love = new Image(getClass().getResourceAsStream("love.gif"));
    stage.getIcons().add(love);
    // get the passing parameter
    List<String> params = getParameters().getRaw();
    String pList = params.size() > 0? params.get(0) : "zoo.txt";
    mp3Player = new MP3Player(pList);
    ProgressIndicator pInd =  mp3Player.getProgressIndicator( );
    pInd.setPrefSize(100, 100);
    Label lab = new Label(pList);
    MAX = mp3Player.getListSize();
    pos = 0;
    //
    Label ilab = new Label("NEW Album or Play List or MP3 Piece");
    TextField txt = new TextField(System.getProperty("user.dir"));
    txt.setOnAction(a -> {
      String album = txt.getText();
      if (!album.isEmpty()) try {
        mp3Player.stop(); // stop the playing
        mp3Player.loadMP3List(album); // load new Album/List/Piece
        lab.setText("ALBUM: "+album.substring(album.lastIndexOf(File.separator)+1));
        setColor(null);
      } catch (Exception ex) {
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle("Poblem with \""+album+"\"");
        alert.setHeaderText("Error: "+ex.toString());
        alert.showAndWait();
      }
    });
    HBox hbox = new HBox(10);
    hbox.setAlignment(Pos.CENTER);
    hbox.getChildren().addAll(ilab, txt);
    // For the volume
    Label vLab = new Label("Volume");
    Slider vol = new Slider(0, 100, 50);
    vol.valueProperty().addListener((ObservableValue <? extends Number > observable,
                                     Number oldValue, Number newValue) -> {
       mp3Player.setVolume(newValue.doubleValue()/100); // set Volume
    });
    vol.setPrefSize(200, 20);
    vol.setShowTickLabels(true);
    vol.setShowTickMarks(true);
    vol.setMajorTickUnit(50);
    vol.setMinorTickCount(5);
    vol.setBlockIncrement(10);
    VBox vVol = new VBox();
    vVol.setAlignment(Pos.CENTER);
    vVol.getChildren().addAll(vLab, vol);
    // For the balancee
    Label bLab = new Label("Balance");
    Slider bal = new Slider(-100, 100, 0);
    bal.valueProperty().addListener((ObservableValue <? extends Number > observable,
                                     Number oldValue, Number newValue) -> {
       mp3Player.setBalance(newValue.doubleValue()/200); // set Balance
    });
    bal.setPrefSize(200, 20);
    bal.setShowTickLabels(true);
    bal.setShowTickMarks(true);
    bal.setMajorTickUnit(50);
    bal.setMinorTickCount(5);
    bal.setBlockIncrement(10);
    VBox vBal = new VBox();
    vBal.setAlignment(Pos.CENTER);
    vBal.getChildren().addAll(bLab, bal);
    //
    HBox hBox = new HBox(5);
    hBox.setAlignment(Pos.CENTER);
    hBox.getChildren().addAll(vBal, vVol);
    //
    VBox vb1 = new VBox(5);
    vb1.setAlignment(Pos.CENTER);
    vb1.setPadding(new Insets(10, 5, 5, 5));
    vb1.getChildren().addAll(lab, pInd);
    // PlayAll
    all = new Button("ALBUM");
    all.setOnAction(a -> {
      if (mp3Player.isIdle()) {
        mp3Player.playAll( );
        lab.setText("Playing ALBUM "+mp3Player.getPlayName());
        setColor(all);
      }
    });
    // Play
    play = new Button("PLAY");
    play.setOnAction(a -> {
      if (mp3Player.isIdle()) {
        mp3Player.play(pos++);
        lab.setText(mp3Player.getPlayName());
        if (pos >= MAX) pos = 0;
        setColor(play);
      }
    });
    // stop
    halt = new Button("STOP");
    halt.setOnAction(a -> {
      mp3Player.stop();
      setColor(halt);
    });
    // skip
    skip = new Button("SKIP");
    skip.setOnAction(a -> {
      mp3Player.skip();
      lab.setText(mp3Player.getPlayName());
      pos = mp3Player.getPlayingIndex()+1;
      setColor(skip);
    });
    // pause
    pause = new Button("PAUSE");
    pause.setOnAction(a -> {
      mp3Player.pause();
      setColor(pause);
    });
    // pause
    resume = new Button("RESUME");
    resume.setOnAction(a -> {
      mp3Player.play(); 
      setColor(resume);
    });
    //
    HBox hb2 = new HBox(10);
    hb2.setAlignment(Pos.CENTER);
    hb2.setPadding(new Insets(20, 5, 5, 5));
    hb2.getChildren().addAll(all, play, skip, pause, resume, halt);
    //
    VBox vb = new VBox();
    vb.setAlignment(Pos.CENTER);
    vb.setPadding(new Insets(5, 5, 5, 5));
    vb.getChildren().addAll(hbox, vb1, hBox, hb2);
    //
    Scene scene = new Scene(vb, 550, 320);
    if ((new File("css.css")).exists()) scene.getStylesheets().add("css.css");
    stage.setScene(scene);
    stage.show();
  }
  private void setColor(Button but) {
    all.setStyle(color);
    play.setStyle(color);
    skip.setStyle(color);
    pause.setStyle(color);
    resume.setStyle(color);
    halt.setStyle(color);
    if (but != null) but.setStyle("-fx-background-color: bisque;");
  }
  private int MAX, pos;
  private MP3Player mp3Player;
  private Button all, play, skip, pause, resume, halt;
  private String color = "-fx-background-color: linear-gradient(#f0ff35, #a9ff00),"+
                         "radial-gradient(center 50% -40%, radius 200%, #b8ee36 45%, #80c800 50%);";
App_MP3Player_pInd.png

Hope you and all CDJ members like it.
Joe

The new ZIP for the API, 2 examples + CSS file
 

Attachments

Sửa lần cuối:

quydtkt

Administrator
1/11/19
365
35
28
26
I want to separate the GUI part from the MP3Player class? Currently, it contains ProgressIndicator. I don't like this
 

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
You are welcome, boss ! And here is the example JoeMP3PlayerLite.java
Java:
// JavaFX
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import javafx.stage.Stage;
import javafx.beans.value.ObservableValue;
// Java
import java.io.*;
import java.util.*;
import java.nio.file.*;
import  java.util.stream.*;
// Joe Nartca (C)
public class JoeMP3PlayerLite extends Application {
  public void stop( ) {
    Platform.exit();
    System.exit(0);
  }
  public void start(Stage stage) throws Exception {
    stage.setTitle("Joe's MP3PlayerLite with Joe's MP3PlayerLite API");
    Image love = new Image(getClass().getResourceAsStream("love.gif"));
    stage.getIcons().add(love);
    // get the passing parameter 
    List<String> params = getParameters().getRaw();
    String pList = params.size() > 0? params.get(0) : "zoo.txt";
    mp3Player = new MP3PlayerLite(pList);
    Label lab = new Label(pList);
    MAX = mp3Player.getListSize();
    pos = 0;
    //
    Label ilab = new Label("NEW Album or Play List or MP3 Piece");
    TextField txt = new TextField(System.getProperty("user.dir"));
    txt.setOnAction(a -> {
      String album = txt.getText();
      if (!album.isEmpty()) try {
        mp3Player.stop(); // stop the playing
        mp3Player.loadMP3List(album); // load new Album/List/Piece
        lab.setText("ALBUM: "+album.substring(album.lastIndexOf(File.separator)+1));
        setColor(null);
      } catch (Exception ex) {
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle("Poblem with \""+album+"\"");  
        alert.setHeaderText("Error: "+ex.toString());
        alert.showAndWait();
      }
    });
    HBox hbox = new HBox(10);
    hbox.setAlignment(Pos.CENTER);
    hbox.getChildren().addAll(ilab, txt);
    // For the volume
    Label vLab = new Label("Volume");
    Slider vol = new Slider(0, 100, 50);
    vol.valueProperty().addListener((ObservableValue <? extends Number > observable, 
                                     Number oldValue, Number newValue) -> { 
       mp3Player.setVolume(newValue.doubleValue()/100); // set Volume
    });
    vol.setPrefSize(200, 20);
    vol.setShowTickLabels(true);
    vol.setShowTickMarks(true);
    vol.setMajorTickUnit(50);
    vol.setMinorTickCount(5);
    vol.setBlockIncrement(10);
    VBox vVol = new VBox();
    vVol.setAlignment(Pos.CENTER);
    vVol.getChildren().addAll(vLab, vol);
    // For the balancee
    Label bLab = new Label("Balance");
    Slider bal = new Slider(-100, 100, 0);
    bal.valueProperty().addListener((ObservableValue <? extends Number > observable, 
                                     Number oldValue, Number newValue) -> { 
       mp3Player.setBalance(newValue.doubleValue()/200); // set Balance
    });
    bal.setPrefSize(200, 20);
    bal.setShowTickLabels(true);
    bal.setShowTickMarks(true);
    bal.setMajorTickUnit(50);
    bal.setMinorTickCount(5);
    bal.setBlockIncrement(10);
    VBox vBal = new VBox();
    vBal.setAlignment(Pos.CENTER);
    vBal.getChildren().addAll(bLab, bal);
    //
    HBox hBox = new HBox(5);
    hBox.setAlignment(Pos.CENTER);
    hBox.getChildren().addAll(vBal, vVol);
    //
    VBox vb1 = new VBox(5);
    vb1.setAlignment(Pos.CENTER);
    vb1.setPadding(new Insets(10, 5, 5, 5));
    vb1.getChildren().addAll(lab);  
    // PlayAll
    all = new Button("ALBUM");
    all.setOnAction(a -> {
      if (mp3Player.isIdle()) {
        mp3Player.playAll( );
        lab.setText("Playing ALBUM "+mp3Player.getPlayName());
        setColor(all);
      }
    });
    // Play
    play = new Button("PLAY");
    play.setOnAction(a -> {
      if (mp3Player.isIdle()) {
        mp3Player.play(pos++);
        lab.setText("Playing "+mp3Player.getPlayName());
        if (pos >= MAX) pos = 0;
        setColor(play);
      }
    });
    // stop
    halt = new Button("STOP");
    halt.setOnAction(a -> {
      mp3Player.stop();
      setColor(halt);
    });
    // skip
    skip = new Button("SKIP");
    skip.setOnAction(a -> {
      mp3Player.skip();
      lab.setText(mp3Player.getPlayName());
      pos = mp3Player.getPlayingIndex()+1;
      setColor(skip);
    });
    // pause
    pause = new Button("PAUSE");
    pause.setOnAction(a -> {
      mp3Player.pause();     
      setColor(pause);
    });
    // pause
    resume = new Button("RESUME");
    resume.setOnAction(a -> {
      mp3Player.play();      
      setColor(resume);
    });
    //
    HBox hb2 = new HBox(10);
    hb2.setAlignment(Pos.CENTER);
    hb2.setPadding(new Insets(20, 5, 5, 5));
    hb2.getChildren().addAll(all, play, skip, pause, resume, halt);
    //
    VBox vb = new VBox();
    vb.setAlignment(Pos.CENTER);
    vb.setPadding(new Insets(5, 5, 5, 5));
    vb.getChildren().addAll(hbox, vb1, hBox, hb2);
    //
    Scene scene = new Scene(vb, 550, 240);
    if ((new File("css.css")).exists()) scene.getStylesheets().add("css.css");
    stage.setScene(scene);
    stage.show();
  }
  private void setColor(Button but) {
    all.setStyle(color);
    play.setStyle(color);
    skip.setStyle(color);
    pause.setStyle(color);
    resume.setStyle(color);
    halt.setStyle(color);
    if (but != null) but.setStyle("-fx-background-color: bisque;");
  }
  private int MAX, pos;
  private MP3PlayerLite mp3Player;
  private Button all, play, skip, pause, resume, halt;
  private String color = "-fx-background-color: linear-gradient(#f0ff35, #a9ff00),"+
                         "radial-gradient(center 50% -40%, radius 200%, #b8ee36 45%, #80c800 50%);";
JoeMP3PlayerLite.png
 

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
Can't watch the music process
Watch? What do you mean "watch"? Visually you need a ProgressIndicator or ProgressBar. Otherwise you have ONLY the acoustic sound. You could compare the sources and see the difference by yourself.
 

quydtkt

Administrator
1/11/19
365
35
28
26
Watch? What do you mean "watch"? Visually you need a ProgressIndicator or ProgressBar. Otherwise you have ONLY the acoustic sound. You could compare the sources and see the difference by yourself.
I mean, I can know how many percent of the song has been run
 

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
how many percent of the song has been run
Oh that. Two possibilties:
  1. use the original MP3Player version with Volume/Balance and ProgressBar or ProgressIndicator, or
  2. use the MP3PlayerLite and modified it according to this tutorials: click HERE. Now it's your turn to develop it :D
When I got time I would implement the "TimeSlider" into MP3Player
 

quydtkt

Administrator
1/11/19
365
35
28
26
Oh that. Two possibilties:
  1. use the original MP3Player version with Volume/Balance and ProgressBar or ProgressIndicator, or
  2. use the MP3PlayerLite and modified it according to this tutorials: click HERE. Now it's your turn to develop it :D
When I got time I would implement the "TimeSlider" into MP3Player
Those are my few questions. Actually, your software is great :-bd
 
  • Like
Reactions: Joe

Joe

Thành viên VIP
21/1/13
2,935
1,304
113
@quydtkt
if you want to modify MP3PlayerLite OUTSIDE it you need to include a method that allows you to pick the currently-running MediaPlayer. Example:
Java:
public class MP3PlayerLite {
   ...
  /**
  getCurrentPlayer
  @return MediaPlayer instance of the running player, null if no running player
  */
  public MediaPlayer getCurrentPlayer() {
    return player;
  }
  ...
}
the player allows you to work directly with all MediaPlayer methods and to do the "fanciest" things you could imagine.
 

quydtkt

Administrator
1/11/19
365
35
28
26
@quydtkt
if you want to modify MP3PlayerLite OUTSIDE it you need to include a method that allows you to pick the currently-running MediaPlayer. Example:
Java:
public class MP3PlayerLite {
   ...
  /**
  getCurrentPlayer
  @return MediaPlayer instance of the running player, null if no running player
  */
  public MediaPlayer getCurrentPlayer() {
    return player;
  }
  ...
}
the player allows you to work directly with all MediaPlayer methods and to do the "fanciest" things you could imagine.
I will try using it