Module javafx.media

Class MediaView

  • All Implemented Interfaces:
    Styleable, EventTarget


    public class MediaView
    extends Node
    A Node that provides a view of Media being played by a MediaPlayer.

    The following code snippet provides a simple example of an Application.start() method which displays a video:

    
     public void start(Stage stage) {
         // Create and set the Scene.
         Scene scene = new Scene(new Group(), 540, 209);
         stage.setScene(scene);
    
         // Name and display the Stage.
         stage.setTitle("Hello Media");
         stage.show();
    
         // Create the media source.
         String source = getParameters().getRaw().get(0);
         Media media = new Media(source);
    
         // Create the player and set to play automatically.
         MediaPlayer mediaPlayer = new MediaPlayer(media);
         mediaPlayer.setAutoPlay(true);
    
         // Create the view and add it to the Scene.
         MediaView mediaView = new MediaView(mediaPlayer);
         ((Group) scene.getRoot()).getChildren().add(mediaView);
     }
     
    The foregoing code will display the video as:

    Hello Media
    Since:
    JavaFX 2.0
    • Property Detail

      • mediaPlayer

        public final ObjectProperty<MediaPlayer> mediaPlayerProperty
        The mediaPlayer whose output will be handled by this view. Setting this value does not affect the status of the MediaPlayer, e.g., if the MediaPlayer was playing prior to setting mediaPlayer then it will continue playing.
        See Also:
        getMediaPlayer(), setMediaPlayer(MediaPlayer)
      • preserveRatio

        public final BooleanProperty preserveRatioProperty
        Whether to preserve the aspect ratio (width / height) of the media when scaling it to fit the node. If the aspect ratio is not preserved, the media will be stretched or sheared in both dimensions to fit the dimensions of the node. The default value is true.
        See Also:
        isPreserveRatio(), setPreserveRatio(boolean)
      • smooth

        public final BooleanProperty smoothProperty
        If set to true a better quality filtering algorithm will be used when scaling this video to fit within the bounding box provided by fitWidth and fitHeight or when transforming. If set to false a faster but lesser quality filtering will be used. The default value depends on platform configuration.
        See Also:
        isSmooth(), setSmooth(boolean)
      • fitWidth

        public final DoubleProperty fitWidthProperty
        Determines the width of the bounding box within which the source media is resized as necessary to fit. If value ≤ 0, then the width of the bounding box will be set to the natural width of the media, but fitWidth will be set to the supplied parameter, even if non-positive.

        See preserveRatio for information on interaction between media views fitWidth, fitHeight and preserveRatio attributes.

        See Also:
        getFitWidth(), setFitWidth(double)
      • fitHeight

        public final DoubleProperty fitHeightProperty
        Determines the height of the bounding box within which the source media is resized as necessary to fit. If value ≤ 0, then the height of the bounding box will be set to the natural height of the media, but fitHeight will be set to the supplied parameter, even if non-positive.

        See preserveRatio for information on interaction between media views fitWidth, fitHeight and preserveRatio attributes.

        See Also:
        getFitHeight(), setFitHeight(double)
      • viewport

        public final ObjectProperty<Rectangle2D> viewportProperty
        Specifies a rectangular viewport into the media frame. The viewport is a rectangle specified in the coordinates of the media frame. The resulting bounds prior to scaling will be the size of the viewport. The displayed image will include the intersection of the frame and the viewport. The viewport can exceed the size of the frame, but only the intersection will be displayed. Setting viewport to null will clear the viewport.
        See Also:
        getViewport(), setViewport(Rectangle2D)
    • Constructor Detail

      • MediaView

        public MediaView​()
        Creates a MediaView instance with no associated MediaPlayer.
      • MediaView

        public MediaView​(MediaPlayer mediaPlayer)
        Creates a MediaView instance associated with the specified MediaPlayer. Equivalent to
        
         MediaPlayer player; // initialization omitted
         MediaView view = new MediaView();
         view.setMediaPlayer(player);
         
        Parameters:
        mediaPlayer - the MediaPlayer the playback of which is to be viewed via this class.
    • Method Detail

      • setMediaPlayer

        public final void setMediaPlayer​(MediaPlayer value)
        Sets the MediaPlayer whose output will be handled by this view.
        Parameters:
        value - the associated MediaPlayer.
      • getMediaPlayer

        public final MediaPlayer getMediaPlayer​()
        Retrieves the MediaPlayer whose output is being handled by this view.
        Returns:
        the associated MediaPlayer.
      • mediaPlayerProperty

        public final ObjectProperty<MediaPlayer> mediaPlayerProperty​()
        The mediaPlayer whose output will be handled by this view. Setting this value does not affect the status of the MediaPlayer, e.g., if the MediaPlayer was playing prior to setting mediaPlayer then it will continue playing.
        See Also:
        getMediaPlayer(), setMediaPlayer(MediaPlayer)
      • setOnError

        public final void setOnError​(EventHandler<MediaErrorEvent> value)
        Sets the error event handler.
        Parameters:
        value - the error event handler.
      • getOnError

        public final EventHandler<MediaErrorEvent> getOnError​()
        Retrieves the error event handler.
        Returns:
        the error event handler.
      • setPreserveRatio

        public final void setPreserveRatio​(boolean value)
        Sets whether to preserve the media aspect ratio when scaling.
        Parameters:
        value - whether to preserve the media aspect ratio.
      • isPreserveRatio

        public final boolean isPreserveRatio​()
        Returns whether the media aspect ratio is preserved when scaling.
        Returns:
        whether the media aspect ratio is preserved.
      • preserveRatioProperty

        public final BooleanProperty preserveRatioProperty​()
        Whether to preserve the aspect ratio (width / height) of the media when scaling it to fit the node. If the aspect ratio is not preserved, the media will be stretched or sheared in both dimensions to fit the dimensions of the node. The default value is true.
        See Also:
        isPreserveRatio(), setPreserveRatio(boolean)
      • setSmooth

        public final void setSmooth​(boolean value)
        Sets whether to smooth the media when scaling.
        Parameters:
        value - whether to smooth the media.
      • isSmooth

        public final boolean isSmooth​()
        Returns whether to smooth the media when scaling.
        Returns:
        whether to smooth the media
      • smoothProperty

        public final BooleanProperty smoothProperty​()
        If set to true a better quality filtering algorithm will be used when scaling this video to fit within the bounding box provided by fitWidth and fitHeight or when transforming. If set to false a faster but lesser quality filtering will be used. The default value depends on platform configuration.
        See Also:
        isSmooth(), setSmooth(boolean)
      • setX

        public final void setX​(double value)
        Sets the x coordinate of the MediaView origin.
        Parameters:
        value - the x coordinate of the origin of the view.
      • getX

        public final double getX​()
        Retrieves the x coordinate of the MediaView origin.
        Returns:
        the x coordinate of the origin of the view.
      • setY

        public final void setY​(double value)
        Sets the y coordinate of the MediaView origin.
        Parameters:
        value - the y coordinate of the origin of the view.
      • getY

        public final double getY​()
        Retrieves the y coordinate of the MediaView origin.
        Returns:
        the y coordinate of the origin of the view.
      • setFitWidth

        public final void setFitWidth​(double value)
        Sets the width of the bounding box of the resized media.
        Parameters:
        value - the width of the resized media.
      • getFitWidth

        public final double getFitWidth​()
        Retrieves the width of the bounding box of the resized media.
        Returns:
        the height of the resized media.
      • fitWidthProperty

        public final DoubleProperty fitWidthProperty​()
        Determines the width of the bounding box within which the source media is resized as necessary to fit. If value ≤ 0, then the width of the bounding box will be set to the natural width of the media, but fitWidth will be set to the supplied parameter, even if non-positive.

        See preserveRatio for information on interaction between media views fitWidth, fitHeight and preserveRatio attributes.

        See Also:
        getFitWidth(), setFitWidth(double)
      • setFitHeight

        public final void setFitHeight​(double value)
        Sets the height of the bounding box of the resized media.
        Parameters:
        value - the height of the resized media.
      • getFitHeight

        public final double getFitHeight​()
        Retrieves the height of the bounding box of the resized media.
        Returns:
        the height of the resized media.
      • fitHeightProperty

        public final DoubleProperty fitHeightProperty​()
        Determines the height of the bounding box within which the source media is resized as necessary to fit. If value ≤ 0, then the height of the bounding box will be set to the natural height of the media, but fitHeight will be set to the supplied parameter, even if non-positive.

        See preserveRatio for information on interaction between media views fitWidth, fitHeight and preserveRatio attributes.

        See Also:
        getFitHeight(), setFitHeight(double)
      • setViewport

        public final void setViewport​(Rectangle2D value)
        Sets the rectangular viewport into the media frame.
        Parameters:
        value - the rectangular viewport.
      • getViewport

        public final Rectangle2D getViewport​()
        Retrieves the rectangular viewport into the media frame.
        Returns:
        the rectangular viewport.
      • viewportProperty

        public final ObjectProperty<Rectangle2D> viewportProperty​()
        Specifies a rectangular viewport into the media frame. The viewport is a rectangle specified in the coordinates of the media frame. The resulting bounds prior to scaling will be the size of the viewport. The displayed image will include the intersection of the frame and the viewport. The viewport can exceed the size of the frame, but only the intersection will be displayed. Setting viewport to null will clear the viewport.
        See Also:
        getViewport(), setViewport(Rectangle2D)