gpt4 book ai didi

java - 如何使用java播放视频?

转载 作者:行者123 更新时间:2023-12-02 03:17:25 24 4
gpt4 key购买 nike

我想使用 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 添加到媒体播放器以将其显示在 jframe 中,但我不能,有什么帮助吗?

最佳答案

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

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

或者您可以使用setMediaLocator(locator)出于相同的目的,但如果您有 URL,则更方便一点,代码将是:

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

关于java - 如何使用java播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40086463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com