WebVideoGadget()

Share your advanced PureBasic knowledge/code with the community.
Karellen
User
User
Posts: 84
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

WebVideoGadget()

Post by Karellen »

Here's a simple gadget to show web videos in your pb application just by passing by the browser url. Maybe it's useful for one or the other.
For now, it supports youtube and dailymotion. Other plattforms can be added easily.

Note: Music taste might vary! :D

Code: Select all

; --------------------------------------------------------------------------------
; WebVideoGadget
; --------------------------------------------------------------------------------
; Compiler        Pb 5.20 LTS 
; Author          Karellen (Pb forums)
; License         Public domain, use at your own risk
; Version         0.1.0 - initial release
; --------------------------------------------------------------------------------
; 
; Syntax:         Result = WebVideoGadget(#Gadget,x,y,Width,Height,URL$,[Flags])
;
;
; Description:    Displays a streaming video from internet services such as youtube.
;                 currently supported hosters are: Youtube, Dailymotion
;               
;
; Parameters:     #Gadget     A number to identify the new gadget. 
;                             #PB_Any can be used to auto-generate this number. 
;
;                 x,y,        The position and dimension of the new gadget.
;                 Width,     
;                 Height
;
;                 URL$        The browser url of the video. Just copy from your browser.
;
;                 Flags (youtube only)       
;
;                   #WVG_ShowRelated    show suggested videos at the end 
;                   #WVG_AutoPlay       starts playback automatically
;                   #WVG_Loop           enables loop playback
;
;
; Return value    Returns nonzero on success and zero on failure. If #PB_Any was 
;                 used as the #Gadget parameter then the Return-value is the 
;                 auto-generated gadget number on success. 
;
;
; Remarks         A webgadget is used to display the video, so gadget control 
;                 and event handling for the WebVideoGadget works similar.
;
;                 Legal note: Keep in mind, that embedding third party content 
;                 to your application might cause license problems. This gadget 
;                 gives you just the technical possibility to embed videos. 
;                 Any legal matters, like copyright issues, are untouched! 
;               
;
; Example         Simple example:
;
;                   If Openwindow(0,0,0,640,480,"WebVideoGadget")               
;                     WebVideoGadget(0,0,0,640,480,"http://www.youtube.com/watch?v=a8EMT01oSGw")
;                   Endif
;
;                   Repeat : Until WaitWindowEvent() = #PB_Event_Closewindow
;
;                 See bottom of file for a more advanced example. Just run this 
;                 file from the Pb ide. When you include this file the example 
;                 code will be ignored by the compiler
;
;
; See also        WebGadget()
;
;
; Supported OS    Windows only, because SetGadgetItemText with #PB_Web_HtmlCode 
;                 is just supported on Windows :(
;
; --------------------------------------------------------------------------------

#WVG_ShowRelated = 1
#WVG_AutoPlay = 2
#WVG_Loop = 4

Procedure WebVideoGadget(id.l, x.l, y.l, width.l, height.l, videoUrl.s, flags.l = 0)
  
  Protected html.s, position.l, flag.s, result.l
  
  ; create common html code
  html = "<html><body scroll=no>" +
         "<head><style type="+#DQUOTE$+"text/css"+#DQUOTE$+">" +
         "body { margin-left: 0px; margin-right:0px; margin-top:0px; margin-bottom:0px }" +
         "</style></head><body>" +
         "<iframe width="+#DQUOTE$+width+#DQUOTE$ + " height="+#DQUOTE$+height+#DQUOTE$ +
         " frameborder="+#DQUOTE$+"0"+#DQUOTE$ + " allowfullscreen"
    
  ; create hoster specific html code
  
  ; youtube
  If FindString(videoUrl.s,"youtube.com")
    position = FindString(videoUrl,"watch?v=")
    If position 
      videoUrl.s = RemoveString(videoUrl, Left(videoUrl,position+7))
    EndIf
    position = FindString(videoUrl,"/embed/")
    If position 
      videoUrl.s = RemoveString(videoUrl, Left(videoUrl,position+6))
    EndIf
    position = FindString(videoUrl,"&")
    If position
      videoUrl.s = Left(videoUrl,position-1)
    EndIf
    videoUrl = "http://www.youtube.com/embed/"+videoUrl
    flag.s = ""
    If flags >= #WVG_Loop
      flag.s + "loop=1"
      flags-#WVG_Loop
    EndIf
    If flags >= #WVG_AutoPlay
      If flag.s <> ""
        flag.s + "&"
      EndIf
      flag.s + "autoplay=1"
      flags-#WVG_AutoPlay
    EndIf
    If flags >= #WVG_ShowRelated
      If flag.s <> ""
        flag.s + "&"
      EndIf
      flag.s + "rel=0"
      flags-#WVG_ShowRelated
    EndIf
    
    videoUrl.s + "?" + flag.s
               
  ; dailymotion
  ElseIf FindString(videoUrl.s,"dailymotion.com")
    position = FindString(videoUrl.s,"#video=")
    If position 
      videoUrl.s = RemoveString(videoUrl, Left(videoUrl,position+6))
    EndIf    
    position = FindString(videoUrl.s,"/video/")
    If position
      videoUrl.s = RemoveString(videoUrl, Left(videoUrl,position+6))
    EndIf
    position = FindString(videoUrl,"_")
    If position
      videoUrl.s = Left(videoUrl,position-1)
    EndIf
    videoUrl = "http://www.dailymotion.com/embed/video/"+videoUrl
  EndIf
    
  ; complete the html code
  html + " src="+#DQUOTE$+videoUrl+#DQUOTE$ + ">" +
         "</iframe></body></html>"
  
  result.l = WebGadget(id,x,y,width,height,"")
  
  If result.l 
    SetGadgetItemText(id, #PB_Web_HtmlCode, html.s)
  EndIf
 
  ProcedureReturn result.l
  
EndProcedure

; --------------------------------------------------------------------------------
; EXAMPLE
; --------------------------------------------------------------------------------
; Due to the compilerif command this code will be ignored by the compiler 
; when you include this file in your application. 
; --------------------------------------------------------------------------------

CompilerIf #PB_Compiler_IsMainFile

  If OpenWindow(0,0,0,730,400,
                "WebVideoGadget - Select video from list by double click",
                #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
    
    FrameGadget(0,10,10,200,320,"Videos")
    ListViewGadget(1,20,40,180,270)
    AddGadgetItem(1,-1,"Phone ad")
    AddGadgetItem(1,-1,"Some news")
    AddGadgetItem(1,-1,"Good old music :)")
    AddGadgetItem(1,-1,"Movie trailer")
    AddGadgetItem(1,-1,"Simpsons")
    AddGadgetItem(1,-1,"Best english ever!")
    
    FrameGadget(2,220,10,500,320,"Display")
    WebVideoGadget(3,230,40,480,270, "")
    
    FrameGadget(4,10,340,710,50,"")
    TextGadget(5,20,362,700,30,"Double click a video from the list or enter an url here:")
    StringGadget(6,280,357,340,22,"")
    ButtonGadget(7,630,355,80,25,"Watch")
  EndIf
  
  
  Repeat 
    
    event = WaitWindowEvent()
    gadget = EventGadget()
    
    Select event
        
      Case #PB_Event_CloseWindow : End
        
      Case #PB_Event_Gadget
        
        If gadget=1 And EventType()=#PB_EventType_LeftDoubleClick
          
          Select GetGadgetState(1)    
            Case 0 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=9-nezImUP0w")
            Case 1 : WebVideoGadget(3,230,40,480,270, "http://www.dailymotion.com/video/x15nmoo_two-americans-and-a-german-share-nobel-prize-for-medicine_news")
            Case 2 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=1ljgzwZbxN0")
            Case 3 : WebVideoGadget(3,230,40,480,270, "http://www.dailymotion.com/video/x12csnp_gravity-i-ve-got-you-trailer_shortfilms")  
            Case 4 : WebVideoGadget(3,230,40,480,270, "http://www.dailymotion.com/video/x15l30f_simpsons-treehouse-of-horror-xxiv-references_shortfilms")  
            Case 5 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=AhmCgAV2aSA")
          EndSelect      
          
        ElseIf gadget=7
          
          WebVideoGadget(3,230,40,480,270, GetGadgetText(6))
          
        EndIf
                
    EndSelect
        
  ForEver
  
CompilerEndIf

Stanley decided to go to the meeting room...
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: WebVideoGadget()

Post by davido »

Very nice, Thank you for sharing. :D
DE AA EB
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: WebVideoGadget()

Post by falsam »

Nice job. Thank you for sharing. My Playlist :)

Code: Select all

; --------------------------------------------------------------------------------
; EXAMPLE
; --------------------------------------------------------------------------------
; Due to the compilerif command this code will be ignored by the compiler 
; when you include this file in your application. 
; --------------------------------------------------------------------------------

CompilerIf #PB_Compiler_IsMainFile

  If OpenWindow(0,0,0,730,400,
                "WebVideoGadget - Select video from list by double click",
                #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
    
    FrameGadget(0,10,10,200,320,"Videos")
    ListViewGadget(1,20,40,180,270)
    AddGadgetItem(1,-1,"Stromae - Formidable")
    AddGadgetItem(1,-1,"Glastonbury 2010 - Slash - Paradise City")

    AddGadgetItem(1,-1,"The Police - Every Breath You Take")
    AddGadgetItem(1,-1,"Scorpions - Tease Me Please Me")
    AddGadgetItem(1,-1,"Martin Garrix - Animals")
    
    FrameGadget(2,220,10,500,320,"Display")
    WebVideoGadget(3,230,40,480,270, "")
    
    FrameGadget(4,10,340,710,50,"")
    TextGadget(5,20,362,700,30,"Double click a video from the list or enter an url here:")
    StringGadget(6,280,357,340,22,"")
    ButtonGadget(7,630,355,80,25,"Watch")
  EndIf
  
  
  Repeat 
    
    event = WaitWindowEvent()
    gadget = EventGadget()
    
    Select event
        
      Case #PB_Event_CloseWindow : End
        
      Case #PB_Event_Gadget
        
        If gadget=1 And EventType()=#PB_EventType_LeftDoubleClick
          
          Select GetGadgetState(1)    
            Case 0 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=S_xH7noaqTA&list=TLaXmFzKuy0uChO87p5y-PFG_rcpzPunnC")
            Case 1 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=5outz-ciR6s")
            Case 2 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=OMOGaugKpzs")  
            Case 3 : WebVideoGadget(3,230,40,480,270, "http://www.dailymotion.com/fr/relevance/search/scorpions/1#video=x2v41p")  
            Case 4 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=gDErDsBDqpo")
          EndSelect      
          
        ElseIf gadget=7
          
          WebVideoGadget(3,230,40,480,270, GetGadgetText(6))
          
        EndIf
                
    EndSelect
        
  ForEver
  
CompilerEndIf

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: WebVideoGadget()

Post by minimy »

Hi friend and thanks for share.
Do you know why not work this url.

http://actualidad.rt.com/static/embed/envivo.html

Thanks!
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: WebVideoGadget()

Post by falsam »

minimy wrote:Do you know why not work this url.
http://www.purebasic.fr/english/viewtop ... 63#p456063

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
Karellen
User
User
Posts: 84
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

Re: WebVideoGadget()

Post by Karellen »

minimy wrote: Do you know why not work this url.
http://actualidad.rt.com/static/embed/envivo.html
Hi, the url works here without any problem. Can you give some more information?
Stanley decided to go to the meeting room...
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: WebVideoGadget()

Post by minimy »

Karellen wrote:
minimy wrote: Do you know why not work this url.
http://actualidad.rt.com/static/embed/envivo.html
Hi, the url works here without any problem. Can you give some more information?

Hi Karellen, try this.

Code: Select all

Enumeration
  #Mainform
  #Navigator
EndEnumeration

OpenWindow(#Mainform, 0, 0, 500, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
WebGadget(#Navigator, 10, 10, 480, 380, "http://actualidad.rt.com/static/embed/envivo.html")

Repeat : Until WaitWindowEvent(100) = #PB_Event_CloseWindow
No work in webgadget.
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: WebVideoGadget()

Post by Kiffi »

minimy wrote:Hi Karellen, try this. [...]
no problems here:

Image

Greetings ... Peter (Win7 / PB5.31)
Hygge
Karellen
User
User
Posts: 84
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

Re: WebVideoGadget()

Post by Karellen »

minimy wrote:Hi Karellen, try this.
Hi, your code works as expected on my laptop (win7/32 and pb 5.31/32) and my main working machine (win 8.1/64 and pb 5.24/32). Also, your url works fine here in all browsers. I don't think the problem is with PB.

Narrow the problem: Is it just this url or also others from this domain? Or maybe the entire domain? What exactly happens (blank screen or error message?) Does the url just fails in the webgadget or also in your browser(s)? Could the domain be blocked by your provider or third party software?
Stanley decided to go to the meeting room...
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: WebVideoGadget()

Post by Mythros »

Hi, Karellen! :) GREAT code! I just have one question though. Can you make it so that this code can stream live, real-time webcam video TO the net?

Thank you SO much!

Sincerely,

Mythros
Karellen
User
User
Posts: 84
Joined: Fri Aug 16, 2013 2:52 pm
Location: Germany

Re: WebVideoGadget()

Post by Karellen »

Hi Mythros,

that's a completely different thing. My Gagdet is not streaming anything, it just displays a webpage in an iframe using PB's webgadget.

To upstream a live video shouldn't be a big problem, have a look here for example:
http://www.sitepoint.com/stream-your-we ... avascript/
Stanley decided to go to the meeting room...
Post Reply