Page 1 sur 1

Web Radio sans DLL

Publié : lun. 15/août/2016 1:43
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.

Re: Web Radio sans DLL

Publié : lun. 15/août/2016 9:58
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

Re: Web Radio sans DLL

Publié : lun. 15/août/2016 12:53
par Ar-S
C'est propre :)
Peut-on, comme via fmodex, récupérer artiste et nom du morceau ?

Re: Web Radio sans DLL

Publié : lun. 15/août/2016 12:59
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.

Re: Web Radio sans DLL

Publié : mar. 16/août/2016 8:52
par Micoute
Merci pour ce partage !

Re: Web Radio sans DLL

Publié : mar. 13/avr./2021 18:53
par Kwai chang caine
J'étais passé à coté :oops:
Merci en retard 8)