Membuat Aplikasi Video Conference dengan JMF bagian 2: Player
Di bagian 1 kita sudah membuat menu, sekarang kita akan membuat Player untuk memainkan media yang dipilih dari Menu File –> Open
Kita membuat satu class yang bertugas untuk memainkan file. Class ini adalah class MediaPlayer. Berikut adalah source code nya:
import javax.media.*;
import javax.media.Manager;
import javax.media.Player;
import java.net.URL;
import javax.swing.JPanel;
import java.awt.*;
import java.io.IOException;
public class MediaPlayer extends JPanel {
public MediaPlayer(URL mediaURL) {
setLayout( new BorderLayout() );
try {
//Create a player to play the media specified in the URL
Player thePlayer = Manager.createRealizedPlayer(mediaURL);
Component video = thePlayer.getVisualComponent(); //Add visual component
Component controls = thePlayer.getControlPanelComponent(); //Add audio component
this.add(video, BorderLayout.CENTER);
this.add(controls, BorderLayout.SOUTH);
thePlayer.start();
}
catch (NoPlayerException noPlayerException){
System.err.println( “No media player found” + noPlayerException );
}
catch (CannotRealizeException cannotRealizeException){
System.err.println( “Could not realize media player” + cannotRealizeException);
}
catch (IOException iOException){
System.err.println( “Error reading from the source” +iOException);
}
}
}
Kemudian, kita edit JConference kita.
Masukkan method openPlayFile() pada action menu File–>Open
Ganti
System.out.println(“Sementara”);
Dengan
openPlayFile();
Kemudian tambahkan method openPlayFile();
void openPlayFile() {
URL theURL = null;
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try
{
theURL = fileChooser.getSelectedFile().toURL();
}
catch ( MalformedURLException malformedURLException )
{
System.err.println( “Could not create URL for the file” + malformedURLException);
}
}
if (theURL != null)
{
MediaPlayer thePlayer = new MediaPlayer(theURL);
this.add(thePlayer, BorderLayout.CENTER);
this.setVisible(true);
}
}
Sekarang kita sudah membuat player untuk aplikasi kita. Post berikutnya akan menambahkan fasilitas capture untuk menampilkan dan menyimpan video dari web cam.