Page 1 of 1

WebVideoGadget()

Posted: Mon Oct 07, 2013 4:53 pm
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


Re: WebVideoGadget()

Posted: Mon Oct 07, 2013 6:26 pm
by davido
Very nice, Thank you for sharing. :D

Re: WebVideoGadget()

Posted: Wed Oct 09, 2013 10:14 am
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

Re: WebVideoGadget()

Posted: Sun Nov 16, 2014 10:18 pm
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!

Re: WebVideoGadget()

Posted: Mon Nov 17, 2014 12:02 am
by falsam
minimy wrote:Do you know why not work this url.
http://www.purebasic.fr/english/viewtop ... 63#p456063

Re: WebVideoGadget()

Posted: Mon Nov 17, 2014 9:09 am
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?

Re: WebVideoGadget()

Posted: Sat Nov 29, 2014 4:59 pm
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.

Re: WebVideoGadget()

Posted: Sat Nov 29, 2014 7:56 pm
by Kiffi
minimy wrote:Hi Karellen, try this. [...]
no problems here:

Image

Greetings ... Peter (Win7 / PB5.31)

Re: WebVideoGadget()

Posted: Mon Dec 01, 2014 12:45 pm
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?

Re: WebVideoGadget()

Posted: Mon Feb 23, 2015 8:11 pm
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

Re: WebVideoGadget()

Posted: Sun Mar 01, 2015 5:41 pm
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/