YouTube
Building on previous tutorials, we will now show you how to play YouTube video.
This tutorial is covering YouTube, but the mechanism described here is actually common to many streaming services, e.g. internet radio stations likely work in the same way.
Background Information
VLC can play YouTube videos just fine, so that means a vlcj media player can too.
However there is a little bit more to consider...
The way it works in VLC is that you 'play' a YouTube 'watch' URL, like this:
This is not a direct streaming URL that can just start playing, it is a URL to a web-page on the YouTube web-site. The actual streaming URL is contained somewhere inside the HTML mark-up on that web-page.
This URL becomes the current media, and when played VLC recognises that you are trying to play a YouTube URL so it launches a LUA script (included with VLC) that loads the web-page at the URL and 'scrapes' it to find the streaming URL.
When the streaming URL is found, it does not immediately start playing. Instead, VLC creates a 'sub-item' which it attaches to the current media.
This 'screen-scraping' of the YouTube web page is brittle - if YouTube change the structure of their web-pages then VLC will sometimes fail to find the streaming URL, when this happens you have to wait for a developer to provide a new LUA script and wait for a new version of VLC to be released.
With vlcj-3 you would have had to handle playing the sub-item yourself, or request that the media player automatically play sub-items for you.
With vlcj-4, it should just work without you needing to do anything extra at all.
You've Seen This Before
It is no different to playing a local file:
It really is as simple as that.
Another Way
If you want, you can handle sub-items yourself.
Please consult the Javadoc for more information on sub-item handling.