Javax Media Player-the Play
试图将.mp4视频嵌入jpanel。 jpanel将加载和执行,但视频不会。尝试{}会引发错误,但不确定为什么尝试{}会抛出错误。从另一个来源复制此代码,但不会编译。
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel
{
public static void main(String[] args)
{
URL mediaUrl=null;
File file = new File("Mortal Combat Video.mp4");
System.out.println(file);
try
{
mediaUrl = file.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 );
System.out.println(mediaPanel);
mediaTest.add( mediaPanel );
mediaTest.setSize( 800, 700 ); // set the size of the player
mediaTest.setLocationRelativeTo(null);
mediaTest.setVisible( true );
}
}
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MediaPlayer extends JPanel {
public MediaPlayer(URL mediauUrl) {
setLayout(new BorderLayout());
Player mediaPlayer = null;
Component video = null;
Component control = null;
try
{
mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl));
video=mediaPlayer.getVisualComponent();
control=mediaPlayer.getControlPanelComponent();
System.out.println("here3");
if (video == null)
System.out.println("video");
else
{
System.out.println("video2");
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)
{
System.out.println("error");
}
}
}
捕获{}打印“错误”,但不确定为什么尝试{}代码不会编译。任何建议将不胜感激。
Trying to embed a .mp4 video in a JPanel. JPanel will load and execute but video does not. The try{} throws an error but not sure why the try{} is throwing the error. Copied this code from another source but it will not compile.
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel
{
public static void main(String[] args)
{
URL mediaUrl=null;
File file = new File("Mortal Combat Video.mp4");
System.out.println(file);
try
{
mediaUrl = file.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 );
System.out.println(mediaPanel);
mediaTest.add( mediaPanel );
mediaTest.setSize( 800, 700 ); // set the size of the player
mediaTest.setLocationRelativeTo(null);
mediaTest.setVisible( true );
}
}
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MediaPlayer extends JPanel {
public MediaPlayer(URL mediauUrl) {
setLayout(new BorderLayout());
Player mediaPlayer = null;
Component video = null;
Component control = null;
try
{
mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl));
video=mediaPlayer.getVisualComponent();
control=mediaPlayer.getControlPanelComponent();
System.out.println("here3");
if (video == null)
System.out.println("video");
else
{
System.out.println("video2");
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)
{
System.out.println("error");
}
}
}
The catch{} prints "error" but not sure why the try{} code will not compile. Any advice is appreciated.
Its running now, video plays, controls work but the video itself is not visible (audio plays).
Here's screenshot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论