我正在使用以下代码在JFrame上播放视频。 Java代码可以很好地在JFrame上播放视频,但问题是它播放视频的速度太快(可能是2倍的速度)。
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);
}
}
}
}
使用xuggle xuggler时是否可以正常速度播放视频?
最佳答案
仅查看我最近在Xuggle上看到的内容,看来您可能缺少睡眠功能。帧速率应通过休眠来控制:
Thread.sleep((long) (1000 / FRAME_RATE)); //where FRAME_RATE is your desired FPS
话虽这么说,但我对您在这里使用的方法不太熟悉,但是如果可以猜测的话,似乎可能需要将其添加到空的while循环中。