Play music in JavaFX - javafx

I have the following code to play music but cannot play the whole track. Music plays for 3 seconds then stops.
(I'm new to javaFX and hope anyone can help)
void Music(ActionEvent event) throws Exception {
String path = "Music.mp3";
playHitSound(path);
}
private void playHitSound(String fileName) {
String path = getClass().getResource(fileName).getPath();
Media media = new Media(new File(path).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaPlayer.play();
}

Related

javafx vlcj video freezes after few seconds while audio keeps playing

I am making a javafx media player using vlcj.
There is my DMediaPlayer.java class that creates the vlcj media player and returns an ImageView which displays the video.
and here is my PlayerView.java
this class displays the video.
public class PlayerView extends BorderPane {
private MenuBar menuBar;
private VideoControlPanel controlPanel;
private Drawer drawer;
private IconsProvider icons;
private MediaPlayerInterface mediaPlayer;
private ImageView mediaView;
private StackPane playerHolder;
private ImageView view;
public PlayerView() {
view = new ImageView();
icons = new IconsProvider();
menuBar = new MenuBar(icons);
setTop(menuBar);
playerHolder = new StackPane();
playerHolder.setAlignment(Pos.TOP_LEFT);
setCenter(playerHolder);
mediaPlayer = new DMediaPlayer();
mediaView = mediaPlayer.getMediaView();
playerHolder.getChildren().add(mediaView);
try {
mediaPlayer.load("/home/doruk/Downloads/Video/movie.mkv");
} catch (FileNotFoundException e) {
System.out.println(e.toString());
}
mediaPlayer.play();
playerHolder.getChildren().add(drawer);
}
}
The problem is the video plays as expected, but after few seconds, the ImageView only displays the single image, its like the video is paused. but the audio keeps on playing.
I thought the issue was due to ImageView being garbage collected as if I initially when the mediaplayer was called as follow:
DMediaPlayer player = new DMediaPlayer()
player.play()
inside the PlayerView class, there would be following errors:
JNA: callback object has been garbage collected
JNA: callback object has been garbage collected
JNA: callback object has been garbage collected
malloc(): unaligned tcache chunk detected
and so I created the the ImageView in the same PlayerView class and passed the reference to the DMediaPlayer so that it can play the video in it, but that does not work either.

JavaFX2 play wav in background

So I'm trying to play indefinitly a song on a background thread, but when the music ends it does not loop as it was supose to.
Tried the offered solution but yet no joy! Here is the code for the main class, hope this helps in the resolution of the issue.
Even tried to loop the thread, but no joy...
Not sure why it's ending after playing the full file once, but not sure how to solve it!
Here is the code I have. Any help is welcome
public class Main extends Application {
Media sugar = new Media(this.getClass().getResource("sounds/t1coSugar.wav").toExternalForm());
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(Main.class, args);
}
#Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("pacman");
primaryStage.setWidth(MazeData.calcGridX(MazeData.GRID_SIZE_X + 2)); //stage size x
primaryStage.setHeight(MazeData.calcGridY(MazeData.GRID_SIZE_Y + 5)); //stage size y
//splash screen
//end of splash screen
final Group root = new Group();
final Scene scene = new Scene(root);
root.getChildren().add(new Maze());
primaryStage.setScene(scene);
primaryStage.show();
int playbackgroundmusic = playbackgroundmusic();
}
private int playbackgroundmusic()
{
Runnable task = new Runnable() {
#Override
public void run() {
playSugar(); //method of the music
}
};
// Run the task in a background thread
Thread backgroundThread = new Thread(task);
// Terminate the running thread if the application exits
backgroundThread.setDaemon(true);
// Start the thread
backgroundThread.start();
return 0;
}
public void playSugar()
{
MediaPlayer mediaplayer = new MediaPlayer(sugar);
mediaplayer.volumeProperty().setValue(0.4);
mediaplayer.setStartTime(Duration.seconds(0));
mediaplayer.setStopTime(Duration.seconds(67));
mediaplayer.setAutoPlay(true);
mediaplayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaplayer.play();
}

why javafx mediaplayer status sometimes returns unknown?

First i am sorry for my poor english...
i made Media Player Application with Javafx.
this player can get mulit file media. and play files out of all limits.
it work well. but sometimes not work..
it is not media error. it is mediaplayer error.
error message is 'mediaPlayer Unknown, media Invalid..' why.??
i played same video file(1920 * 1080), sometimes work and sometimes not work..
and javafx is depend on OS ??
player works perfectly on windown7 computer
but player have this error on windown10 computer...
please give me advice..
MediaPlayer mediaPlayer = null;
Stage stage = new Stage();
AnchorPane pane = new AnchorPane();
Scene scene = new Scene(pane);
MediaView mediaView = new MediaView();
int mNextFileIndex = -1;
List<File> fileLists = new ArrayList<>();
Media media;
mediaplayer play Method
public void playNextMedia() {
if (mediaPlayer != null) {
mediaPlayer.dispose();
mediaView.setMediaPlayer(null);
}
mNextFileIndex = (mNextFileIndex + 1) % fileLists.size();
media =new Media(fileLists.get(mNextFileIndex).toURI().toString());
media.setOnError(()-> {
MainApp.makeLog("media error");
});
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
mediaPlayer.setOnReady(() -> {
mediaPlayer.play();
});
mediaPlayer.setOnEndOfMedia(() -> {
playNextMedia();
});
mediaPlayer.setOnError(() -> {
systom.out.println("mediaPlayer error");
Systeom.out.println(mediaPlayer.getError().getMessage());
playNextMedia();
});
}
Button Method
#FXML
private void playMedia(ActionEvent event) {
mNextFileIndex = -1;
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(new
FileChooser.ExtensionFilter("Select a File (*.mp4)", "*.mp4"),
new FileChooser.ExtensionFilter("All Files", "*.*"));
List<File> list = fileChooser.showOpenMultipleDialog(null);
if (list != null) {
for (File file : list) {
fileLists.add(file)
}
playNextMedia();
pane.getChildren().add(mediaView);
stage.setScene(scene);
stage.show();
}

JavaFX WebView not loading graph data from url (WebSocket?)

Using this code and the hardcoded url, the graph in the url doesn't load. I tried disabling my firewall and running it under JDK 9 with a pre-release build of Netbeans 9. No luck. A WebSocket test says that WebSocket is working fine (url commented out in the code). Any ideas what's wrong or what I should check? My guess is it has to do with WebSocket regardless of the test results. I'm using Netbeans 8.2 and jdk1.8.0_144.
//
// Code from https://stackoverflow.com/questions/42297864/javafx-webview-in-java-project
//
public class FXWebViewInSwing {
private JFXPanel jfxPanel;
public void createAndShowWindow() {
JFrame frame = new JFrame();
JButton quit = new JButton("Quit");
quit.addActionListener(event -> System.exit(0));
jfxPanel = new JFXPanel();
Platform.runLater(this::createJFXContent);
JPanel buttonPanel = new JPanel();
buttonPanel.add(quit);
frame.add(BorderLayout.CENTER, jfxPanel);
frame.add(BorderLayout.SOUTH, buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1024, 576);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void createJFXContent() {
WebView webView = new WebView();
webView.getEngine().load("https://www.tradingview.com/chart/bKsZf5LY/");
// webView.getEngine().load("https://websocket.org/echo.html");
Scene scene = new Scene(webView);
jfxPanel.setScene(scene);
}
public static void main(String[] args) {
FXWebViewInSwing swingApp = new FXWebViewInSwing();
SwingUtilities.invokeLater(swingApp::createAndShowWindow);
}
}

Play streaming audio with JavaFX

I tryed to play follwing streams:
url: "http://icecast.unitedradio.it/Radio105.mp3", codec: "mp3"
url: "http://shoutcast.radio24.it:8000/", codec: "AAC"
using the MediaPlayer of JavaFX.
This is the code:
Media media = new Media("http://icecast.unitedradio.it/Radio105.mp3");
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mediaView = new MediaView(mediaPlayer);
mediaView.setFitHeight(500);
mediaView.setFitWidth(500);
mediaPlayer.setVolume(new Double(1));
root.getChildren().add(mediaView);
Button playButton = new Button();
playButton.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>()
{
#Override
public void handle(javafx.scene.input.MouseEvent event)
{
mediaPlayer.play();
}
});
root.getChildren().add(playButton);
primaryStage.setScene(scene);
primaryStage.show();
But pressing the playbutton the audio does not play.
Debugging the Media I have found that using one of stream above, in the Locator.init() the contentLength is -1. May be this the reason why it does not play?

Resources