I'm using following code to play video on JFrame. Java code plays the video on JFrame fine, but the problem is that it is playing the video too fast (maybe 2x speed).
package swingplayvideo;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaListenerAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IVideoPictureEvent;
public class SwingPlayVideo
{
public static void main(String[] args)
{
String sourceUrl = "C:\\MVI_4896.MOV";
SwingPlayVideo videoPlayer = new SwingPlayVideo();
videoPlayer.play(sourceUrl);
}
public void play(String sourceUrl)
{
IMediaReader reader = ToolFactory.makeReader(sourceUrl);
reader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
final MyVideoFrame frame = new MyVideoFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
MediaListenerAdapter adapter = new MediaListenerAdapter()
{
@Override
public void onVideoPicture(IVideoPictureEvent event)
{
frame.setImage((BufferedImage) event.getImage());
}
};
reader.addListener(adapter);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
while (reader.readPacket() == null)
do {} while(false);
}
private class MyVideoFrame extends JFrame
{
Image image;
public void setImage(final Image image)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
MyVideoFrame.this.image = image;
repaint();
}
});
}
@Override
public synchronized void paint(Graphics g)
{
if (image != null)
{
g.drawImage(image, 0, 0, null);
}
}
}
}
Is it even possible to play the video at normal speed when using xuggle xuggler?
Just looking at what I've seen recently with Xuggle, it appears you may be missing a sleep function. The framerate should be controlled by sleeping:
That being said, I'm not as familiar with your method you are using here, but it seems like this may need to be added to your empty while loop, if I could guess.