java:求一段代码,很简单,运行后直接播放一个小视频,播放完后(或者自行定时)立刻自动关闭。
发布网友
发布时间:2022-04-23 14:06
我来回答
共1个回答
热心网友
时间:2023-10-17 10:24
下载JMF(java media framework)
下面是一个例子
import java.awt.BorderLayout;
import java.awt.Component;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class MediaPlayer extends javax.swing.JPanel {
public MediaPlayer(URL mediauUrl) {
initComponents();
setLayout(new BorderLayout());
try {
Player mediaPlayer = Manager.createRealizedPlayer(new MediaLocator(mediauUrl));
Component video = mediaPlayer.getVisualComponent();
Component control = mediaPlayer.getControlPanelComponent();
if (video != null) {
add(video, BorderLayout.CENTER); // place the video component in the panel
}
add(control, BorderLayout.SOUTH); // place the control in panel
mediaPlayer.start();
} catch (Exception e) {
}
}
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getTopLevelAncestor());
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 720, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 480, Short.MAX_VALUE)
);
}
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
URL mediaUrl = null;
try {
mediaUrl = fileChooser.getSelectedFile().toURI().toURL();
} catch (MalformedURLException ex) {
System.out.println(ex);
}
JFrame mediaTest = new JFrame("Movie Player");
mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MediaPlayer mediaPanel = new MediaPlayer(mediaUrl);
mediaTest.add(mediaPanel);
mediaTest.setSize(800, 700); // set the size of the player
mediaTest.setLocationRelativeTo(null);
mediaTest.setVisible(true);
}
}
热心网友
时间:2023-10-17 10:24
下载JMF(java media framework)
下面是一个例子
import java.awt.BorderLayout;
import java.awt.Component;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class MediaPlayer extends javax.swing.JPanel {
public MediaPlayer(URL mediauUrl) {
initComponents();
setLayout(new BorderLayout());
try {
Player mediaPlayer = Manager.createRealizedPlayer(new MediaLocator(mediauUrl));
Component video = mediaPlayer.getVisualComponent();
Component control = mediaPlayer.getControlPanelComponent();
if (video != null) {
add(video, BorderLayout.CENTER); // place the video component in the panel
}
add(control, BorderLayout.SOUTH); // place the control in panel
mediaPlayer.start();
} catch (Exception e) {
}
}
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getTopLevelAncestor());
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 720, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 480, Short.MAX_VALUE)
);
}
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
URL mediaUrl = null;
try {
mediaUrl = fileChooser.getSelectedFile().toURI().toURL();
} catch (MalformedURLException ex) {
System.out.println(ex);
}
JFrame mediaTest = new JFrame("Movie Player");
mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MediaPlayer mediaPanel = new MediaPlayer(mediaUrl);
mediaTest.add(mediaPanel);
mediaTest.setSize(800, 700); // set the size of the player
mediaTest.setLocationRelativeTo(null);
mediaTest.setVisible(true);
}
}