Media Test Class
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class MediaTest {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
URL mediaURL = null;
try {
mediaURL = fileChooser.getSelectedFile().toURI().toURL();
} catch (MalformedURLException e) {
System.err.println("Could not create URL for the file!..");
}
if (mediaURL != null) {
JFrame mediaTest = new JFrame("Media Tester"); mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MediaPanel mediaPanel = new MediaPanel(mediaURL);
mediaTest.add(mediaPanel);
mediaTest.setSize(300, 300);
mediaTest.setVisible(true);
}
}
}
}
Media Panel Class
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JPanel;
public class MediaPanel extends JPanel {
MediaPanel(URL mediaURL) {
setLayout(new BorderLayout());
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
try {
Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
if (video != null) {
add(video, BorderLayout.CENTER);
}
if (controls != null) {
add(controls, BorderLayout.SOUTH);
}
mediaPlayer.start();
} catch (NoPlayerException e) {
System.err.println(e);
} catch (CannotRealizeException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
}
}
No comments:
Post a Comment