0% found this document useful (0 votes)
29 views

Multimedia Dalam Java

The document discusses implementing multimedia objects in JavaFX applications. It provides steps to create a JavaFX project, add multimedia playback functionality, and build a basic video player application with controls for selecting, playing, pausing, and stopping videos. The application uses MediaPlayer to load video files and control playback, and MediaView to display the video.

Uploaded by

sayalatief2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Multimedia Dalam Java

The document discusses implementing multimedia objects in JavaFX applications. It provides steps to create a JavaFX project, add multimedia playback functionality, and build a basic video player application with controls for selecting, playing, pausing, and stopping videos. The application uses MediaPlayer to load video files and control playback, and MediaView to display the video.

Uploaded by

sayalatief2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Menerapkan objek multimedia dalam aplikasi dengan JavaFX

1. Buka Netbeans, Pilih File  New Project  JavaFX JavaFX Application

2. Kemudian masukkan Project Name (nama project terserah kalian), kemudian pilih lokasi
penyimpanannya, kemudian uncheck pada bagian Create Application Class  Finish

3. Setelah project dibuat, kemudian pilih project  pada bagian Souce Packages , klik kanan 
New  Java Package. Disini nama package saya buat jfx.videoplayer
4. Setelah bikin package, kita buat Class baru (nama class bebas), disini saya beri nama classnya
VideoPlayerEx
Klik kanan package yang sudah dibuat  New  Java Class

5. Kemudian dilanjutkan dengan menambahkan coding


package jfx.videoplayer;

import java.io.File;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;

public class VideoPlayerEx extends Application{


File file;
final Label lblFile = new Label();
Button pilih = new Button();
Button play = new Button();
Button pause = new Button();
Button stop = new Button();

final VBox vb = new VBox();


final Slider s = new Slider();

HBox hb = new HBox();

MediaPlayer mp;
MediaView mv;

@Override
public void start(final Stage primaryStage) throws Exception {
final StackPane root = new StackPane();
primaryStage.setTitle("VideoPlayerEx");

pilih.setText("Pilih File");
play.setText("Play");
pause.setText("Pause");
stop.setText("Stop");
Media m = new Media(fileChooser());
mp = new MediaPlayer(m);
mv = new MediaView(mp);
mp.play();

pilih.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {

mp.dispose();
mp = new MediaPlayer(new Media(fileChooser()));
mv.setMediaPlayer(mp);
mp.play();

mp.setOnReady(new Runnable() {

@Override
public void run() {
onReady(primaryStage);
}
});

mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {

@Override
public void changed(ObservableValue<? extends Duration> ov, Duration t, Duration t1) {
s.setValue(t1.toSeconds());
}
});

}
});

mp.setOnReady(new Runnable() {

@Override
public void run() {
onReady(primaryStage);
}
});

mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> ov, Duration t, Duration t1) {
s.setValue(t1.toSeconds());
}
});

play.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent t) {
mp.play();
}
});

pause.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent t) {
mp.pause();
}
});

stop.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent t) {
mp.stop();
}
});

s.setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override
public void handle(MouseEvent t) {
mp.seek(Duration.seconds(s.getValue()));
}
});

s.setMin(0.0);
s.setValue(0.0);
s.setMax(mp.getTotalDuration().toSeconds());

hb.getChildren().add(pilih);
hb.getChildren().add(play);
hb.getChildren().add(pause);
hb.getChildren().add(stop);

vb.getChildren().add(s);
vb.getChildren().add(lblFile);
vb.getChildren().add(hb);

root.getChildren().add(mv);
root.getChildren().add(vb);

primaryStage.setScene(new Scene(root, 600, 480));


primaryStage.show();
}

private void onReady(Stage stage) {


int w = mp.getMedia().getWidth();
int h = mp.getMedia().getHeight();

stage.setMinWidth(w);
stage.setMinHeight(h);

vb.setMinSize(w-50, 500);
vb.setTranslateY(h+30);
s.setMin(0.0);
s.setValue(0.0);
s.setMax(mp.getTotalDuration().toSeconds());
}

private String fileChooser() {


FileChooser fileChooser = new FileChooser();

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("MP4 Files (*.mp4)",


"*.MP4");
fileChooser.getExtensionFilters().add(extFilter);

file = fileChooser.showOpenDialog(null);

String path="file://" +file.toURI().getPath().replaceAll("","%20");


lblFile.setText(path);

return path;

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

6. Ketika dijalankan akan muncul output seperti di bawah ini. Untuk memilih file MP4  Klik
Open

7. Output
Multimedia pada Program Java (Pemutar Musik)

1. Buatlah project  Java  Java Application

2. Buatlah package dan class

3. Tuliskan coding di bawah ini


package Multimedia;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CobaMultimedia extends Applet implements ActionListener {

// GUI Widgets
Button play, stop, loop;

AudioClip mySong;

public void init() {

// Make Gui
play=new Button("Play");
add(play);
play.addActionListener(this);

stop=new Button("Stop");
add(stop);
stop.addActionListener(this);

loop=new Button("Loop");
add(loop);
loop.addActionListener(this);

// Load the audio file


mySong = getAudioClip(getCodeBase(), "irRoom.wav");

} // init

public void actionPerformed(ActionEvent e) {

if (e.getSource() == play)
mySong.play();
if (e.getSource() == stop)
mySong.stop();
if (e.getSource() == loop)
mySong.loop();

} // actionPerformed

} // Player

4. Output program

You might also like