why javafx mediaplayer status sometimes returns unknown? - javafx

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();
}

Related

javafx mp4 working in IDE but not in jar file

ok so i have a mp4 file in my src folder in my project and it work's fine in eclipse but when i export it to a runnable jar file it doesn't work.
public void playRickRoll() throws MalformedURLException {
StackPane spRickRoll = new StackPane();
Stage stage = new Stage();
stage.setResizable(false);
stage.getIcons().add(new Image("60AKG.png"));
// URL videoURL = getClass().getResource("RickRoll.mp4");
// Media media = new Media(videoURL.toString());
File f = new File("src/RickRoll.mp4");
Media media = new Media(f.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView();
mediaView.setMediaPlayer(mediaPlayer);
spRickRoll.getChildren().add(mediaView);
stage.setScene(new Scene(spRickRoll, 460, 360));
stage.show();
mediaPlayer.play();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent arg0) {
mediaPlayer.stop();
}
});
}

JavaFX custom dialog set Layout of node

We created a Custom Dialog without an FXML file. We are using JavaFX 8.
The dialog loads and functions as expected but we can not move the Buttons and the TextField to enhance the styling.
We have tried to use tf.setLayoutY(50) this has no effect.
We used this tf.setPromptText("This Works ?") and it works.
We would rather not use css to accomplish this styling.
And we will consider a FXML file if we can keep the two event handlers that force data to be entered in the TextField.
So the question is: How to style this Custom Dialog?
The code is a mess as it includes some concepts we tried:
public void CustomDialog() {
Dialog dialog = new Dialog<>();
dialog.setResizable(false);
final Window window = dialog.getDialogPane().getScene().getWindow();
stage = (Stage) window;
stage.setMinHeight(600);
stage.setMinWidth(400);
TextField tf = new TextField();
tf.setLayoutX(10);
tf.setLayoutY(50);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
dialog.getDialogPane().getChildren().add(tf);
dialog.getDialogPane().setContent(tf);
// Create an event filter that consumes the action if the text is empty
EventHandler<ActionEvent> filter = event -> {
if (tf.getText().isEmpty()) {
event.consume();
}
};
// lookup the buttons
ButtonBase okButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
Button cancelButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
// add the event-filter
okButton.addEventFilter(ActionEvent.ACTION, filter);
cancelButton.addEventFilter(ActionEvent.ACTION, filter);
stage.setOnCloseRequest(event -> {
if (tf.getText().isEmpty()) {
event.consume();
}
}
//Scene scene = new Scene(root);
//dialogStage.setScene(scene);
dialog.initModality(Modality.APPLICATION_MODAL);
//dialogStage.setAlwaysOnTop(true);
//dialogStage.setResizable(false);
tf.setPromptText("This Works ?");
tf.requestFocus();// This does not work
dialog.showAndWait();
}
Grendel we enhanced your answer so anyone who comes by and sees the code you posted in your question will understand as you said it was a mess
Your posted answer was real old school but less work perhaps than building a FXML file
Besides it is good to know some old school tricks
public void NewDialog(){
Label lblAmt = new Label("Enter Amount");
Button btnOK = new Button("OK");
TextField txtAmt = new TextField();
AnchorPane secondaryLayout = new AnchorPane();
secondaryLayout.setStyle("-fx-border-color:red;-fx-border-width:10px; -fx-background-color: lightblue;");
secondaryLayout.getChildren().addAll(lblAmt,btnOK,txtAmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
lblAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
txtAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
Scene secondScene = new Scene(secondaryLayout, 300, 180);
EventHandler<ActionEvent> filter = event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
};
// New window (Stage)
Stage newWindow = new Stage();
newWindow.initStyle(StageStyle.UNDECORATED);
//newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setResizable(false);
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt -> {
String str = txtAmt.getText();
System.out.println("################ str "+str);
if(txtAmt.getText().equals("")) {
evt.consume();
txtAmt.requestFocus();
}else{
newWindow.close();
}
});
newWindow.setOnCloseRequest(event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
});
txtAmt.requestFocus();
newWindow.showAndWait();
}

javafx stagestyle UNDECORATED

