javauser-interfacejavafxmedia-playercorretto

Incompatible Types: javafx.scene.media.Media cannot be converted to javax.print.attribute.standard.Media


All,

I'm building a media player using Amazon Corretto 11 library and OpenJFX. I have this issue that's keeping me on the struggle. Still a newbie in Java, it will be much appreciated if someone can help me out on this.

Error while compiling:
D:\Java Masterclass\Media_Player\src\main\controller.java:30:59
java: incompatible types: javafx.scene.media.Media cannot be converted to javax.print.attribute.standard.Media
Main.java file:

package main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
        controller.primaryStage = primaryStage;
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setTitle("Media Player");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}
Main.fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.media.*?>

<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.controller">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="Video">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#loadVideo" text="Load" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <left>
      <ListView fx:id="videoList" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </left>
   <bottom>
      <Button mnemonicParsing="false" onMouseClicked="#toggleVideoPlay" prefHeight="26.0" prefWidth="95.0" text="Play / Pause" BorderPane.alignment="CENTER" />
   </bottom>
   <center>
      <MediaView fx:id="player" fitHeight="400.0" fitWidth="600.0" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>
videoItem.java file:

package customViews;

import javafx.scene.control.Label;

import javax.print.attribute.standard.Media;

public class videoItem extends Label {

    private Media video;

    public videoItem(String videoPath, Media video){
        this.video = video;
        super.setText("Video name: "+ getVideoName(videoPath));
    }

    private String getVideoName(String videoPath) {
        String[] pathParts = videoPath.split("/");
        return pathParts[pathParts.length - 1];
    }

    public Media getVideo() {
        return video;
    }
}
Controller.java file:

package main;

import customViews.videoItem;
import javafx.event.ActionEvent;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class controller {
    static Stage primaryStage;
    public MediaView player;
    public ListView<videoItem> videoList;

    public void loadVideo(ActionEvent actionEvent) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Video File");
        String videoPath = fileChooser.showOpenDialog(primaryStage).toURI().toString();
        Media video = new Media(videoPath);
        playVideo(video);

        addToLibrary(videoPath, video);
    }

    private void addToLibrary(String videoPath, Media video) {
        videoList.getItems().add(new videoItem(videoPath, **video**));
    }

    private void playVideo(Media video) {
        MediaPlayer mediaPlayer = new MediaPlayer(video);
        mediaPlayer.setAutoPlay(false);
        player.setMediaPlayer(mediaPlayer);
    }

    public void toggleVideoPlay(MouseEvent mouseEvent) {
        if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
            if (player.getMediaPlayer().getStatus().equals(MediaPlayer.Status.PLAYING)){
                player.getMediaPlayer().pause();
            } else {
                player.getMediaPlayer().play();
            }
        }
    }
}

In controller.java file, the video parameter passed goes back to javafx.scene.media.Media, but the intention is to have it from javax.print.attribute.standard.Media specified in videoItem.java. Imported javax.print.attribute.standard.Media but that didn't work, as both as the same Media naming conventions.

I am trying to build a list of opened videos, per below:

MediaPlayer SceneBuilder Screenshot

Thanks a mil for anyone that can help me on this.

Cheers


Solution

  • Newbie mistake I guess. videoItem file should never contain javax.print.attribute.standard.Media but rather javafx.scene.media.Media.