This is what I have in controller:

@FXML
private MediaView mediaView;

In method that handle button pressed I got filechooser:

   File file = fileChooser.showOpenDialog(null);

    if(file != null){
        initPlayer(file.toURI().toString());
    }   

and this is method initPlayer:

private void initPlayer (String uri) {
        if (uri == null)
            return;
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer = null;
        }

        Media media = new Media(uri);
        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        mediaView = new MediaView(mediaPlayer);
        mediaPlayer.setOnReady(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
            }
        });
    }

this is part of code from my view created in scene builder:

<Pane layoutX="80.0" layoutY="14.0" prefHeight="480.0" prefWidth="640.0">
          <MediaView fx:id="mediaView" fitHeight="480.0" fitWidth="640.0" />
        </Pane>

When I choose file I am hearing sound but I don´t see video. What´s problem with this code? What I am missing?

有帮助吗?

解决方案

A MediaView instance is created by the FXMLLoader and placed in your Pane when you load your FXML file.

You don't need to create a new MediaView and when you do it is not being attached to the scene which is why you can't see anything.

Instead of:

  mediaView = new MediaView(mediaPlayer);

Write:

 mediaView.setMediaPlayer(mediaPlayer);

其他提示

If you hear only the sound and you are on Windows XP computer there may be missing codecs.

See http://docs.oracle.com/javafx/2/system_requirements_2-2/jfxpub-system_requirements_2-2.htm

Or you can check the error player provides, if there's any

mediaPlayer.setOnError(new Runnable() {    
    @Override
    public void run() {
        String message = mediaPlayer.errorProperty().get().getMessage();
        System.out.println(message);
    }
});

In your Fxml you need

<FlowPane alignment="CENTER" columnHalignment="CENTER" layoutX="9.0" layoutY="48.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="9.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="48.0">
<children>
<MediaView fx:id="mediaView">
</MediaView>
</children>
</FlowPane>

and in you controller :

 @FXML    private MediaPlayer mediaPlayer;
 @FXML    private Duration duration;
 @FXML    private MediaView mediaView;
 ....

 @Override
    public void initialize(URL url, ResourceBundle rb) {
        try {
            init();
        } catch (MalformedURLException ex) {
            Logger.getLogger(MainOnlineController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


  public void init() throws MalformedURLException { 
        ....
        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        mediaView.setMediaPlayer(mediaPlayer);
        mediaPlayer.setOnPlaying(new Runnable() {
                    public void run() {
                        if (stopRequested) {
                            mediaPlayer.pause();
                            stopRequested = false;
                        } else {
                            playButton.setText("||");
                        }
                    }
                });
    ...other methods for mediaPlayer
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top