Yet Another Video Player

Share your advanced PureBasic knowledge/code with the community.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Yet Another Video Player

Post by chris319 »

Here is a simple video player which plays YouTube videos in a web gadget. It's nice to see videos without all of the YouTube bling.

If you have "controls" enabled (set to 1) in the URL, by clicking in the video area you can start and pause the video, control the volume, etc. You then exit the program by clicking on the control bar at the bottom of the screen. Or, you can disable "controls" (set to 0) in the URL if you don't need the controls.

I have it set to play full screen but you can tailor this to suit. The next thing it can use is a better way of entering the YouTube video URL.

Suggestions for improvements welcome.

Code: Select all

Enumeration
#MainWindow
#WebGadget
EndEnumeration

ExamineDesktops()
nWidth = DesktopWidth(0)
nHeight = DesktopHeight(0)

;WebAddress.s = "https://www.youtube.com/embed/OALyNX4IbBI?autoplay=1&showinfo=0&controls=1"
;URL$ = "https://www.youtube.com/embed/lGbF_F_i0cY?autoplay=1&showinfo=0&controls=1&rel=0"
webAddress.s = "https://www.youtube.com/embed/lGbF_F_i0cY?autoplay=1&showinfo=0&controls=0&rel=0"

HTML.s = "<html>" + #LF$ +
#TAB$ + "<head>" + #LF$ +
#TAB$ + #TAB$ + "<style type='text/css'>" + #LF$ +
#TAB$ + #TAB$ + #TAB$ + "body {margin-left: 0px; margin-right:0px; margin-top:0px; margin-bottom:0px; overflow: hidden}" + #LF$ +
#TAB$ + #TAB$ + "</style>" + #LF$ +
#TAB$ + "</head>" + #LF$ +
#TAB$ + "<body scroll='no'>" + #LF$ +
#TAB$ + #TAB$ + "<iframe width='" + Str(nWidth) + "' height='" + Str(nHeight) + "' src='" + WebAddress + "' frameborder='0'></iframe>" + #LF$ +
#TAB$ + "</body>" + #LF$ +
"</html>"
lpValueName.s = GetFilePart(ProgramFilename()) : lpData = 11001

