Java 使用vlcj播放youtube视频不再工作

Java 使用vlcj播放youtube视频不再工作,java,ubuntu,youtube,vlc,vlcj,Java,Ubuntu,Youtube,Vlc,Vlcj,我在使用vlcj播放youtube视频时遇到问题。直到上周左右,一切都正常。 我打电话给mediaPlayer.playMedia(“”),工作正常,但不再是了。它根本不会产生任何错误。 请注意,我可以播放本地文件和其他在线流式URL,但不能播放youtube视频 这是我正在使用的代码。(基本上这是一个现成的例子) /* *此文件是VLCJ的一部分。 * *VLCJ是自由软件:您可以重新发布和/或修改它 *它是根据GNU通用公共许可证的条款发布的 *自由软件基金会,或者许可证的第3版,或者 *(

我在使用vlcj播放youtube视频时遇到问题。直到上周左右,一切都正常。 我打电话给mediaPlayer.playMedia(“”),工作正常,但不再是了。它根本不会产生任何错误。 请注意,我可以播放本地文件和其他在线流式URL,但不能播放youtube视频

这是我正在使用的代码。(基本上这是一个现成的例子)

/*
*此文件是VLCJ的一部分。
*
*VLCJ是自由软件:您可以重新发布和/或修改它
*它是根据GNU通用公共许可证的条款发布的
*自由软件基金会,或者许可证的第3版,或者
*(由您选择)任何更高版本。
*
*分发VLCJ是希望它会有用,
*但无任何保证;甚至没有任何关于
*适销性或适合某一特定目的。见
*有关更多详细信息,请参阅GNU通用公共许可证。
*
*您应该已经收到GNU通用公共许可证的副本
*还有VLCJ。如果没有,请参阅。
* 
*版权所有2009、2010、2011卡布里卡软件有限公司。
*/
包com.javacodegeks.youtube.test;
导入java.awt.BorderLayout;
导入java.awt.Canvas;
导入java.awt.Color;
导入java.awt.Frame;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入java.util.List;
导入javax.swing.BoxLayout;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UIManager.LookAndFeelInfo;
导入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;
导入uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.test.VlcjTest;
/**
*一个最小的YouTube播放器。
*
*URL/MRL必须采用以下格式:
* 
*   http://www.youtube.com/watch?v=000000
* 
*唯一不同于“常规”媒体播放器的是
*应用程序由以下代码组成:
* 
*mediaPlayer.setPlaySubItems(true);//
谷歌是否改变了youtube页面的格式

很有可能。这种情况经常发生,以至于我在应用程序中不依赖这种功能(尽管如果可以的话,这会很好)。它太易怒了,对Youtube的布局做了一个小小的更改,它就停止工作了。我不确定VLCJ使用的Lua文件是否与VLC的安装相同(尽管我可能错了)

/*
 * 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);
//  }
}
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());
      }
    });