WebVideoGadget (Contributeur : Karellen)

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

WebVideoGadget (Contributeur : Karellen)

Message par falsam »

Vu sur le forum anglais, WebVideoGadget permet de se constituer une playlist vidéo de vos titres préférées issue de youtube et dailymotion.

L'intégration des vidéos youtube n'est pas simple car youtube ne permet pas la lecture de certains de ses titres dans des lecteurs tiers.

Vous trouverez aussi un extrait plutot hétéroclite de ma playlist du jour. N'hésitez pas à fournir la votre en fournissant le code de la zone Example située à la fin de ce code.

Code : Tout sélectionner

; --------------------------------------------------------------------------------
; 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,"Stromae - Formidable")
    AddGadgetItem(1,-1,"Glastonbury 2010 - Slash - Paradise City")
    AddGadgetItem(1,-1,"Grand Corps Malade - j'ai pas les mots")
    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=otOLRmyUo9Q")
            Case 3 : WebVideoGadget(3,230,40,480,270, "http://www.youtube.com/watch?v=OMOGaugKpzs")  
            Case 4 : WebVideoGadget(3,230,40,480,270, "https://www.youtube.com/watch?v=wENdZneWDYs") 
            Case 5 : 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
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%