Swing and JMF Media Player

  • Thread starter Christian Otteneuer
  • Start date
C

Christian Otteneuer

Hallo NG,

I am not sure if I am right in these newsgroup. I have the following
problem: I want to add the MediaPlayer from JMF to my Swing GUI.
Therefor, I have written two simple classes:

// My SwingApplication:
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.*;

public class SwingApplication {

public SwingApplication() {
Frame mf = new Frame();
MediaPlayerObject m = new MediaPlayerObject();
JPanel panel = new JPanel();
panel.add(m);
mf.add(panel);
mf.setSize(new Dimension(1024,768));
mf.setVisible(true);
}

public static void main (String[] args) {
SwingApplication sw = new SwingApplication();
}
}

// My MediaPlayerObject
import javax.media.bean.playerbean.MediaPlayer;
import javax.swing.JPanel;

public class MediaPlayerObject extends JPanel {
public MediaPlayer player;
String videofile = "d:/myvideofile.mpg";

MediaPlayerObject() {
if (player != null)
player.close ();
player = new MediaPlayer();
player.setMediaLocation("file:///" + videofile);
player.setPlaybackLoop(false);
//player.addControllerListener (this);
player.prefetch ();
this.add(player);
player.start ();
}
}

The MediaPlayerObject starts playing my video and it is added to my
JPanel in the SwingApplication class.

Now the problem: When I start class, I can hear the video. But I have to
change the size of the whole frame first until I can also see it. What
can I do that I can see it immediately?

Hope, someone can help me?

Cheers,
Chris
 
P

Philipp Leitner

For GUI problems it maz be better to post to comp.lang.java.gui instead
of programmer, but I think repainting (i.e. calling mf.repaint() ) may
help you.

/philipp
 
Joined
Jun 27, 2008
Messages
2
Reaction score
0
Possible Incorrect Implementation

I need to include JMF in my project and i was facing problems playing my video files. This is because all my video files were included in the same JAR archive. Hence, i need to place the video files outside of my JAR file. However, i face no problem using a compiler (which is lame). i really need to create a JAR for my project to run.

Came across this forum and found your implementation. It's cool as i get to use .setMediaLocation() which leads my program directly to the video files.

This is the code i implemented and it works fine...repainting doesn't solve the issue though.
import javax.swing.JPanel;
import javax.media.bean.playerbean.MediaPlayer;
import java.awt.BorderLayout;
import java.awt.Color;

public class MediaPanel extends JPanel
{
MediaPlayer mediaPlayer;

public MediaPanel( String mediaURL )
{ setLayout(new BorderLayout());
setBackground(Color.BLACK);

mediaPlayer = new MediaPlayer();

mediaPlayer.setMediaLocation(mediaURL);

mediaPlayer.setPlaybackLoop(false);
add(mediaPlayer, BorderLayout.CENTER);
mediaPlayer.prefetch();
}
}

This code is not perfect, but it sure solves your previous problem.
 
Joined
Jul 24, 2008
Messages
1
Reaction score
0
i'm having huge truble implementing a simple video player. basically i have this interface:

package projfinal;

/**
*
* @author balazar
*/

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.media.*;

public class userWindow extends javax.swing.JFrame {
(...)
private void videoMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MoviePlayer player;
JFileChooser chooser = new JFileChooser();

int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
try{
player = new MoviePlayer(chooser.getSelectedFile().toURI().toURL());
vidPanel.add(player);
vidPanel.repaint();
}catch(Exception e){
System.out.println(e);
}
}

}

(...)
}

it is an incomplete application that will allow users to watch a video and record subtitles (btnA, btnB, ....) in each time instance.

my problem is to add the video player in vidPanel. i actually get the sound but the image does not appear.

package projfinal;

/**
*
* @author Administrador
*/

import javax.media.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class MoviePlayer extends JPanel{
Player player;

MoviePlayer(URL fileURL){

try{
player = Manager.createRealizedPlayer(new MediaLocator(fileURL));
player.start();
}catch(Exception e){
System.out.println(e);
}
}
}


this is my simple video class. it is probably incomplete but i just want to get the video thing working.

can anyone help me?

cumps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top