Reputation: 169
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");
}
}
}
Its running now, video plays, controls work but the video itself is not visible (audio plays).
Here's screenshot:
Upvotes: 0
Views: 216