Jump to content
JohnLM

Creating an app to play Youtube videos

Recommended Posts

Specs:  Delphi XE7, VCL, Windows 7 -- youtube video URL's, to play videos only. 

 

I don't know why, but I am struggling with this one, how to create an app to just play youtube videos in.  

 

At first, I thought I should be using the TWebBrowser component.  But then I thought maybe that is not a good idea because then the whole webpage will show, and that is not what I want.  I only want to play video by its URL.  Thus, only to show a video window. 

 

I have a list of youtube URL's (and also their thumbnail URL's) , so I will use a combobox or listbox control on my form.  I would also like to play other website videos, like Vemeo, and Dailymotion, etc. 

 

Although I've played around with using the TWebBrowser, that was a fail--for obvious reasons.  And i've also tried CEF4Delphi and WebView4Delphi and those two are also not working.  So I am thinking I need some other component all-together.  

 

What (free) components do I need to accomplish this?

Share this post


Link to post

Can TWebBrowser be updated in Delphi XE7? I ask because I am sceptical about the old WebBrowser's compatibility with Youtube's system.

 

If you browse blogs a lot you will notice that sometimes the authors embed a Youtube video on their blog posts. I propose you find out how that is done and then use TWebBrowser (or other Browser component) with a web page you create with that embedded video. There might be a better way to do this but this is the only one I could think of apart from using a 3rd party Youtube library (if such a thing exists).

Edited by PeaShooter_OMO

Share this post


Link to post

Success!! 

 

I was initially trying the built-in TWebBrowser, which failed.  And then tried the recommended browser engine alternative for Windows 7 (under XE7) and it failed.   

 

But I believe it failed because I do not have that component, the WebView4Delphi (TWVBrowser and TWVWindowParent components) set up correctly and I was not getting a webpage or anything showing in the TWVWindorParent window. 

 

Then, I went to the demos folder and tried the SimpleBrowser and that worked, that is, it will show a browser window of anything, say for instance, www.google.com and so on, and all under Windows 7 on my laptop.  I use my laptop with win7 as my main work station.  I surf the internet and build applications and what-not's. 

 

And then after further searches, I found out how to play a Youtube video in a window using the WebView4Delphi browser component alternative (for win 7) without loading the whole youtube webpage.  And that is through embeding, and the same for Dailymotion videos, though a bit different, both methods work perfectly.

 

And now, all I need to do is add a few more features. 

 

877960807_im-praxis-creatinganapptoplayyoutubevideos.png.26472b91eedb22dedc8f07de66fc6f9f.png

 

 

 

 

 

Edited by JohnLM

Share this post


Link to post
8 hours ago, JohnLM said:

And then after further searches, I found out how to play a Youtube video in a window using the WebView4Delphi browser component alternative (for win 7) without loading the whole youtube webpage.  And that is through embeding, and the same for Dailymotion videos, though a bit different, both methods work perfectly.

 

Can you show what you did for embedding?

With a few lines of code, if possible

Share this post


Link to post

Actually, the screenshot in my previous post does show it :) 

 

But here is how I call the URL.  What I do is grab a URL via rightclick+copy from youtube's main dashboard and parse it to change its format and feed that into the Navigate() command in the button's [Go] event: 

 

/// 1
function findStr(fStr, sStr: string): boolean;
begin
  if pos(fStr,sStr,1)>0 then result:=true else result:=false;
end;
/// 1 - end

/// 2
function findStrReplace(sStr: string; fStr: string; rpStr: string): string;
begin
  if pos(fStr,sStr,1)>0 then result := StringReplace(sStr,fStr,rpStr,[rfReplaceAll, rfIgnoreCase]);
end;
/// 2 - end

procedure TMainForm.GoBtnClick(Sender: TObject);
var
  s: string;
begin               
{
change this https://www.youtube.com/watch?v=ZJhJILyS388
    to this https://www.youtube.com/embed/ZJhJILyS388
}
  s:=addresscb.Text;
  addressCb.Text:=findStrReplace(s, 'watch?v=', 'embed/'); //# 2
  WVBrowser1.Navigate(AddressCb.Text);
end;

I'm sure there are better ways to code it than how I did it but you get the idea.  Also, I have to do something similar for the Dailymotion URL's, which a bit more complicated but doable. Then, I will have to include some more conditions to determine what kind of video am I serving, Youtube, Dailymotion, etc.. So the code above will have to change, eventually. 

 

At least now, I can watch videos in a small window (can size it too, obviously) and can make the apps window stay-on-top while I work or code on the laptop.   Something I always wanted to do but didn't know how, until now. 

 

What I am thinking next, is how to add URL's to the combobox while the program is running.  That idea is tricky to me as I've never done that and don't know how, yet. Then, every time I copy/paste/[Go], the URL is to be checked if in the combobox list, and if not, add it, and if it is already there, ignore it.  Something like that, I guess. 

 

Anyway, I am already liking this app :) 

 

Share this post


Link to post
1 hour ago, JohnLM said:

What I am thinking next, is how to add URL's to the combobox while the program is running.  That idea is tricky to me as I've never done that and don't know how, yet. Then, every time I copy/paste/[Go], the URL is to be checked if in the combobox list, and if not, add it, and if it is already there, ignore it.  Something like that, I guess.

That is pretty trivial to implement, eg:

procedure TMainForm.GoBtnClick(Sender: TObject);
var
  url: string;
  idx: integer;
begin               
{
change this https://www.youtube.com/watch?v=ZJhJILyS388
    to this https://www.youtube.com/embed/ZJhJILyS388
}
  url := StringReplace(addresscb.Text, 'watch?v=', 'embed/', [rfIgnoreCase]);
  addressCb.Text := url;
  WVBrowser1.Navigate(url);
  idx := ComboBox1.Items.IndexOf(url);
  if idx = -1 then
    ComboBox1.Items.Add(url);
end;

 

Share this post


Link to post
13 hours ago, JohnLM said:

and if not, add it, and if it is already there, ignore it.  Something like that, I guess. 

 

Maybe you can read the clipboard with a timer and everytime the clipboard has a new "https://www.youtube.com" string you parse, add (see Remy) and start the video.

Share this post


Link to post
11 minutes ago, Die Holländer said:

Maybe you can read the clipboard with a timer and everytime the clipboard has a new "https://www.youtube.com" string you parse

He can also be notified if the clipboard changes. There is also a sequence number that indicates the change. I suggest a thorough read of Using the Clipboard

  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×