Xuggle Xuggler在JFrame上播放视频的速度太快了

时间:2015-12-06 13:02:25

标签: java video xuggler xuggle

我正在使用以下代码在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时,是否可以以正常速度播放视频?

2 个答案:

答案 0 :(得分:0)

看看我最近看到的Xuggle,你可能会错过睡眠功能。帧率应该通过睡眠来控制:

public async Task<ApiResponse<ProductLookupDto>> GetProductsByValueOfTheDay()
{
    string url = "http://domain.com?param=10120&isBasic=true";
    var result = await url.GetJsonAsync<ProductLookupDto>();
    return new ApiResponse<ProductLookupDto>()
    {
        Data = result,
        Message = "success"
    };
}

话虽这么说,我对你在这里使用的方法并不熟悉,但是如果我猜的话,似乎可能需要将这个添加到你的空循环中。

答案 1 :(得分:0)

方法readPacket()读取音频和视频帧。我认为您必须使用Thread.sleep

FPS = (Frames video + frames audio) /video time进行计算