I'm using a translator.
enter image description here
Windows desktop appears unexpectedly when you click on the image.
Scrollpane -> BorderPane
Normal at first Occurs like an image later
FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/Main_fx.fxml"));
Parent root = loader.load();
st = new Stage();
final UndecoratorScene undecorator = new UndecoratorScene(st, (Region) root);//NewMainScene lib 사용(프로젝트 및 패키지 명 :Newtable)
undecorator.getStylesheets().add(getClass().getResource("/View/winDec.css").toExternalForm());
st.setScene(undecorator);
stage = (Stage) lb.getScene().getWindow();
st.getIcons().add(new Image(config2.class.getResourceAsStream("/View/207411.jpg" )));
st.initStyle(StageStyle.TRANSPARENT); //스타일 미적용.
st.setResizable(resize);
st.setMaximized(maximized);
st.setTitle(judul);
st.sizeToScene();
// Undecorator undecor = undecorator.getUndecorator();
//최소로 줄이수 있는 화면 크기 값 .
st.setMinWidth(350);
st.setMinHeight(200);
GetStage gs = new GetStage();//트레이 창으로 보낼때 현재 Stage 값 전달.
gs.SetStage(st);
st.toFront();
st.show();
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
st.setX((primScreenBounds.getWidth() - st.getWidth()) / 2);
st.setY((primScreenBounds.getHeight() - st.getHeight()) / 2);
createTrayIcon(st);//트레이 창 이벤트 메소드
firstTime = true;
Platform.setImplicitExit(false);
stage.close();//controllSplash stage 종료
Hander_Main controller = (Hander_Main)loader.getController();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
Date sys_date = new Date();
String now= formatter.format(sys_date);
controller.setPrimaryStage(st);
controller.setScene(undecorator);
controller.setFirstDate(now);
st.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we)
{
hide(st);
}
});
} catch (Exception e) {
st.hide();
dialog(Alert.AlertType.ERROR,"Main_fx Stage ERR\n"+e);
System.exit(0);
}

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?

Show gif animation popup while processing in JAVAFX

I have a method inside of one of my controllers that requires some seconds to process. I would like to have a gif animation popup while this occurs but I only get a static image. This is my code:
#FXML
public void search(ActionEvent e) {
final Stage dialog = new Stage();
Group popup = new Group();
Image image = new Image("file:resources/images/bender.gif");
ImageView view = new ImageView(image);
popup.getChildren().add(view);
Scene dialogScene = new Scene(popup);
dialog.setScene(dialogScene);
dialog.show();
Platform.runLater(new Runnable() {
#Override
public void run() {
Match msg = stablishSearchConditions();
TreeItem<String> root = new TreeItem<>("ROOT");
int indexName = 1;
String mensaje = "Mensaje ";
treeLabelResults.setText("");
arbol.setRoot(root);
for (Match message : msg.each()) {
TreeItem<String> nodo = new TreeItem<String>(mensaje + indexName);
root.getChildren().add(nodo);
root.setExpanded(true);
String mens = message.getMessage();
TreeItem<String> nodo2 = new TreeItem<String>(mens);
nodo.getChildren().add(nodo2);
indexName++;
}
dialog.close();
}
});
}
You are blocking the fx application thread by running the expensive operation on this thread. This prevents your UI from updating, including animating the GIF.
Move the expensive operations to a non-application thread instead and only use Platform.runLater() to "commit" the ui updates:
Runnable expensiveTask = () -> {
// expensive operations that should not run on the application thread
Match msg = stablishSearchConditions();
TreeItem<String> root = new TreeItem<>("ROOT");
int indexName = 1;
String mensaje = "Mensaje ";
for (Match message : msg.each()) {
TreeItem<String> nodo = new TreeItem<String>(mensaje + indexName);
root.getChildren().add(nodo);
root.setExpanded(true);
String mens = message.getMessage();
TreeItem<String> nodo2 = new TreeItem<String>(mens);
nodo.getChildren().add(nodo2);
indexName++;
}
// update ui -> application thread
Platform.runLater(() -> {
treeLabelResults.setText("");
arbol.setRoot(root);
dialog.close();
});
};
// start new thread for expensiveTask
new Thread(expensiveTask).start();

Resources