Video and Audio

Video and Audio

You can use the Media class to acquire the supply of the media, the MediaParticipant class to play and management the media, and the MediaView class to show the video. Media (video and audio) is important in creating wealthy Internet functions. JavaFX supplies the Media, MediaParticipant, and MediaView lessons for working with media. Currently, JavaFX helps MP3, AIFF, WAV, and MPEG-4 audio codecs and FLV and MPEG-4 video codecs. The Media class represents a media supply with properties length, width, and peak, as proven in Figure under. You can assemble a Media object from an Internet URL string.

Image description

The MediaParticipant class performs and controls the media with properties akin to autoPlay, currentCount, cycleCount, mute, quantity, and wholeDuration, as proven in Figure under. You can assemble a MediaParticipant object from a media and use the pause() and play() technique to pause and resume enjoying.

Image description

The MediaView class is a subclass of Node that gives a view of the Media being performed by a MediaParticipant. The MediaView class supplies the properties for viewing the media, as proven in Figure under.

Image description

The code under provides an instance that shows a video in a view, as proven in Figure under. You can use the play/pause button to play or pause the video and use the rewind button to restart the video, and use the slider to regulate the amount of the audio.

Image description

package deal software;
import javafx.software.Application;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.management.Button;
import javafx.scene.management.Label;
import javafx.scene.management.Slider;
import javafx.scene.format.BorderPane;
import javafx.scene.format.HBox;
import javafx.scene.format.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaParticipant;
import javafx.scene.media.MediaView;
import javafx.util.Duration;

public class MediaDemo extends Application {
    personal static ultimate String MEDIA_URL = "http://cs.armstrong.edu/liang/frequent/pattern.mp4";

    @Override // Override the beginning technique within the Application class
    public void begin(Stage majorStage) {
        Media media = new Media(MEDIA_URL);
        MediaParticipant mediaPlayer = new MediaParticipant(media);
        MediaView mediaView = new MediaView(mediaPlayer);

        Button playButton = new Button(">");
        playButton.setOnAction(e -> {
            if(playButton.getText().equals(">"))  else {
                mediaPlayer.pause();
                playButton.setText(">");
            }
        });

        Button rewindButton = new Button("<<");
        rewindButton.setOnAction(e -> mediaPlayer.search(Duration.ZERO));

        Slider slVolume = new Slider();
        slVolume.setPrefWidth(150);
        slVolume.setMaxWidth(Region.USE_PREF_SIZE);
        slVolume.setMinWidth(30);
        slVolume.setValue(50);
        mediaPlayer.volumeProperty().bind(slVolume.valueProperty().divide(100));

        HBox hBox = new HBox(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(playButton, rewindButton, new Label("Volume"), slVolume);

        BorderPane pane = new BorderPane();
        pane.setCenter(mediaView);
        pane.setBottom(hBox);

        // Create a scene and place it within the stage
        Scene scene = new Scene(pane, 650, 500);
        majorStage.setTitle("MediaDemo"); // Set the stage title
        majorStage.setScene(scene); // Place the scene within the stage
        majorStage.present(); // Display the stage
    }

    public static void important(String[] args) {
        Application.launch(args);
    }
}

The supply of the media is a URL string outlined in strains 18. The program creates a Media object from this URL (line 22), a MediaParticipant from the Media object (line 23), and a MediaView from the MediaParticipant object (line 24). The relationship amongst these three objects is proven in Figure under.

Image description

A Media object helps stay streaming. You can now obtain a big media file and play it in the identical time. A Media object might be shared by a number of media gamers and totally different views can use the identical MediaParticipant object.

A play button is created (line 26) to play/pause the media (line 29). The button’s textual content is modified to || (line 30) if the button’s present textual content is > (line 28). If the button’s present textual content is ||, it’s modified to > (line 33) and the participant is paused (line 32).

A rewind button is created (line 37) to reset the playback time to the start of the media stream by invoking search(Duration.ZERO) (line 38).

A slider is created (line 40) to set the amount. The media participant’s quantity property is certain to the slider (strains 45).

The buttons and slider are positioned in an HBox (strains 47–49) and the media view is positioned within the middle of the border pane (line 52) and the HBox is positioned on the backside of the border pane (line 53).

HI-FI News

by way of DEV Community https://dev.to

June 27, 2024 at 10:59PM