I'm using the MediaPlayer class in JavaFX to run the media the whole time untill you close the application but the MediaPlayer just stops after 1 minute and the file is 11 minutes long. This is my code:
#Override
public void start(Stage primaryStage) throws Exception {
FileChooser chooser = new FileChooser();
File file = chooser.showOpenDialog(primaryStage);
Media media = null;
if(file != null) {
media = new Media(file.toURI().toString());
}
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
mediaPlayer.play();
Group root = new Group();
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
Hotfix, try adding the following code inside start()
primaryStage.setOnCloseRequest(windowEvent -> {
mediaPlayer.stop();
});
Related
I am trying to play radio channel through http on mediaplayer object but its not working (blank screen appears)
#Override
public void start(Stage primaryStage) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
primaryStage.setTitle("Embedded Media Player");
Group root = new Group();
Scene scene = new Scene(root, 540, 241);
URL channel=new URL ("http://178.33.178.204:9322/stream?type=http&nocache=353");
Media media= new Media(channel.toString());
MediaPlayer player= new MediaPlayer(media);
Button play= new Button("play");
MediaView mediaView = new MediaView(player);
player.setAutoPlay(true);
root.getChildren().add(mediaView);
primaryStage.setScene(scene);
primaryStage.show();
}
i tried to play local its working and video also
but when i try to play it through my code its local video and audio works but http not working
The HTTP request that you are providing in the MediaPlayer constructor seems to be the issue. The JavaFX MediaPlayer documentation should provide information for the supported file types.
EDIT: See if the MediaPlayer is returning any errors.
player.setOnError(new Runnable() {
#Override
public void run() {
String message = player.errorProperty().get().getMessage();
System.out.println(message);
}});
1.I have a code that opens a pane and I can open file explorer, but I do not know how to open said image when I select it from file explorer. Also this code is meant to be used in different #Overrride statements but I have only managed to get this far using one statement. Is there a way for me to call some of these events from another Override statement?
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
ImageView myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
EventHandler<ActionEvent> loadEventListener
= t -> {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
ImageView myImageView = new ImageView();
myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
}
};
Since you already have an ImageView and have shown it in the scene, simply set its image to the image you load.
For this you need to make the ImageView an instance variable:
private ImageView myImageView ;
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
// presumably you intended this somewhere?
pane.setCenter(rootBox);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
Note there is no need to load a buffered image and then convert it. So all you need in the event handler is
loadEventListener = t -> {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
if (file != null) {
Image image = new Image(file.toURI().toString());
myImageView.setImage(image);
}
};
I'm trying to edit five images I have in a folder at once (using javafx motion blur), rather than select them one after the other. This is my code, I'm not exactly sure what I'm doing wrong but when I run it, only the last image in the folder gets edited. The others remain as they are.
public class IterateThrough extends Application {
private Desktop desktop = Desktop.getDesktop();
#Override
public void start(Stage primaryStage) throws IOException {
File dir = new File("filepath");
File [] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File file : directoryListing) {
if(file.getName().toLowerCase().endsWith(".jpeg")){
//desktop.open(file);
Image image = new Image(new FileInputStream(file));
//Setting the image view
ImageView imageView = new ImageView(image);
//setting the fit height and width of the image view
imageView.setFitHeight(600);
imageView.setFitWidth(500);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
// instantiate Motion Blur class
MotionBlur motionBlur = new MotionBlur();
// set blur radius and blur angle
motionBlur.setRadius(15.0);
motionBlur.setAngle(110.0);
//set imageView effect
imageView.setEffect(motionBlur);
//Creating a Group object
Group root = new Group(imageView);
//Creating a scene object
Scene scene = new Scene(root, 600, 500);
//Setting title to the Stage
primaryStage.setTitle("Loading an image");
//Adding scene to the stage
primaryStage.setScene(scene);
//Displaying the contents of the stage
primaryStage.show();
}
}
}
}
public static void main(String[] args) {
launch(args);
}
}
first, you shouldn't just let the primaryStage show() in the for-each loop, because when the primaryStage first show(), the fx thread pause on that instruction, the remaining pictures will not be read and when you close the window, the loop won't continue to go on for remaining, it represents the end of the application.
so it's better to let the primaryStage show() outside the each-loop after all image has been read.
second, you shouldn't use the "Group" container to contains all images, better use VBox/HBox/FlowPane, make sure above image won't covers below's.
here's modified code for reference.
public class IterateThrough2 extends Application{
private Desktop desktop = Desktop.getDesktop();
#Override
public void start(Stage primaryStage) throws IOException{
File dir = new File("filepath");
File[] directoryListing = dir.listFiles();
FlowPane root = new FlowPane();
if(directoryListing != null){
for(File file : directoryListing){
if(file.getName().toLowerCase().endsWith(".jpeg")){
Image image = new Image(new FileInputStream(file));
ImageView imageView = new ImageView(image);
imageView.setFitHeight(600);
imageView.setFitWidth(500);
imageView.setPreserveRatio(true);
MotionBlur motionBlur = new MotionBlur();
motionBlur.setRadius(15.0);
motionBlur.setAngle(110.0);
imageView.setEffect(motionBlur);
root.getChildren().add(imageView);
}
}
}
Scene scene = new Scene(root, 600, 500);
primaryStage.setTitle("Loading an image");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}
I'm working on JavaFX 8 app and I'm trying to open MP3 file using MediaPlayer.
I had passed wrong URL errors and I've no exceptions right now, when I start such part of code, but app opens and there is no sound. Tried with some oracle tutorial and when i put such URL: "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv" everything is working so I guess it's still wrong URL, but app is starting and I've litterally no clue whats wrong.
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
//Add a scene
Group root = new Group();
Scene scene = new Scene(root, 500, 200);
File file = new File("C:\\Users\\Me\\Desktop\\SomeFile.mp3");
Media media = new Media(file.toURI().toASCIIString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
// create mediaView and add media player to the viewer
MediaView mediaView = new MediaView(mediaPlayer);
((Group)scene.getRoot()).getChildren().add(mediaView);
//show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Considering the file is correctly located and loaded you can do the following:
file.toURI().toURL().toExternalForm()
If you are writing an app that plays various media files, consider using user interface to obtain paths to external file resources instead of hardcoding them. You can use FileChooser or DirectoryChooser
i am trying to create a videoplayer with javafx, but when I try to run the class, it shows some error
public class MoviePlayer extends Application{
public static void main (String [] args){
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
Group root = new Group();
Media media = new Media("C:\\Users\\goldAnthony\\Videos\\Whistle.mp4");
MediaPlayer player = new MediaPlayer(media);
MediaView view = new MediaView(player);
root.getChildren().add(view);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
}
this is the error it shows
Error: Could not find or load main class Player.VideoPlayer2
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
some i have asked says the code works fine, i dont really know what seems to be the problem because it gives this error above