JavaFX8 MediaPlayer no sound error/wrong URL - java

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

Related

how to play radio channel over http on mediaplayer object?

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

MediaPlayer noSound OSX

I've been using MediaPlayer on win10 and it works perfectly fine, but on my MacBook Air (10.13.4 (17E202)) is no sound output at all. I've already found similar problems and observed that its caused by the GC.
I tried everything avoiding this, based on answers like this one
I tried making the media player final, static, private, public, working with getter and setter, outsourcing, as a class var, as a local var but nothing is working.
I'm running on Java 9.0.4.
If I add player.setCycleCount(MediaPlayer.INDEFINITE); the sound plays, but its looping and if I set it to 2 its working well, but my next , back, play, pause buttons are getting very weird then.
Using AudioClip instead is no option for me, because I have to use the MediaPlayer .
public class Main extends Application {
MediaPlayer player;
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
File file = new File("/users/xxx/desktop/xxx/Song.mp3");
Media media = new Media(file.toURI().toString());
this.player = new MediaPlayer(media);
player.play();
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
}
I would appreciate every help.
Try adding player.setStartTime(new Duration(0)); before player.play()
I solved the problem. I had to put player.stop(); before the player.start(); call.
¯\_(ツ)_/¯

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.

How to use JavaFX MediaPlayer correctly?

I'm writing a simple game and trying to play sounds but I can't get it to work when I create the Media object it throws IllegalArgumentException. I'm not much of a Java coder and any help will be appreciated.
Here is a sample code:
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Main{
public static void main(String[] args) {
Media pick = new Media("put.mp3"); //throws here
MediaPlayer player = new MediaPlayer(pick);
player.play();
}
}
Obviously "put.mp3" exists and located in the correct directory, I checked the path using: System.out.println(System.getProperty("user.dir"));
what am I doing wrong here?
The problem is because you are trying to run JavaFX scene graph control outside of JavaFX Application thread.
Run all JavaFX scene graph nodes inside the JavaFX application thread.
You can start a JavaFX thread by extending JavaFX Application class and overriding the start() method.
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
Media pick = new Media("put.mp3"); // replace this with your own audio file
MediaPlayer player = new MediaPlayer(pick);
// Add a mediaView, to display the media. Its necessary !
// This mediaView is added to a Pane
MediaView mediaView = new MediaView(player);
// Add to scene
Group root = new Group(mediaView);
Scene scene = new Scene(root, 500, 200);
// Show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
// Play the media once the stage is shown
player.play();
}
public static void main(String[] args) {
launch(args);
}
}
Ok thanks to #ItachiUchiha insight on the matter I was able to solve my problem, It seems that any code that uses javaFX must run from within javaFX application Thread but not every program has to use javaFX API. In short what I did is start my game from within the Application.start(Stage ps) like so:
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
new Game(9,9,BasicRobot.FACING.SOUTH, 19);
}
public static void main(String[] args) throws InterruptedException {
launch();
}
}
That way the Game class and everything it creates and uses can use javaFX. To play the sounds I created a Utils class:
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Utils {
public static void playSound(String fileName){
Media m = new Media("file:///" + System.getProperty("user.dir").replace('\\', '/') + "/" + fileName);
MediaPlayer player = new MediaPlayer(m);
player.play();
}
}
and now all I have to do to play a sound is call Utils.playSound("fileName.mp3") from anywhere inside my Game.
Maybe this would work:
MediaMetadataRetriever mediaMetadataRetriever1 =new MediaMetadataRetriever();
mediaMetadataRetriever1.setDataSource(getApplicationContext(), Uri.parse(myvidou_uri.toString()));
mediaPlayer1=MediaPlayer.create(getApplicationContext(),myvidou_uri);
mediaPlayer1.setDisplay(holder);
textView1.setText("info\n");

shows an error when i run my javafx application

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

Categories

Resources