Skip to content

Instantly share code, notes, and snippets.

@Yuki44
Forked from EdwinSilvaDK/MPlayer.java
Created November 29, 2016 14:07
Show Gist options
  • Select an option

  • Save Yuki44/e79e1c97ef4e684b6885a3331d974170 to your computer and use it in GitHub Desktop.

Select an option

Save Yuki44/e79e1c97ef4e684b6885a3331d974170 to your computer and use it in GitHub Desktop.
Music Player using JavaFX
package com.github.shkesar.JMusic;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MPlayer extends Application {
File selectedFile;
MediaPlayer mplayer;
Slider musicSlider;
public MPlayer() {
musicSlider = new Slider();
}
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane() {{
Label filenameLabel = new Label("");
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Audio Files", "*.wav", "*.mp3"));
VBox vbox = new VBox() {{
getChildren().add(filenameLabel);
HBox hbox = new HBox() {{
Button playButton = new Button("Play");
Button pauseButton = new Button("Pause");
playButton.setOnAction(e -> {
mplayer.play();
});
pauseButton.setOnAction(e -> {
mplayer.pause();
});
Button stopButton = new Button("Stop") {{
setOnAction(e -> {
mplayer.stop();
});
}};
getChildren().addAll(playButton, pauseButton, stopButton);
}};
getChildren().add(hbox);
}};
setCenter(vbox);
MenuBar menubar = new MenuBar() {{
Menu menu = new Menu("File") {{
MenuItem selectMenuItem = new MenuItem("Select") {{
setOnAction(e -> {
selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) {
Media media = null;
Pattern spacePattern = Pattern.compile(" ");
String url = ("file://" + selectedFile.getAbsolutePath());
Matcher matcher = spacePattern.matcher(url);
url = matcher.replaceAll("\\ ");
System.out.println(url);
media = new Media(url);
mplayer = new MediaPlayer(media);
musicSlider.setMin(0);
musicSlider.setMax(mplayer.getStopTime().toSeconds());
}
});
}};
MenuItem pauseMenuItem = new MenuItem("Pause");
MenuItem playMenuItem = new MenuItem("Play");
MenuItem stopMenuItem = new MenuItem("Stop");
getItems().addAll(selectMenuItem, playMenuItem, pauseMenuItem, stopMenuItem);
}};
getMenus().add(menu);
}};
setTop(menubar);
setBottom(musicSlider);
}};
new Thread(new Runnable() {
@Override
public void run() {
double currentTime = mplayer.getCurrentTime().toSeconds();
musicSlider.setValue(currentTime);
try {
Thread.sleep(900);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Scene scene = new Scene(root, 400, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
}
@newtingz
Copy link

hey hello i need your help
I have a music player currently stuck on the mission to make the custom listview display the mp3;s details saved to an observable list when the information was extracted, so what it does is that when the listview object is getting called the stored data for the mp3 file is accessed and displayed on screen in the custom list cell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment