【问题标题】:how to play video using java?如何使用java播放视频?
【发布时间】:2016-10-17 12:31:24
【问题描述】:

我想使用 java 在媒体播放器中播放视频,我使用 JMF 显示视频,但是它在我使用的 JMF 库中的一种数据类型中出现错误,这是我的代码。

public static void main(String args[]) {
    // create a file chooser
    JFileChooser fileChooser = new JFileChooser();

    // show open file dialog
    int result = fileChooser.showOpenDialog(null);

    if (result == JFileChooser.APPROVE_OPTION) // user chose a file
    {
        URL mediaURL = null;

        try {
            // get the file as URL
            mediaURL = fileChooser.getSelectedFile().toURL();
        } // end try
        catch (MalformedURLException malformedURLException) {
            System.err.println("Could not create URL for the file");
        } // end catch

        if (mediaURL != null) // only display if there is a valid URL
        {
            JFrame mediaTest = new JFrame("Media Tester");
            mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            MediaPlayer mediaPanel = new MediaPlayer(mediaURL);

            mediaTest.add(mediaPanel);

            mediaTest.setSize(300, 300);
            mediaTest.setVisible(true);
        } // end inner if
    } // end outer if
}

问题是它在这行给我一个错误

MediaPlayer mediaPanel = new MediaPlayer(mediaURL);

构造函数 MediaPlayer(URL) 未定义,因为 MediaPlayer 构造函数不接受任何内容,但我需要将 url 添加到 mediaplayer 以在 jframe 中显示它,但我不能,有什么帮助吗?

【问题讨论】:

标签: java media-player media jmf


【解决方案1】:

您需要使用setMediaLocation(location) 指定要启动的媒体的位置,因此请尝试以下操作:

MediaPlayer mediaPanel = new MediaPlayer();
mediaPanel.setMediaLocation(mediaURL.toString());

您也可以将setMediaLocator(locator) 用于相同目的,但如果您有URL 会更方便一点,代码如下:

MediaPlayer mediaPanel = new MediaPlayer();
mediaPanel.setMediaLocator(new MediaLocator(mediaURL));

【讨论】:

  • 媒体定位器解决了错误,但是当我运行它时,它什么也没给我 jframe 成功打开但里面没有视频,,,你能帮忙吗?
  • JMF 现在已经过时了,你不应该过度依赖它,你应该使用 Java FX 的播放器。对于JMF,我能做的最好的就是提供初始教程deitel.com/articles/java_tutorials/20060422/PlayingVideowithJMF
  • here下载源代码,尤其是MediaPanel
【解决方案2】:

如果你只是想播放一个 mp4 文件,那就太简单了

Desktop.getDesktop().open(new File("test.mp4"));

但是,如果你想专门在 JFrame 中播放它 然后请记住,JMF 不支持 mp4 格式。 顺便请分享您的错误信息。

【讨论】:

    【解决方案3】:

    我们还可以将 ProcessBuilder() 用于任何项目或视频......

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class MainClass extends JPanel{
        public MainClass(){
            JButton btn1 = new JButton("Button1");
            btn1.setBounds(70, 50, 260, 40);
            JTextField txtProName = new JTextField();
            txtProName.setBounds(100, 50, 200, 40);
            add(btn1);
            add(txtProName);
        }
        public static void main(String[]args){
            JFrame frm = new JFrame();
            frm.getContentPane().add(new MainClass());
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setSize(200, 300);
            frm.setVisible(true);
        }
    }
    class bListener implements ActionListener{
        bListener(){
            public void actionPerformed(ActionEvent e){
                ProcessBuilder p = new ProcessBuilder(txtProName.getText());
                try{
                    p.start();
                }catch(IOException ex){
                    Logger.getLogger(frm.class.getName()).log(level.SERVERE, ex.getMessage(), ex);
                }
            }
        }
    }
    

    我们也可以将 JFileChooser() 添加到这个项目中,以便在文件之间选择一个来运行它..

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 2011-07-13
      • 1970-01-01
      • 2019-08-13
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多