0

I am a beginner in java programming. I am going to play a video in java but get stuck. First I tried vlcj, but it is only for 32-bit JDK(is it right?) and now I am trying JMF using mpg file. The problem is that I could only hear the sound of the file, could not see the video. It shows: Unable to handle format: MPEG, 640x360, FrameRate=29.9, Length=345600 The code I am using is:

    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());    
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) {    
}    
}
public static void main(String[] args) {

URL mediaUrl=null;    
File file = new File("src/media/Jellyfish.mpg");     

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 );    
mediaTest.add( mediaPanel );

mediaTest.setSize( 800, 700 ); // set the size of the player    
mediaTest.setLocationRelativeTo(null);    
mediaTest.setVisible( true );
}    
}

Can somebody help? Thanks in advance

ArK
  • 20,698
  • 67
  • 109
  • 136
Yohan Gao
  • 1
  • 1
  • 1
  • 2
    if you are a beginner, it's better to first decently learn the language. also: don't use jmf, it hasn't been supported for years, and there are better alternatives around – Stultuske Jul 06 '16 at 10:29
  • maybe this can help: http://stackoverflow.com/questions/12802134/i-can-not-start-the-player-in-my-code – Stultuske Jul 06 '16 at 10:30
  • Thank you very much. Yeah, JMF is really bad and I am just trying to insert a video in java. Did not expect that there so many problems and difficulties :) – Yohan Gao Jul 06 '16 at 10:46
  • *"just trying to insert a video in java"* Ther is no 'just' about embedding a video in Java. It cannot be done within the core API in Swing or AWT, so requires libraries like VLCJ (BTW - follow [this link](https://www.google.com.au/search?q=VLCJ+64+bit) for evidence of a 64 bit version) or JMF (which as mentioned by @Stultuske, is obsolete). Two other options. Open the video in a separate window provided by Java-FX, using the [`MediaPlayer`](https://docs.oracle.com/javafx/2/api/javafx/scene/media/MediaPlayer.html) or (very crude) `Desktop.getDesktop().open(theVideoFile.mpg);` to open it in .. – Andrew Thompson Jul 06 '16 at 12:15
  • .. whatever media player installed on the system that claims that file type. But there are many things we need to know before we can advise the best route. Does your app. need to know when the video has finished playing? Does your app. need to *control* the video (e.g. start/stop/fast forward..)? Does the video need to be embedded in one of the windows of your Swing app., or is it OK to have it open in a new window (& possibly in a different app. entirely)? But that brings me back to - handling media is always complicated, so probably better to stick to easier things first - for a while yet. – Andrew Thompson Jul 06 '16 at 12:19
  • Thanks Andrew, i tried JavaFX and it is working. – Yohan Gao Jul 06 '16 at 19:47

0 Answers0