Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed YouTube videos in newer versions of Delphi?

I'm trying to load a YouTube video into a TWebBrowser in Delphi XE7 and I'm getting an error that says this:

Adobe Flash Player or an HTML5 supported browser is required for video playback.
Get the latest Flash Player
Learn more about upgrading to an HTML5 browser

I can load normal HTML just fine.

The examples I've found posted here earlier are for much older versions of Delphi, so I'm wondering if this is an issue with newer versions, or TWebBrowser, or something in my environment (VMWare 7 with Windows 7).

EDIT: My objective is simply to be able to load and play a video from a URL, like a YouTube video. Solutions other than TWebBrowser are fine, especially if they can run cross-platform.

like image 918
David Schwartz Avatar asked Dec 25 '22 18:12

David Schwartz


2 Answers

You are wondering whether your problems relate to Delphi version. Well the WebBrowser control is a system control. Delphi version is not relevant because the service is provided by the underlying system. If anything has changed it is likely to be the way You Tube delivers videos.

If you are crafting the HTML that embeds the remote video then you should follow the latest documentation from You Tube as to how it should be done. Don't use years old Delphi specific articles as your guide. Use modern articles specific to the latest technology used by You Tube.

I do have a feeling, although you don't state so in the question, that you are using an old and possibly deprecated method to embed a You Tube video. Use an iframe as described here: http://www.w3schools.com/html/html_youtube.asp


Adobe Flash Player or an HTML5 supported browser is required for video playback.

Your WebBrowser control will, in the absence of you taking specific steps otherwise, be using a legacy IE browser engine. So it won't have HTML5 support. And perhaps not even Flash support, that is if You Tube is still prepared to serve videos as Flash. Nowadays HTML5 is preferred. Not least because modern browser support it out of the box and there is no need for third party Flash plugin installation.

One way to opt in to using a modern HTML5 browser with the WebBrowser control is to make explicit registry settings (browser feature emulation), and perhaps specify a DOCTYPE. More details here: How to have Delphi TWebbrowser component running in IE9 mode? Although that question specifically asks about IE9, the documentation links in the answer provide details for other IE versions.

If you don't have control over the HTML document then you will need to use the above method.

On the other hand, if you do control the content of the HTML document then there is another way. You can place this

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

as the first item in your page's <head>. The meaning of edge is the latest version of IE. If you wish to target a specific version, e.g. IE9 then you would use:

<meta http-equiv="X-UA-Compatible" content="IE=9" />

More info on this here:

  • https://msdn.microsoft.com/en-us/library/ms533876.aspx
  • https://www.modern.ie/en-gb/performance/how-to-use-x-ua-compatible
  • How to put the WebBrowser control into IE9 into standards?

Older versions of IE do not support this header and if you need to cater for them then you are back to browser feature emulation in the registry. Thanks to @whosrdaddy and @TLama in the comments, it seems that IE8 introduced support for X-UA-Compatible.

like image 183
David Heffernan Avatar answered Dec 28 '22 23:12

David Heffernan


As stated before I belive using TWebBrowser is the wrong way arround, because you have to little control about you video. Because then you have control over the video playback you self.

*** NOTE ****

DIRECT streaming of YouTube videos breaks the terms of service

*** NOTE ****

As i prommised you I've made an example here of howto play a youtube video on a Wincontrol ex. TPanel.

Since the example includes code for parsing the youtube URL and code for parsing the sourcecode of the youtube page where the video is embedded I can not post the complete source code here. And you have to get it from this link here

I'll here go trough the main idea of my exampel.

first a screenshot of the final result: enter image description here

The first thins to is are to import the WindowsMediaPlayer system component (not to be confused with the one ships with Delphi) and save WMPLib_TLB.pas alon with the project source.

Next step is do declare a private instance of the class:

WindowsMediaPlayer: TWindowsMediaPlayer;

And in formCreate, create an instance af set it up:

procedure TMainform.FormCreate(Sender: TObject);
begin
  WindowsMediaPlayer := TWindowsMediaPlayer.Create(Panel2);
  WindowsMediaPlayer.Parent := Panel2;
  WindowsMediaPlayer.Align := TAlign.alClient;
  WindowsMediaPlayer.Visible := True;
  WindowsMediaPlayer.Settings.AutoStart := True;
  WindowsMediaPlayer.uiMode := 'none';

  with TYoutubeThread.Create('https://www.youtube.com/watch?v=7vkYiCdn834') do
    OnTerminate := YoutubeThreadTerminate;
end;

Next step is to create an TYoutubeThread. TYoutubeThread is a thread that will get the HTML sourcocode of the requested youtubepage and parse it in order to get the information about the embedded video. The sourcecode for this thread are to be found in the complete example.

When the thread terminates we need to setup the GUI :

procedure TMainform.YoutubeThreadTerminate(Sender: TObject);
var
  YoutubeThread: TYoutubeThread;
begin
  YoutubeThread := Sender as TYoutubeThread;
  if YoutubeThread = nil then
    exit;

  //The information list are sorted my number of pixels in the video
  FInformation := YoutubeThread.Youtube.Informations.Last;

  Caption := Format('%s %s (%dx%d)', [YoutubeThread.Youtube.Title, FInformation.Quality, FInformation.Size.cx, FInformation.Size.cy]);
  Panel1.Visible := True;
  Width := FInformation.Size.cx + 50;
  Height := FInformation.Size.cy + Panel1.Height + 50;
  WindowsMediaPlayer.URL := FInformation.VideoLink;

  TrackBar1.Max := 0;
end;

Ive omitted two units, they can be downloded here http://pastebin.com/TqCUV9tg and here http://pastebin.com/WFGctwrf. And you'll lso need a copy of SuperObject

Or you could download the complete working example here

like image 28
Jens Borrisholt Avatar answered Dec 29 '22 00:12

Jens Borrisholt