I would like to use MediaView to play video streams and video files generated by the Shinobi media server but MediaView does not seem to be able to handle any stream or file generated by Shinobi.
I am using Java 18 and JavaFX 19 (I have tried older versions as well).
I have one sample file generated by Shinobi here.
It plays fine in VLC, which shows that the file has the following attributes:
Codec: H264 - MPEG-4 AVC (part 10) (avc1)
Video resolution: 1280x720
Decoded format: Planar 4:2:2 YUV full scale
Chroma location: left
I have added error handlers to the MediaPlayer, MediaView and Media objects but there are no errors when I attempt to play the file.
Does anybody have any ideas as to why the player wouldn't like the file above?
Has anybody had success playing files from Shinobi (it uses FFMPEG under the covers.)?
It does play other files like:
https://coderslegacy.com/wp-content/uploads/2020/07/Pygame_Platformer-1.mp4";
I expect the video to play without errors or for the player to tell me why it can not play the video.
Here is my code:
package com.example.videotester;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaErrorEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;
public class HelloApplication extends Application {
#Override
public void start(Stage stage) throws Exception {
Button button1 = new Button("Play");
Button button2 = new Button("Pause");
Button button3 = new Button("Stop");
String path = "C:/Users/Rob/Desktop/rlrO5DVBJS-2022-10-26T20-52-34.mp4";
File f = new File(path);
path = f.toURI().toString();
// path="http://192.168.1.239:8080/532046fecc8da376f3f32f5518bad33b/videos/NUW6mXm9CF/rlrO5DVBJS/2022-10-26T14-32-07.mp4";
// path="http://192.168.1.239:8080/2921f1ca7204e640734709e29fc2033f/hls/NUW6mXm9CF/rlrO5DVBJS/s.m3u8";
// path="http://192.168.1.239:8080/2921f1ca7204e640734709e29fc2033f/h265/NUW6mXm9CF/rlrO5DVBJS/s.hevc";
// path="http://192.168.1.239:8080/2921f1ca7204e640734709e29fc2033f/mp4/NUW6mXm9CF/rlrO5DVBJS/s.mp4";
// path="http://192.168.1.239:8080/5ee9ca532fd17f860e3cef43a288b951/mjpeg/NUW6mXm9CF/rlrO5DVBJS"; //mjpeg
// path="http://192.168.1.239:8080/f7fb8d581d5aab4ebb8732de13b61337/videos/NUW6mXm9CF/rlrO5DVBJS/2022-10-26T15-57-19.mp4";
// path="http://192.168.1.239:8080/5ee9ca532fd17f860e3cef43a288b951/videos/NUW6mXm9CF/rlrO5DVBJS/2022-10-27T19-49-31.mp4";
// path="http://192.168.1.239:8080/c6c8a86382548433c505d9e7cf7c2085/videos/NUW6mXm9CF/rlrO5DVBJS/2022-10-26T04-05-00.mp4";
// path="https://coderslegacy.com/wp-content/uploads/2020/07/Pygame_Platformer-1.mp4";
// path="https://www.dropbox.com/s/h1ky0he5dvclhkt/rlrO5DVBJS-2022-10-30T20-53-29.mp4?dl=0";
//Instantiating Media class
// Media media = new Media(new File(path).toURI().toString());
//URL url = new URL(path);
final Media media;
final MediaPlayer mediaPlayer;
MediaView mediaView = null;
try {
media = new Media(path);
if (media.getError() == null) {
media.setOnError(() -> System.out.println("media player error : " + media.getError()));
try {
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
button1.setOnAction(e -> mediaPlayer.play());
button2.setOnAction(e -> mediaPlayer.pause());
button3.setOnAction(e -> mediaPlayer.stop());
mediaPlayer.setOnReady(() -> System.out.println("Video player ready"));
if (mediaPlayer.getError() == null) {
mediaPlayer.setOnError(() -> System.out.println("media player error : " + mediaPlayer.getError()));
mediaView = new MediaView(mediaPlayer);
mediaView.setOnError(mee -> System.out.println("media view error : " + t));
} else
System.out.println("Error in media player: " + mediaPlayer.getError());
} catch (Exception mediaPlayerException) {
System.out.println("media player exception " + mediaPlayerException);
}
} else
System.out.println("Error media creating media " + media.getError());
} catch (Exception mediaException) {
// Handle exception in Media constructor.
System.out.println("Handle exception " + mediaException);
System.exit(1);
}
GridPane layout = new GridPane();
layout.setHgap(10);
layout.setVgap(10);
layout.add(button1, 0, 0);
layout.add(button2, 1, 0);
layout.add(button3, 2, 0);
layout.add(mediaView, 0, 1, 4, 1);
Scene scene = new Scene(layout, 300, 200);
stage.setTitle("Video Player Tester");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
Rob - your file in
https://www.dropbox.com/s/h1ky0he5dvclhkt/rlrO5DVBJS-2022-10-30T20-53-29.mp4?dl=0
has a color subsampling of 4:2:2 (color information scaled to 50%) most consumer video is 4:2:0 (color information scaled to 25%).
The file at https://coderslegacy.com/wp-content/uploads/2020/07/Pygame_Platformer-1.mp4 is 4:2:0.
I am guessing that the Java media player is passing the video to the operating system and the OS's default decoder may not support the higher color resolution of 4:2:2.
Related
I have List<MediaPlayer> that is populating by
private List<MediaPlayer> players() {
for (String file : path.list((dir1, name) -> {
if (name.endsWith(SUPPORTED_VIDEO_FILE_EXTENSIONS)){
return true;
}
return false;
})) {
players.add(createPlayer(Paths.get(path + "/" + file).toUri().toString()));
System.out.println(Paths.get(path + "/" + file).toUri().toString());
}
return players;
}
In my case size of List<MediaPlayer> is 3. Than i use it there
if (currentNumOfVideo == -1) {
currentNumOfVideo = 0;
}
mv = new MediaView();
MediaPlayer currentPlayer = players.get(currentNumOfVideo);
mv.setMediaPlayer(currentPlayer);
currentPlayer.play();
players.get(currentNumOfVideo).setOnEndOfMedia(() -> {
players.get(currentNumOfVideo).stop();
if (currentNumOfVideo < players.size()) {
currentNumOfVideo += 1;
mv.setMediaPlayer(players.get(currentNumOfVideo));
players.get(currentNumOfVideo).play();
} else {
currentNumOfVideo = 0;
mv.setMediaPlayer(players.get(currentNumOfVideo));
players.get(currentNumOfVideo).play();
}
});
First video playing, when it ends second video starts. After second video MediaPlayer stops and didn't play third video.
I understand that because of my setOnEndOfMedia that is only on first MediaPlayer. When the second video starts it doesn't have setOnEndOfMedia. How can I setOnEndOfMedia on every video in my List<MediaPlayer>.
Personally, I would not create all the MediaPlayer instances ahead-of-time. Instead, get a list of Media objects or at least the URIs pointing to the media. Then create a method which is responsible for playing the next video when the current video ends. That method will dispose the old MediaPlayer, create the next MediaPlayer, and configure it to call the same method upon completion. For example:
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
public class Main extends Application {
private MediaView mediaView;
private List<Media> media;
private int curIndex;
#Override
public void start(Stage primaryStage) throws Exception {
media = ...; // get media from somewhere
mediaView = new MediaView();
primaryStage.setScene(new Scene(new StackPane(mediaView), 720, 480));
primaryStage.show();
playNextVideo();
}
private void playNextVideo() {
disposePlayer();
if (curIndex == media.size()) {
return; // no more videos to play
}
MediaPlayer player = new MediaPlayer(media.get(curIndex++));
player.setAutoPlay(true); // play ASAP
player.setOnEndOfMedia(this::playNextVideo); // play next video when this one ends
mediaView.setMediaPlayer(player);
}
private void disposePlayer() {
MediaPlayer player = mediaView.getMediaPlayer();
if (player != null) {
player.dispose(); // release resources
}
}
}
This may cause a pause between videos. If that's not acceptable you could create the next MediaPlayer ahead-of-time, either at the same time as the current MediaPlayer or when the current player reaches a certain timestamp (e.g. 10 seconds before the end). But I still wouldn't create all the MediaPlayer instances ahead-of-time.
I'm trying to make a program with some filters of image by using JavaFx, so I need two button at least, one is a file chooser to open an image, and another one will be a choice box that allows to choose a filter.
My problem is how could a choice box get the path name or a file object from the file chooser.
here is my program unfinished :
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Filter extends Application{
public void start(final Stage stage) {
stage.setTitle("FilterShop");
final FileChooser fileChooser = new FileChooser();
final Button openButton = new Button("Select a photo");
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.getItems().add("Choose a Filter");
choiceBox.getItems().addAll("Remove watermark", "Brightness", "Grey", "Mosaic");
choiceBox.getSelectionModel().selectFirst();
final Pane stac = new Pane();
openButton.setOnAction(e -> {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
Image image = new Image(file.toURI().toString());
ImageView imageView = new ImageView(image);
imageView.setX(50);
imageView.setY(50);
imageView.setFitWidth(300);
imageView.setFitHeight(470);
imageView.setPreserveRatio(true);
stac.getChildren().add(imageView);
}
});
choiceBox.setOnAction(event1 -> {
if (choiceBox.getValue() == "Mosaic") {
try {
BufferedImage imagen = ImageIO.read(/* A file object is needed here. */ );
new Mosaic().mosaico(imagen, 80, 80);
} catch (IOException ie) {
System.err.println("I/O Error");
ie.printStackTrace(System.err);
}
}
});
openButton.setLayoutX(300);
openButton.setLayoutY(350);
choiceBox.setLayoutX(430);
choiceBox.setLayoutY(350);
stac.getChildren().addAll(openButton, choiceBox);
stage.setScene(new Scene(stac, 800, 400));
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
I am not sure what is the exact issue you are facing. Firstly FileChooser is not a Button. It is a helper class to interact with ToolKit to open the OS specific file chooser and return the results. And obvisously, it will no keep record of the returned results.
It is your duty to keep a reference of the retrieved file. This can be done in "N" number of ways. As you question focuses on get getting the value from the open button, I would suggest the below approach.
openButton.setOnAction(e -> {
File file = fileChooser.showOpenDialog(stage);
openButton.getProperties().put("FILE_LOCATION", file.getAbsolutePath());
...
});
choiceBox.setOnAction(event1 -> {
if (choiceBox.getValue() == "Mosaic") {
File file = new File(openButton.getProperties().get("FILE_LOCATION").toString());
}
});
I am open A JavaFX application using a Jframe.And after using JavaFx application window i close the window .And want to again open this same Javafx window but an error occur-
java.lang.IllegalStateException: Application launch must not be called more than once
As stated in the documentation, calling Application.launch(...) more than once will result in an exception:
public static void launch(String... args)
Launch a standalone application. This method is typically called from
the main method(). It must not be called more than once or an
exception will be thrown.
If you need to mix Swing and JavaFX, you should embed the JavaFX pieces in a JFXPanel and place it in a JFrame. You can then show and hide the JFrame as often as you need, using setVisible(...). An application working this way will not have an Application subclass at all.
Mixing Swing and JavaFX is tricky, and not recommended for beginners. The problem is that each toolkit has its own UI thread, and all access of the UI must be executed on the correct UI thread (i.e. the AWT event dispatch thread for Swing/AWT components, and the JavaFX Application Thread for JavaFX components). Data that is shared between both must provide proper synchronization to ensure that it is safely accessible from multiple threads.
Here is a very simple example. Clicking the button will show the window with FX content. If you close that window, and then click the button again, it will be shown again.
import java.awt.BorderLayout;
import java.util.Random;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SwingFXExample {
private JFrame mainFrame ;
private JFrame fxFrame ;
private JFXPanel fxPanel ;
public SwingFXExample() {
// must be on Swing thread...
if (! SwingUtilities.isEventDispatchThread()) {
throw new IllegalStateException("Not on Event Dispatch Thread");
}
mainFrame = new JFrame();
JButton showFX = new JButton("Show FX Window");
JPanel content = new JPanel();
content.add(showFX);
mainFrame.add(content, BorderLayout.CENTER);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fxFrame = new JFrame();
fxPanel = new JFXPanel();
fxFrame.add(fxPanel);
fxFrame.setSize(640, 640);
fxFrame.setLocationRelativeTo(null);
Platform.runLater(() -> initFX());
showFX.addActionListener(event -> fxFrame.setVisible(true));
}
private void initFX() {
// must be on FX Application Thread...
if (! Platform.isFxApplicationThread()) {
throw new IllegalStateException("Not on FX Application Thread");
}
LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis());
Series<Number, Number> series = new Series<>();
series.setName("Random data");
Random rng = new Random();
for (int i = 0; i <= 10; i++) {
series.getData().add( new Data<>(i, 100*rng.nextDouble()) );
}
chart.getData().add(series);
chart.setOnMouseClicked(evt -> {
if (evt.getClickCount() == 2) {
System.out.println("Double click!");
}
});
fxPanel.setScene(new Scene(chart, 400, 400));
}
public void showMainWindow() {
mainFrame.setSize(350, 120);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SwingFXExample app = new SwingFXExample();
app.showMainWindow();
});
}
}
In a JavaFX dialog, I would like to show a list of files with their icons and file names.
It was easy to find how to get an icon for a file extension:
File file = File.createTempFile("icon", ".doc");
FileSystemView view = FileSystemView.getFileSystemView();
java.swing.Icon icon = view.getSystemIcon(file);
file.delete();
But, how can I draw that Swing Icon in a JavaFX ListView?
private static class AttachmentListCell extends ListCell<String> {
#Override
public void updateItem(String fileName, boolean empty) {
if (item != null) {
// Get file Icon for fileName as shown above.
java.swing.Icon icon =
// Transform Icon to something that can be
// added to the box, maybe an ImageView.
javafx.scene.image.ImageView image = ???
// Label for file name
Label label = new Label(item);
HBox box = new HBox();
box.getChildren().addAll(image, label);
setGraphic(box);
}
}
}
Here is an example for a list view with file icons:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javax.swing.filechooser.FileSystemView;
public class ListViewCellFactory2 extends Application {
ListView<String> list = new ListView<String>();
ObservableList<String> data = FXCollections.observableArrayList(
"a.msg", "a1.msg", "b.txt", "c.pdf",
"d.html", "e.png", "f.zip",
"g.docx", "h.xlsx", "i.pptx");
#Override
public void start(Stage stage) {
VBox box = new VBox();
Scene scene = new Scene(box, 200, 200);
stage.setScene(scene);
stage.setTitle("ListViewSample");
box.getChildren().addAll(list);
VBox.setVgrow(list, Priority.ALWAYS);
list.setItems(data);
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
#Override
public ListCell<String> call(ListView<String> list) {
return new AttachmentListCell();
}
});
stage.show();
}
private static class AttachmentListCell extends ListCell<String> {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
Image fxImage = getFileIcon(item);
ImageView imageView = new ImageView(fxImage);
setGraphic(imageView);
setText(item);
}
}
}
public static void main(String[] args) {
launch(args);
}
static HashMap<String, Image> mapOfFileExtToSmallIcon = new HashMap<String, Image>();
private static String getFileExt(String fname) {
String ext = ".";
int p = fname.lastIndexOf('.');
if (p >= 0) {
ext = fname.substring(p);
}
return ext.toLowerCase();
}
private static javax.swing.Icon getJSwingIconFromFileSystem(File file) {
// Windows {
FileSystemView view = FileSystemView.getFileSystemView();
javax.swing.Icon icon = view.getSystemIcon(file);
// }
// OS X {
//final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
//javax.swing.Icon icon = fc.getUI().getFileView(fc).getIcon(file);
// }
return icon;
}
private static Image getFileIcon(String fname) {
final String ext = getFileExt(fname);
Image fileIcon = mapOfFileExtToSmallIcon.get(ext);
if (fileIcon == null) {
javax.swing.Icon jswingIcon = null;
File file = new File(fname);
if (file.exists()) {
jswingIcon = getJSwingIconFromFileSystem(file);
}
else {
File tempFile = null;
try {
tempFile = File.createTempFile("icon", ext);
jswingIcon = getJSwingIconFromFileSystem(tempFile);
}
catch (IOException ignored) {
// Cannot create temporary file.
}
finally {
if (tempFile != null) tempFile.delete();
}
}
if (jswingIcon != null) {
fileIcon = jswingIconToImage(jswingIcon);
mapOfFileExtToSmallIcon.put(ext, fileIcon);
}
}
return fileIcon;
}
private static Image jswingIconToImage(javax.swing.Icon jswingIcon) {
BufferedImage bufferedImage = new BufferedImage(jswingIcon.getIconWidth(), jswingIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
jswingIcon.paintIcon(null, bufferedImage.getGraphics(), 0, 0);
return SwingFXUtils.toFXImage(bufferedImage, null);
}
}
EDIT: On a HDPI monitor, the file icons are clipped.
What has to be done to have the icons entirely displayed in the row?
How can I retrieve the scale factor from Java?
I came up with this code, which seems to work for converting the icon returned by getSystemIcon to a format which JavaFX can understand. It does this by using a combo of SwingUtilities.invokeLater with Platform.runLater to try to mitigate any potential threading issues between the two projects. The javax.swing.Icon is painted to java.awt.BufferedImage which is converted by SwingFXUtils to a JavaFX Image.
So the Swing Icon => JavaFX image portion of the code you were asking about seems to work. The only thing is, when I tested the application on OS X 10.7 running Oracle Java8u20, every file extension type I tried gave the exact same icon. So it would seem that your method for getting an icon from the system via the swing FileSystemView isn't really supported on OS X (as far as I can tell). I guess you could try it on another OS and see if it works for you there (presuming that supporting this icon lookup feature on OS X is not necessary for your task).
So googling around brought up the following question:
Access file type icons Mac OSX
And in that, Sam Barnum recommends a FileView works much better than a FileSystemView - and so it did for me. I switched your code to use a FileView and after that started getting different icons for different file types on OS X. The icons were still really small 16x16 icons (I wouldn't know how to retrieve the hi-res retina icons for instance), but at least the icons which were retrieved appeared correct and file type specific.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class FileIconViewer extends Application {
#Override
public void start(Stage stage) throws IOException {
Runnable fetchIcon = () -> {
File file = null;
try {
file = File.createTempFile("icon", ".png");
// commented code always returns the same icon on OS X...
// FileSystemView view = FileSystemView.getFileSystemView();
// javax.swing.Icon icon = view.getSystemIcon(file);
// following code returns different icons for different types on OS X...
final javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
javax.swing.Icon icon = fc.getUI().getFileView(fc).getIcon(file);
BufferedImage bufferedImage = new BufferedImage(
icon.getIconWidth(),
icon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB
);
icon.paintIcon(null, bufferedImage.getGraphics(), 0, 0);
Platform.runLater(() -> {
Image fxImage = SwingFXUtils.toFXImage(
bufferedImage, null
);
ImageView imageView = new ImageView(fxImage);
stage.setScene(
new Scene(
new StackPane(imageView),
200, 200
)
);
stage.show();
});
} catch (IOException e) {
e.printStackTrace();
Platform.exit();
} finally {
if (file != null) {
file.delete();
}
}
};
javax.swing.SwingUtilities.invokeLater(fetchIcon);
}
public static void main(String[] args) { launch(args); }
}
Note: there is an existing request in the JavaFX issue tracker to add this functionality to JavaFX (it is currently not scheduled for implementation, though you could log into the issue tracker and vote for the issue, comment on it, link it back to this StackOverflow question, etc):
RT-19583 Possibility to get native icons, on different sizes.
I am new to Java and are trying to display an image. I got code on the net but when trying it I get an error with the importing of " import javax.imageio.ImageIO;" The error message reads "javax.imageio.ImageIO" is either a misplace package name or a non-existing entity.
I have seen this on many samples but it does not work with me.
Is there any advice
mport java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Showmap extends Panel
{
BufferedImage img;
public Showmap ()
{
try
{
image = ImageIO.read (new File ("KNP.jpg"));
}
/*
catch (IOException e)
{
BufferedImage image;
public ShowImage() {
try {
System.out.println("Enter image name\n");
BufferedReader bf=new BufferedReader(new
InputStreamReader(System.in));
String imageName=bf.readLine();
File input = new File(imageName);
image = ImageIO.read(input);
}*/
catch (IOException e)
{
System.out.println ("Error:" + e.getMessage ());
}
}
public void paint (Graphics g)
{
g.drawImage (image, 0, 0, null);
}
static public void main (String args []) throws
Exception
{
JFrame frame = new JFrame ("Display image");
Panel panel = new Showmap ();
frame.getContentPane ().add (panel);
frame.setSize (500, 500);
frame.setVisible (true);
}
}
Thanks
Ivan
In your Project select:
Right Click on "JRE System Libary"
Select Properties
On Execution Enviroment select "J2SE-1.5(jre8)" or later; you should use the latest version of jre8
I was programming with "Ready to Program" and tried many options with out success. When I copied the same code to "JCreator" and run it fro there it was working fine. Seems "import javax.imageio.ImageIO;" is not working with "Ready to Program".