If RegCreateKeyEx_(#HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", 0, #Null, #REG_OPTION_VOLATILE, #KEY_ALL_ACCESS, #Null, @phkResult, @lpdwDisposition) = #ERROR_SUCCESS
RegSetValueEx_(phkResult, lpValueName, 0, #REG_DWORD, @lpData, SizeOf(LONG))
RegCloseKey_(phkResult)

If OpenWindow(#MainWindow, 0, 0, nWidth, nHeight, "WebGadget: YouTube",#PB_Window_BorderLess) 
AddKeyboardShortcut(#MainWindow, #PB_Shortcut_Escape, 27);ESC TO QUIT

WebGadget(#WebGadget, 0, 0, nWidth, nHeight, #Null$)
SetGadgetAttribute(#WebGadget, #PB_Web_BlockPopupMenu, #True)
SetGadgetAttribute(#WebGadget, #PB_Web_BlockPopups, #True)

WebBrowser2.IWebBrowser2 = GetWindowLongPtr_(GadgetID(#WebGadget), #GWL_USERDATA)
WebBrowser2\put_Silent(#True)
SetGadgetItemText(#WebGadget, #PB_Web_HtmlCode, HTML)

Repeat ;: Until WaitWindowEvent() = #PB_Event_CloseWindow 

event = WaitWindowEvent(20)

If event = #PB_Event_Menu ;KEYBOARD INPUT
menuItem = EventMenu()
Select menuItem

Case 27:CloseWindow(#MainWindow): End

EndSelect

EndIf

ForEver

EndIf

If RegOpenKeyEx_(#HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", 0, #KEY_SET_VALUE, @phkResult) = #ERROR_SUCCESS
RegDeleteValue_(phkResult, lpValueName)
RegCloseKey_(phkResult)
EndIf : End
Else
MessageRequester("WebGadget: YouTube", "Registry update failed." + #LF$ + #LF$ + "Operation cancelled.", #MB_ICONERROR)

 EndIf
Last edited by chris319 on Sun Jul 23, 2017 7:48 pm, edited 3 times in total.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Yet Another Video Player

Post by chris319 »

The previous version of this program had bugs, sorry. See original post for new, bug-fixed version.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Yet Another Video Player

Post by JHPJHP »

Still has bugs...

Original working version: http://www.purebasic.fr/english/viewtop ... 90#p507090
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: Yet Another Video Player

Post by VB6_to_PBx »

chris319 wrote:The previous version of this program had bugs, sorry. See original post for new, bug-fixed version.
anything wrong with doing it this way ?

Code: Select all

ShellExecute_(0,"","https://www.youtube.com/embed/OALyNX4IbBI",#Null,#Null,#SW_SHOW)
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Yet Another Video Player

Post by JHPJHP »

Not sure what ShellExecute has to do with playing a YouTube video in a PureBasic application from a WebGadget.

If just opening a YouTube video was the intent, why not use RunProgram and make it cross-platform?
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Yet Another Video Player

Post by chris319 »

JHPJHP wrote:Still has bugs...
It works fine for me. Could you please elucidate on what the alleged bugs are?
Last edited by chris319 on Sat Jul 22, 2017 12:14 pm, edited 3 times in total.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: Yet Another Video Player

Post by VB6_to_PBx »

JHPJHP wrote:Not sure what ShellExecute has to do with playing a YouTube video in a PureBasic application from a WebGadget.

If just opening a YouTube video was the intent, why not use RunProgram and make it cross-platform?

the following Code plays just the YouTube Video in the PB WebGadget
but unfortunately its Windows only

Code: Select all



;        WebBrowser__PureBasic_MiniBrowser__v3.pb

;        http://www.purebasic.fr/english/viewtopic.php?f=13&t=51988

;
; ------------------------------------------------------------
;
;   PureBasic - MiniBrowser
;
;    (c) 2003 - Fantaisie Software
;
; ------------------------------------------------------------
;
; This program requiers the Microsoft freely distribuable
; ATL.dll shared library.
;

Procedure ResizeWebWindow()
  ResizeGadget(10, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-52)
  ResizeGadget(4, #PB_Ignore, #PB_Ignore, WindowWidth(0)-185, #PB_Ignore)
  ResizeGadget(5, WindowWidth(0)-25, #PB_Ignore, #PB_Ignore, #PB_Ignore)
  ResizeGadget(6, #PB_Ignore, #PB_Ignore, WindowWidth(0), #PB_Ignore)
EndProcedure


If OpenWindow(0, 100, 200, 500, 300, "PureBasic MiniBrowser v1.0", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)

  CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
    StatusBarText(0, 0, "Welcome to the world's smallest Browser ! :)", 0)
     
  ButtonGadget(1,   0, 0, 50, 25, "Back")
  ButtonGadget(2,  50, 0, 50, 25, "Next")
  ButtonGadget(3, 100, 0, 50, 25, "Quit")

  StringGadget(4, 155, 5, 0, 20, "" )
 
  ButtonGadget(5, 0, 0, 25, 25, "Go")
 
  FrameGadget(6, 0, 30, 0, 2, "", 2) ; Nice little separator

;- Note : need to Parse https://www.youtube.com/watch?feature=player_embedded&v=QtxvPRev3I8   to only this -> https://www.youtube.com/v/QtxvPRev3I8
;- Note : need to Parse https://www.youtube.com/watch?v=CD1CYMPNSis   to only this -> https://www.youtube.com/v/CD1CYMPNSis
  If WebGadget(10, 0, 31, 0, 0, "https://www.youtube.com/v/CD1CYMPNSis") = 0 : MessageRequester("Error", "ATL.dll not found", 0) : End : EndIf

 
  AddKeyboardShortcut(0, #PB_Shortcut_Return, 0)
 
  ResizeWebWindow()
 
  Repeat
    Event = WaitWindowEvent()
   
    Select Event
      Case #PB_Event_Gadget
     
        Select EventGadget()
          Case 1
            SetGadgetState(10, #PB_Web_Back)
         
          Case 2
            SetGadgetState(10, #PB_Web_Forward)
         
          Case 3
            SetGadgetState(10, #PB_Web_Stop)
               End
         
          Case 5
            SetGadgetText(10, GetGadgetText(4))
           
        EndSelect     
     
      Case #PB_Event_Menu ; We only have one shortcut
        SetGadgetText(10, GetGadgetText(4))

      Case #PB_Event_SizeWindow
        ResizeWebWindow()
     
    EndSelect
     
  ForEver
   
EndIf
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: Yet Another Video Player

Post by VB6_to_PBx »

updated Code with better Links and revolving choices in Select Case

Code: Select all





;        WebBrowser__PureBasic_MiniBrowser__v5.pb

;        http://www.purebasic.fr/english/viewtopic.php?f=13&t=51988

;
; ------------------------------------------------------------
;
;   PureBasic - MiniBrowser
;
;    (c) 2003 - Fantaisie Software
;
; ------------------------------------------------------------
;
; This program requiers the Microsoft freely distribuable
; ATL.dll shared library.
;

Define.i Cnt

Procedure ResizeWebWindow()
		  ResizeGadget(10, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-52)
		  ResizeGadget(4, #PB_Ignore, #PB_Ignore, WindowWidth(0)-185, #PB_Ignore)
		  ResizeGadget(5, WindowWidth(0)-25, #PB_Ignore, #PB_Ignore, #PB_Ignore)
		  ResizeGadget(6, #PB_Ignore, #PB_Ignore, WindowWidth(0), #PB_Ignore)
EndProcedure


If OpenWindow(0, 100, 200, 940, 705, "PureBasic MiniBrowser v1.0", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)

    CreateStatusBar(0, WindowID(0))
         AddStatusBarField(#PB_Ignore)
         StatusBarText(0, 0, "Welcome to the world's smallest Browser ! :)", 0)
     
    ButtonGadget(1,   0, 0, 50, 25, "Back")
    ButtonGadget(2,  50, 0, 50, 25, "Next")
    ButtonGadget(3, 100, 0, 50, 25, "Quit")
    StringGadget(4, 155, 5, 0, 20, "" )
    ButtonGadget(5, 0, 0, 25, 25, "Go")
    FrameGadget(6, 0, 30, 0, 2, "", 2) ; Nice little separator

;-  Note : need to Parse https://www.youtube.com/watch?v=CD1CYMPNSis   to only this -> https://www.youtube.com/v/CD1CYMPNSis
    If WebGadget(10, 0, 31, 0, 0, "https://www.youtube.com/v/CD1CYMPNSis") = 0 : MessageRequester("Error", "ATL.dll not found", 0) : End : EndIf
    AddKeyboardShortcut(0, #PB_Shortcut_Return, 0)
    ResizeWebWindow()
 
    Repeat
         Event = WaitWindowEvent()
   
         Select Event
         Case #PB_Event_Gadget
              Select EventGadget()
              Case 1 : SetGadgetState(10, #PB_Web_Back)
              Case 2
                   SetGadgetState(10, #PB_Web_Forward)
							            Select Cnt
							            Case 0 : Cnt = Cnt + 1 : SetGadgetText(10, "https://www.youtube.com/v/1Qcexr6CZ5Y")
							            Case 1 : Cnt = Cnt + 1 : SetGadgetText(10, "https://www.youtube.com/v/V1wWKOAdS38")
							            Case 2 : Cnt = Cnt + 1 : SetGadgetText(10, "https://www.youtube.com/v/wTG-bCMG05E")
							            Case 3 : Cnt = Cnt + 1 : SetGadgetText(10, "https://www.youtube.com/v/qoX0Olfqziw")
							            Case 4 : Cnt = Cnt + 1 : SetGadgetText(10, "https://www.youtube.com/v/tZugz8FTqog")
							            Case 5 : Cnt = Cnt + 1 : SetGadgetText(10, "https://www.youtube.com/v/euMNVyuqmwo")
                                 Case 6 : Cnt = 0 : SetGadgetText(10, "https://www.youtube.com/v/CD1CYMPNSis")
							            EndSelect
              Case 3 : SetGadgetState(10, #PB_Web_Stop) : End
              Case 5 : SetGadgetText(10, GetGadgetText(4))
              EndSelect     
         Case #PB_Event_Menu ; We only have one shortcut
              SetGadgetText(10, GetGadgetText(4))
         Case #PB_Event_SizeWindow : ResizeWebWindow()
         EndSelect
    ForEver
EndIf




 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Yet Another Video Player

Post by chris319 »

Here it is with a help window activated by pressing F1.

Thanks to JHPJHP for improvements to player code.

Code: Select all

Enumeration
#MainWindow
#WebGadget
#helpWindow
#textGadget1
#textGadget2
#textGadget3
#textGadget4
#textGadget5
#textGadget6
EndEnumeration

Procedure helpWindow()

OpenWindow(#helpWindow,0,0,800,400,"",#PB_Window_ScreenCentered|#PB_Window_BorderLess)
helpWindowOpen = #True

gadWidth = WindowWidth(#helpWindow)
gadX = WindowWidth(#helpWindow) / 8

TextGadget(#textGadget1,gadX,50,gadWidth, 40,"Click on video content for keyboard control.")
SetGadgetFont(#textGadget1,FontID(1))

TextGadget(#textGadget2,gadX,100,WindowWidth(#helpWindow), 40,"Space bar: Pause / Play")
SetGadgetFont(#textGadget2,FontID(1))

TextGadget(#textGadget3,gadX,150,WindowWidth(#helpWindow), 40,"Up / Down arrows: Volume")
SetGadgetFont(#textGadget3,FontID(1))

TextGadget(#textGadget4,gadX,200,WindowWidth(#helpWindow), 40,"0 (zero): Restart from beginning")
SetGadgetFont(#textGadget4,FontID(1))

TextGadget(#textGadget5,gadX,250,WindowWidth(#helpWindow), 40,"F1: This window")
SetGadgetFont(#textGadget5,FontID(1))

TextGadget(#textGadget6,gadX,300,WindowWidth(#helpWindow), 40,"ESC: Exit program")
SetGadgetFont(#textGadget6,FontID(1))

EndProcedure

openHelp = #False: helpWindowOpen = #False

ExamineDesktops()
nWidth = DesktopWidth(0)
nHeight = DesktopHeight(0)

vid$ = "lGbF_F_i0cY"

webAddress.s = "https://www.youtube.com/embed/" + vid$ + "?autoplay=1&showinfo=0&controls=0&rel=0"

HTML.s = "<html>" + #LF$ +
#TAB$ + "<head>" + #LF$ +
#TAB$ + #TAB$ + "<style type='text/css'>" + #LF$ +
#TAB$ + #TAB$ + #TAB$ + "body {margin-left: 0px; margin-right:0px; margin-top:0px; margin-bottom:0px; overflow: hidden}" + #LF$ +
#TAB$ + #TAB$ + "</style>" + #LF$ +
#TAB$ + "</head>" + #LF$ +
#TAB$ + "<body scroll='no'>" + #LF$ +
#TAB$ + #TAB$ + "<iframe width='" + Str(nWidth) + "' height='" + Str(nHeight) + "' src='" + WebAddress + "' frameborder='0'></iframe>" + #LF$ +
#TAB$ + "</body>" + #LF$ +
"</html>"

lpValueName.s = GetFilePart(ProgramFilename()) : lpData = 11001

If RegCreateKeyEx_(#HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", 0, #Null, #REG_OPTION_VOLATILE, #KEY_ALL_ACCESS, #Null, @phkResult, @lpdwDisposition) = #ERROR_SUCCESS
RegSetValueEx_(phkResult, lpValueName, 0, #REG_DWORD, @lpData, SizeOf(LONG))
RegCloseKey_(phkResult)

If OpenWindow(#MainWindow, 0, 0, nWidth, nHeight, "",#PB_Window_BorderLess) 
AddKeyboardShortcut(#MainWindow,#PB_Shortcut_Escape, 27);ESC TO QUIT
AddKeyboardShortcut(#MainWindow,#PB_Shortcut_F1,101);ESC TO QUIT

WebGadget(#WebGadget, 0, 0, nWidth, nHeight,"")
SetGadgetAttribute(#WebGadget, #PB_Web_BlockPopupMenu, #True)
SetGadgetAttribute(#WebGadget, #PB_Web_BlockPopups, #True)

LoadFont(1, "Arial", 18,#PB_Font_Bold)

WebBrowser2.IWebBrowser2 = GetWindowLongPtr_(GadgetID(#WebGadget),#GWL_USERDATA)
WebBrowser2\put_Silent(#True)
SetGadgetItemText(#WebGadget, #PB_Web_HtmlCode, HTML)

HelpWindow()

Repeat

event = WaitWindowEvent(20)

If GetActiveWindow() = #mainWindow And helpWindowOpen = #True
helpWindowOpen = #False
CloseWindow(#helpWindow)
EndIf

If event = #PB_Event_Menu ;KEYBOARD INPUT
menuItem = EventMenu()

Select menuItem

Case 27:CloseWindow(#MainWindow): End

Case 101: helpwindow()

EndSelect

EndIf

ForEver

EndIf

If RegOpenKeyEx_(#HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", 0, #KEY_SET_VALUE, @phkResult) = #ERROR_SUCCESS
RegDeleteValue_(phkResult, lpValueName)
RegCloseKey_(phkResult)
EndIf : End
Else
MessageRequester("WebGadget: YouTube", "Registry update failed." + #LF$ + #LF$ + "Operation cancelled.", #MB_ICONERROR)

EndIf
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Yet Another Video Player

Post by JHPJHP »

Hi chris319,

Works fine, but the information [ F1 ] TextGadget's are a bit out of place.
Because the video is already embedded HTML, you would be better off with a floating (semi-transparent) DIV, controlled from a JavaScript Event Handler, refined using CSS.

PureBasic_MiniBrowser_2003_example_modified_2017:
chris319 wrote:YouTube does support HTML5 but I kind of doubt PB does.
The PureBasic WebGadget is HTML5 compatible after adding the Registry setting FEATURE_BROWSER_EMULATION (Windows Only).
- see this post for additional information: WebGadget; FEATURE_BROWSER_EMULATION; Windows 10;
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Yet Another Video Player

Post by chris319 »

JHPJHP wrote:The PureBasic WebGadget is HTML5 compatible after adding the Registry setting FEATURE_BROWSER_EMULATION (Windows Only).
- see this post for additional information: WebGadget; FEATURE_BROWSER_EMULATION; Windows 10;
Thank you for explaining that.

I would like the user to have immediate keyboard control over playback when the program starts without having to click on the web gadget. I have tried SetActiveGadget() on the web gadget but to no avail. Any ideas?

The help window as is forces the user to click on the video to get the window out of the way and to gain keyboard control over playback. The opaque background gives me control over the appearance of the text and its background for greatest legibility.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Yet Another Video Player

Post by JHPJHP »

Hi chris319,
chris319 wrote:I would like the user to have immediate keyboard control over playback when the program starts without having to click on the web gadget. I have tried SetActiveGadget() on the web gadget but to no avail. Any ideas?
Applied a simple work-around using the Functions SetCursorPos and mouse_event.
- side effect to the (double) mouse_event: autoplay=0 enables auto play, autoplay=1 disables auto play

The trick was using a JavaScript EventListener combined with PureBasic script, to know when the page would accept the Mouse-Click event; used in many of my HTML5 scripts.
Last edited by JHPJHP on Thu Jul 27, 2017 6:57 am, edited 3 times in total.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Yet Another Video Player

Post by chris319 »

I tried your example program and still had to click on the video content (web gadget) to gain keyboard control. The alternative would be to have a setting where the user doesn't have to click on anything but would forego keyboard control. The user would be able to end the video (and the program) by pressing the ESC key. That part works well. No clicking required.

I hadn't seen the html5 tag embedded in the URL before, so thanks for that.

I would like to see no YouTube branding at all but some appears initially, then disappears after a few seconds.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Yet Another Video Player

Post by JHPJHP »

Hi chris319,
chris319 wrote:I tried your example program and still had to click on the video content (web gadget) to gain keyboard control.
Not sure why it didn't work for you. Tested on PureBasic 5.60 (x86 / x64) ... Windows 10:
- updated the functions that set the WebGadget focus

Updated the example with a new method to change various HTML parameters dynamically...

As was previously posted, I included your idea of an information menu, but instead of TextGadgets, I used an embedded semi-transparent DIV.
- press the F1 key to toggle the information menu ON / OFF
chris319 wrote:I would like to see no YouTube branding at all but some appears initially, then disappears after a few seconds.
Some of the YouTube branding can be removed by adding the modestbranding tag; included in the latest update.
Last edited by JHPJHP on Thu Jul 27, 2017 6:57 am, edited 1 time in total.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Yet Another Video Player

Post by chris319 »

In your example the video begins playing immediately but with no keyboard control until you click on the web gadget, just like mine.

Setting modestbranding to 1 or 0 makes no difference in my program. Either way, I get the video title and an arrow at the top of the screen when the mouse is moved. This doesn't bother me because the user shouldn't be moving the mouse once playback is underway.

Note that mine is a full-screen player intended top show only the video. If the user wants to watch a video inside a postage stamp, any browser will do that.

Is there a way to query YouTube and get the H and V dimensions of a video? It would be useful to know the native aspect ratio of the video.
Locked