我在使用vlcj播放youtube视频时遇到问题。直到上周左右一切正常。 我正在调用mediaPlayer.playMedia(“http://www.youtube.com/watch?v=1t8fl96HPQI”)并且工作正常但不再适用。它根本不会产生任何错误。 请注意,我可以播放本地文件和其他在线流媒体网址,但不能播放YouTube视频。
这是我正在使用的代码。 (基本上是一个现成的例子)
/*
* This file is part of VLCJ.
*
* VLCJ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VLCJ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VLCJ. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2009, 2010, 2011 Caprica Software Limited.
*/
package com.javacodegeeks.youtube.test;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;
import uk.co.caprica.vlcj.binding.internal.libvlc_media_t;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.test.VlcjTest;
/**
* A minimal YouTube player.
* <p>
* The URL/MRL must be in the following format:
* <pre>
* http://www.youtube.com/watch?v=000000
* </pre>
* The only thing that makes this different from a 'regular' media player
* application is the following piece of code:
* <pre>
* mediaPlayer.setPlaySubItems(true); // <--- This is very important for YouTube media
* </pre>
* Note that it is also possible to programmatically play the sub-item in
* response to events - this is slightly more complex but more flexible.
* <p>
* The YouTube web page format changes from time to time. This means that the
* lua scripts that vlc provides to parse the YouTube web pages when looking
* for the media to stream may not work. If you get errors, especially errors
* alluding to malformed urls, then you may need to update your vlc version to
* get newer lua scripts.
*/
public class NewClass extends VlcjTest {
private MediaPlayerFactory factory;
private EmbeddedMediaPlayer mediaPlayer;
private Frame mainFrame;
private JLabel urlLabel;
private JTextField urlTextField;
private JButton playButton;
public static void main(String[] args) throws Exception {
setLookAndFeel();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new NewClass().start();
}
});
}
public NewClass() {
mainFrame = new Frame("vlcj YouTube Test");
mainFrame.setIconImage(new ImageIcon(getClass().getResource("/icons/vlcj-logo.png")).getImage());
mainFrame.setSize(800, 600);
mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exit(0);
}
});
mainFrame.setLayout(new BorderLayout());
JPanel cp = new JPanel();
cp.setBackground(Color.black);
cp.setLayout(new BorderLayout());
JPanel ip = new JPanel();
ip.setBorder(new EmptyBorder(4, 4, 4, 4));
ip.setLayout(new BoxLayout(ip, BoxLayout.X_AXIS));
urlLabel = new JLabel("URL:");
urlLabel.setDisplayedMnemonic('u');
urlLabel.setToolTipText("Enter a URL in the format http://www.youtube.com/watch?v=000000");
urlTextField = new JTextField(40);
urlTextField.setFocusAccelerator('u');
urlTextField.setToolTipText("Enter a URL in the format http://www.youtube.com/watch?v=000000");
playButton = new JButton("Play");
playButton.setMnemonic('p');
ip.add(urlLabel);
ip.add(urlTextField);
ip.add(playButton);
cp.add(ip, BorderLayout.NORTH);
Canvas vs = new Canvas();
vs.setBackground(Color.black);
cp.add(vs, BorderLayout.CENTER);
mainFrame.add(cp, BorderLayout.CENTER);
urlTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play();
}
});
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play();
}
});
factory = new MediaPlayerFactory();
mediaPlayer = factory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(factory.newVideoSurface(vs));
mediaPlayer.setPlaySubItems(true); // <--- This is very important for YouTube media
mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem) {
List<String> items = mediaPlayer.subItems();
System.out.println(items);
System.out.println(mediaPlayer.subItemCount());
}
});
}
private void start() {
mainFrame.setVisible(true);
}
private void play() {
String mrl = urlTextField.getText();
mediaPlayer.playMedia(mrl);
}
private void exit(int value) {
mediaPlayer.stop();
mediaPlayer.release();
factory.release();
System.exit(value);
}
/**
* Set the cross platform look and feel.
*
* @throws Exception if an error occurs
*/
// private static void setLookAndFeel() throws Exception {
// String lookAndFeelClassName = null;
// LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
// for(LookAndFeelInfo lookAndFeel : lookAndFeelInfos) {
// if("Nimbus".equals(lookAndFeel.getName())) {
// lookAndFeelClassName = lookAndFeel.getClassName();
// }
// }
// if(lookAndFeelClassName == null) {
// lookAndFeelClassName = UIManager.getSystemLookAndFeelClassName();
// }
// UIManager.setLookAndFeel(lookAndFeelClassName);
// }
}
对于youtube视频,您需要以下代码:
mediaPlayer.setPlaySubItems(true); // <--- This is very important for YouTube media
以下代码获取所有子项。
mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem) {
List<String> items = mediaPlayer.subItems();
System.out.println(items);
System.out.println(mediaPlayer.subItemCount());
}
});
当它工作时,我注意到上面的代码正在打印2个子项,其中一个是视频的实际网址。
但现在只打印一个这样的子项目:
[http://www.youtube.com/get_video_info?video_id=1t8fl96HPQI&el=detailpage]
这不是视频的实际网址,我认为这就是为什么不播放。
有关为何发生这种情况的任何想法?谷歌是否以其YouTube页面的格式更改了任何内容?
我尝试升级vlc youtube.lua文件,正如我发现的教程中所建议的但没有运气。
另外,当我在vlc中使用相同的链接时,它可以正常工作。
规格:
Ubuntu 12.0.4 64位
VLC媒体播放器2.0.5 Twoflower
vlcj 2.2.0
提前致谢,
马里奥斯
答案 0 :(得分:0)
google是否以其YouTube页面的格式更改了任何内容?
最有可能。这种情况一直发生在我不依赖于我的应用程序中的这个功能的地方(尽管如果我可以的话,它会很好。)它太气质了,对Youtube的布局进行了一些小改动,它停止了工作。我不确定VLCJ使用的Lua文件是否与安装VLC相同(尽管我可能在那里错了。)