how to play radio channel over http on mediaplayer object? - java

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

Related

Java Playing MP4 on top of an existing image

I have a stationary image and would like to play various MP4 clips on top of this image with some level of transparency. are there any examples for this in Java. I attempted JavaFX and can play the video but I need the image to be present (visible) at all times then I would like to play the video clips on top of the image. any suggestions/examples? I have code from an example but it only plays the video. I need to start with a still image then play the video on top with some transparency:
enter code here
public static void main(String[] args) throws Exception{
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
//goes to user Directory
File f = new File("c:\\tpd\\video.mp4");
Image background = new Image(new
FileInputStream("c:\\tpd\\Image.jpg"));
//Converts media to string URL
Media media = new Media(f.toURI().toURL().toString());
player = new javafx.scene.media.MediaPlayer(media);
MediaView viewer = new MediaView(player);
//change width and height to fit video
DoubleProperty width = viewer.fitWidthProperty();
DoubleProperty height = viewer.fitHeightProperty();
width.bind(Bindings.selectDouble(viewer.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(viewer.sceneProperty(), "height"));
viewer.setPreserveRatio(true);
ImageView foreground = new ImageView(background);
viewer.setOpacity(.6f);
StackPane root = new StackPane();
root.getChildren().addAll(foreground,viewer);
Scene scene = new Scene(root,1920, 1080, Color.BLACK);
stage.setAlwaysOnTop(true);
stage.setScene(scene);
stage.setTitle("myVideo");
stage.show();
player.play();
}

JavaFX Video Stutter (MediaView)

So I've been running into an issue lately. Whenever I want to play a videofile with JavaFX MediaPlayer combined with MediaView the video seems to stutter all over the place. I tried different types of videofiles (I rendered most of them myself as .mp4 with h264 encoding) where framerates range from 30fps to 60fps, the bitrate being 14.000.000 to 28.000.000 bps.
The lag also occurs with most files that I haven't rendered myself, so I'm quite sure that there is nothing wrong with my renderings.
edit
Further testing confirmed that it does not matter that I rendered the videofiles myself.
Do you guys know why this videostutter might be occuring and how to fix it? I'm open to any type of fixes, including using different ways of displaying the videofiles other than through JavaFX. Here is the main code of the videoplayer:
public class VideoPlayer extends JPanel implements Checkable
{
private MediaView view;
public JFXPanel pane;
private String path;
public MediaPlayer player;
private Dimension size;
public VideoPlayer(String path, Dimension size)
{
this.size = size;
this.path = path;
pane = new JFXPanel();
player = new MediaPlayer(new Media(path));
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX(pane);
}
});
pane.setPreferredSize(size);
pane.setSize(size);
pane.setLocation(0,0);
add(pane);
}
public void initFX(JFXPanel fxPanel) {
Scene scene = createScene();
pane.setScene(scene);
}
private Scene createScene()
{
view = new MediaView();
view.setMediaPlayer(player);
Group root = new Group();
view.setPreserveRatio(true);
view.setFitWidth(size.width);
view.setSmooth(false);
root.getChildren().add(view);
Scene scene = null;
try{
scene = new Scene(root, size.width, size.height)
;}catch(Exception e){System.out.println(e);}
return scene;
}
public void play()
{
player.play();
}
}
Here, the videoplayer receives a String with the path to the videofile, the Dimension size is the size the player should be.
The class itself extends JPanel so that it can be added to a JFrame. This JPanel contains a JFXPanel which in its turn contains the MediaView.
If you guys can figgure out why the video is getting stutter it would be amazing. I'm open to all solutions!
Thanks for the help in advance!
Edits
As suggested by #AlmasB I made a bare-bone pure JavaFX application to play the exact same files. It might have reduced stutter by a tiny amount, but perhaps not at all. It was very hard to see wether it was better or not. Here is the code of the bare-bones JavaFX videoplayer:
public class VideoPlayerTest extends Application {
static MediaView view;
static MediaPlayer player;
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
//root.getChildren().add(btn);
makeMediaView();
root.getChildren().add(view);
player.play();
Scene scene = new Scene(root, 1920, 1080);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
private static void makeMediaView(){
player = new MediaPlayer(new Media("file:///C:/Users/wesse/Documents/CowLite%20GabenQuest/resources/cutscenes/gabenquestepisode1intro.mp4.old"));
view = new MediaView(player);
view.setPreserveRatio(true);
view.setFitWidth(1920);
}
public static void main(String[] args) {
launch(args);
}
}
As suggested by #AlmasB some info:
System info
Windows 10
Latest JDK version (1.8)
gtx960, intel i5 3470, 8gb RAM
video tests
1080p 60fps 28mbit/s stutter
1080p 30fps 14mbit/s stutter
720p 29fps 1.8mbit/s no stutter, but poor video quality due to low bitrate/resolution, downloaded from YT
1080p 60fps 5.3mbit/s very slight stutter, decent video quality, downloaded from YT
1080p 60fps 6.3mbit/s medium stutter, perfect quality, self rendered
I'm rendering some videos myself now with lower bitrate to see if this is the problem.

JavaFX8 MediaPlayer no sound error/wrong URL

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

JavaFX MediaPlayer just stops after 1 minute+

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

JavaFx Video Dimension OUT of screen

I recently found javafx 2.1 very useful for my project of making a video player but after a
success I faced a problem with the video size Dimensions. In other words, when I run the
program and video is playing normally I can't see the whole video because it's dimensions
are bigger than my screen resolution .What Can I do in the following code to resize the actual size of video in windows7 64bit:
public class HelloFx extends Application {
public static void main(String[] args){
launch(args);
}
#Override
public void start(final Stage stage) throws Exception {
stage.setTitle("Movie Player");
final BorderPane root = new BorderPane();
final Media media = new Media("file:///Users//user//Videos//Sintel.mp4");
final MediaPlayer player = new MediaPlayer(media);
final MediaView view = new MediaView(player);
// System.out.println("media.width: "+media.getWidth());
root.getChildren().add(view);
final Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
player.setOnReady(new Runnable() {
#Override
public void run() {
int w = player.getMedia().getWidth();
int h = player.getMedia().getHeight();
stage.setMinWidth(w);
stage.setMinHeight(h);
}
});
//player.play();
}
}
The JavaFX 2 MediaView class has 2 functions which can help. They are .setFitHeight() and .setFitWidth() .
So, you could, instead of letting the media dictate the size of screen, let your stage set the size of the screen...
public void run() {
int w = stage.getWidth(); // player.getMedia().getWidth();
int h = stage.getHeight(); // player.getMedia().getHeight();
// stage.setMinWidth(w);
// stage.setMinHeight(h);
// make the video conform to the size of the stage now...
player.setFitWidth(w);
player.setFitHeight(h);
}
Then the video should fit inside of the stage. That above code is pretty crude, and you may want to "Scale" the video better, ie: find the ratio of the media width VS the stage width & media height VS stage height ... But that code above should get you started.

Categories

Resources