Web Radio sans DLL

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 :

Web Radio sans DLL

Message par falsam »

Sur la base d'un code de Firace (forum anglophone) que j'ai amélioré en ajoutant :
- Deux boutons pour augmenter ou diminuer le volume (html + JavaScript).
- Un sélecteur de radio.

Ce code fonctionne sous windows7 windows8 et windows10.

Code : Tout sélectionner

Enumeration window
  #mainForm
EndEnumeration

Enumeration gadget
  #audio
  #stream
EndEnumeration

Declare loadStream()
Declare selectStream()

Global stream.s, HTML.s

OpenWindow(#mainForm, 88, 244, 190, 160, "Web Radio", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
SetWindowColor(#mainForm, RGB(0, 0, 0))
WebGadget (#audio, 0, 0, 170, 137, "")
ComboBoxGadget(#stream, 0, 137, 190, 23)

BindGadgetEvent(#stream, @selectStream(), #PB_EventType_Change)

loadStream()
SetGadgetState(#stream, 0)
selectStream()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure loadStream()
  Restore stream
  
  For i = 0 To 3
    Read.s title$
    Read.s stream$
    AddGadgetItem(#stream, -1, title$)
    SetGadgetItemData(#stream, i, i)
  Next
EndProcedure

Procedure selectStream()
  Protected index = GetGadgetItemData(#stream, GetGadgetState(#stream)), n
  
  Restore stream
  For n = 0 To index
    Read.s title$
    Read.s stream$
  Next
  
  stream = stream$
  
  ;HTML
  HTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<body bgcolor=black scroll=no>"
  HTML + "<audio id = 'stream' autoplay controls>"
  HTML + "<source src='" + stream + "'>"
  HTML + "</audio>"
  HTML + "<p style='padding-left: 40px'>"
  HTML + "<button onclick='subtractVolume()' type='button' style= 'width: 50px; height: 24px' title='Volume -0.1'>-</button>"
  HTML + "<button onclick='addVolume()' type='button' style= 'width: 50px; height: 24px' title='Volume +0.1'>+</button>"
  HTML + "</p>"
  
  ;Script
  HTML + "<script>"
  HTML + "var audio = document.getElementById('stream');"
  HTML + "audio.volume = 0.5;"
  HTML + "function addVolume() { if (audio.volume + 0.1 < 1.0) { audio.volume += 0.1;} }"
  HTML + "function subtractVolume() { if (audio.volume - 0.1 > 0) { audio.volume -= 0.1;} }"
  HTML + "</script>"
  
  SetGadgetItemText(#audio, #PB_Web_HtmlCode , HTML)
EndProcedure

DataSection
  stream:
  Data.s "Deep link NYC", "http://176.9.219.133:9998/stream"
  Data.s "DI Radio", "http://5.39.71.159:8110/stream"
  Data.s "Club hits", "http://178.32.62.172:9371/stream"
  Data.s "Creek Valley Radio", "http://192.99.34.205:8356/stream"
EndDataSection
J'aimerais voir un screenshoot sous windows7. D'avance Merci.
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%
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Web Radio sans DLL

Message par falsam »

Merci Zorro pour cette capture sous window7. C'est parfait. :wink:

A propos des liens shoutcast.
Si vous insérez un lien Shoutcast il y a des chances qu'il ne fonctionne pas en lecture.
L'astuce consiste à mettre un /; à la fin de l'url.
Exemple : http://176.31.246.143:8300 devient http://176.31.246.143:8300/;

Ne pas insérer les contrôles de navigation.
Pour ne pas afficher les controles de navigation, enlever le flag controls.

Le code

Code : Tout sélectionner

HTML + "<audio id = 'stream' autoplay controls>"
devient

Code : Tout sélectionner

"<audio id = 'stream' autoplay>"
Exemple de code sans affichage des contrôle de navigation.

Code : Tout sélectionner

Enumeration window
  #mainForm
EndEnumeration

Enumeration gadget
  #audio
  #stream
EndEnumeration

Declare loadStream()
Declare selectStream()

Global stream.s, HTML.s

OpenWindow(#mainForm, 88, 244, 214, 26, "Web Radio", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
SetWindowColor(#mainForm, RGB(128, 128, 128))
ComboBoxGadget(#stream, 0, 1, 115, 24)
WebGadget (#audio, 115, -7, 175, 137, "")

BindGadgetEvent(#stream, @selectStream(), #PB_EventType_Change)

loadStream()
SetGadgetState(#stream, 0)
selectStream()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure loadStream()
  Restore stream
  
  For i = 0 To 6
    Read.s title$
    Read.s stream$
    AddGadgetItem(#stream, -1, title$)
    SetGadgetItemData(#stream, i, i)
  Next
EndProcedure

Procedure selectStream()
  Protected index = GetGadgetItemData(#stream, GetGadgetState(#stream)), n
  
  Restore stream
  For n = 0 To index
    Read.s title$
    Read.s stream$
  Next
  
  stream = stream$
  
  ;HTM
  HTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<body bgcolor='rgb(128,128,128)' scroll=no>"
  HTML + "<audio id = 'stream' autoplay>"
  HTML + "<source src='" + stream + "'>"
  HTML + "</audio>"
  HTML + "<button onclick='subtractVolume()' type='button' style= 'width: 25px; height: 25px' title='Volume -0.1'>-</button>"
  HTML + "<button onclick='addVolume()' type='button' style='width: 25px; height: 25px' title='Volume +0.1'>+</button>"
  HTML + "<button onclick='playpause()' type='button' style='width: 40px; height: 25px' title='Play or Pause' id='playpause' data-status='play'>||</button>"
  
  ;Script
  HTML + "<script>"
  HTML + "var audio = document.getElementById('stream');"
  HTML + "var selector = document.getElementById('playpause');"
  HTML + "audio.volume = 0.5;"
  HTML + "function addVolume() { if (audio.volume + 0.1 < 1.0) { audio.volume += 0.1;} }"
  HTML + "function subtractVolume() { if (audio.volume - 0.1 > 0) { audio.volume -= 0.1;} }"
  HTML + "function playpause() {"  
  HTML + "  if (selector.getAttribute('data-status') == 'play') {"
  HTML + "      audio.pause();"
  HTML + "      selector.innerHTML = '>>';"
  HTML + "      selector.setAttribute('data-status', 'pause');"
  HTML + "  } else {"
  HTML + "      audio.play();" 
  HTML + "      selector.innerHTML = '||';"
  HTML + "      selector.setAttribute('data-status', 'play');"
  HTML + "  }"
  HTML + "}"
  HTML + "</script>"
  
  SetGadgetItemText(#audio, #PB_Web_HtmlCode , HTML)
EndProcedure

DataSection
  stream:
  Data.s "Deep link NYC", "http://176.9.219.133:9998/stream"
  Data.s "DI Radio", "http://5.39.71.159:8110/stream"
  Data.s "Club hits", "http://178.32.62.172:9371/stream"
  Data.s "Creek Valley Radio", "http://192.99.34.205:8356/stream"
  Data.s "Chiltrax", "http://server1.chilltrax.com:9000/;"
  Data.s "Rire & Chanson", "http://cdn.nrjaudio.fm/audio1/fr/30401/mp3_128.mp3"
  Data.s "Chante France", "http://stream1.chantefrance.com/Chante_France"  
EndDataSection
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%
Avatar de l’utilisateur
Ar-S
Messages : 9477
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Web Radio sans DLL

Message par Ar-S »

C'est propre :)
Peut-on, comme via fmodex, récupérer artiste et nom du morceau ?
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Web Radio sans DLL

Message par falsam »

■ Lire un fichier MP3

Avec ce nouveau code il sera possible de lire un fichier mp3.
Difficulté : L''élément HTML audio n'accepte pas les accents et les apostrophes.

J'ai ajouté la procédure htmlentities() qui existe en php mais pas avec PureBasic pour transformer les accents et apostrophes en entités HTML.

Exemple le caractère é devient &#233;

Le code dans lequel vous retrouverez la procédure htmlentities()

Code : Tout sélectionner

Enumeration font
  #globalFont   ;Web + control
  #italicFont   ;Current select
EndEnumeration

Enumeration window
  #mainForm
EndEnumeration

Enumeration gadget
  #audio
  #stream
  #soundSelect
  #nowplaying
EndEnumeration

Declare loadStream()
Declare selectStream()
Declare selectSound()
Declare setUrl(source.s)
Declare.s htmlentities(buffer.s)

Global stream.s, HTML.s

; Fonts
LoadFont(#globalFont, "Arial", 9)
LoadFont(#italicFont, "Ariel", 9, #PB_Font_Italic)
SetGadgetFont(#PB_Default, FontID(#globalFont))

; Form
OpenWindow(#mainForm, 88, 244, 214, 110, "Web Radio", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
SetWindowColor(#mainForm, RGB(128, 128, 128))
ComboBoxGadget(#stream, 0, 1, 115, 24)
WebGadget (#audio, 115, -7, 175, 35, "")
ButtonGadget(#soundSelect, 0, 28, 214, 23, "Select Sound")
TextGadget(#nowplaying, 5, 55, 210, 60, "")
SetGadgetFont(#nowplaying, FontID(#italicFont))
SetGadgetColor(#nowplaying, #PB_Gadget_BackColor , RGB(128, 128, 128))
SetGadgetColor(#nowplaying, #PB_Gadget_FrontColor , RGB(154, 205, 50))

;Triggers
BindGadgetEvent(#stream, @selectStream(), #PB_EventType_Change)
BindGadgetEvent(#soundSelect, @selectSound(), #PB_EventType_LeftClick) 

loadStream()
SetGadgetState(#stream, 0)
selectStream()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure loadStream()
  Restore stream
  
  For i = 0 To 6
    Read.s title$
    Read.s stream$
    AddGadgetItem(#stream, -1, title$)
    SetGadgetItemData(#stream, i, i)
  Next
EndProcedure

Procedure selectStream()
  Protected index = GetGadgetItemData(#stream, GetGadgetState(#stream)), n
  
  Restore stream
  For n = 0 To index 
    Read.s title$
    Read.s stream$
  Next
  
  setURL(stream$) 
  SetGadgetText(#nowplaying, "You listen ..." + #CRLF$ + title$)
EndProcedure

Procedure selectSound()
  Protected pattern.s = "MP3 (*.mp3)|*.mp3|All files (*.*)|*.*"
  Protected source.s
  
  source =  OpenFileRequester("Select MP3", "*.mp3", pattern, 0)
  
  If source 
    setURL(htmlentities(source))
    SetGadgetText(#nowplaying, "you listen ..." + #CRLF$ + GetFilePart(source))
  EndIf
  
EndProcedure

Procedure setUrl(source.s)
  ;HTM
  HTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<body bgcolor='rgb(128,128,128)' scroll=no>"
  HTML + "<audio id = 'stream' autoplay>"
  HTML + "<source src='" + source + "'>"
  HTML + "</audio>"
  HTML + "<button onclick='subtractVolume()' type='button' style= 'width: 25px; height: 25px' title='Volume -0.1'>-</button>"
  HTML + "<button onclick='addVolume()' type='button' style='width: 25px; height: 25px' title='Volume +0.1'>+</button>"
  HTML + "<button onclick='playpause()' type='button' style='width: 40px; height: 25px' title='Play or Pause' id='playpause' data-status='play'>||</button>"
  
  ;Script
  HTML + "<script>"
  HTML + "var audio = document.getElementById('stream');"
  HTML + "var selector = document.getElementById('playpause');"
  HTML + "audio.volume = 0.5;"
  HTML + "function addVolume() { if (audio.volume + 0.1 < 1.0) { audio.volume += 0.1;} }"
  HTML + "function subtractVolume() { if (audio.volume - 0.1 > 0) { audio.volume -= 0.1;} }"
  HTML + "function playpause() {"  
  HTML + "  if (selector.getAttribute('data-status') == 'play') {"
  HTML + "      audio.pause();"
  HTML + "      selector.innerHTML = '>>';"
  HTML + "      selector.setAttribute('data-status', 'pause');"
  HTML + "  } else {"
  HTML + "      audio.play();" 
  HTML + "      selector.innerHTML = '||';"
  HTML + "      selector.setAttribute('data-status', 'play');"
  HTML + "  }"
  HTML + "}"
  HTML + "</script>"
  
  SetGadgetItemText(#audio, #PB_Web_HtmlCode , HTML)
EndProcedure

Procedure.s htmlentities(buffer.s)
  Protected i
  Protected CharToFind.s="'àáâãäåçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝŸ"

  For i=1 To Len(CharToFind) 
   buffer = ReplaceString(buffer, Mid(CharToFind,i,1), "&#" + Asc(Mid(CharToFind,i,1)) + ";")
  Next
  ProcedureReturn buffer
EndProcedure


DataSection
  stream:
  Data.s "Deep link NYC", "http://176.9.219.133:9998/stream"
  Data.s "DI Radio", "http://5.39.71.159:8110/stream"
  Data.s "Club hits", "http://178.32.62.172:9371/stream"
  Data.s "Creek Valley Radio", "http://192.99.34.205:8356/stream"
  Data.s "Chiltrax", "http://server1.chilltrax.com:9000/;"
  Data.s "Rire & Chanson", "http://cdn.nrjaudio.fm/audio1/fr/30401/mp3_128.mp3"
  Data.s "Chante France", "http://stream1.chantefrance.com/Chante_France"  
EndDataSection
On peut imaginer aussi un système de Drag & Drop.
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%
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: Web Radio sans DLL

Message par Micoute »

Merci pour ce partage !
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Web Radio sans DLL

Message par Kwai chang caine »

J'étais passé à coté :oops:
Merci en retard 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Répondre