Page 1 of 2

Buttongadget and loadsound

Posted: Tue Jul 26, 2022 9:11 am
by Stefan
;works

Code: Select all

buttonPic=LoadImage(#PB_Any,"pictures/button.jpg.")
ButtonImageGadget(1,10,10,ImageWidth(buttonPic),ImageHeight(buttonPic),ImageID(buttonPic))
;doesn' work Programm hang

Code: Select all

buttonPic=LoadImage(#PB_Any,"https://myhomepage.com/pictures/button.jpg.")
ButtonImageGadget(1,10,10,ImageWidth(buttonPic),ImageHeight(buttonPic),ImageID(buttonPic))
;In both cases it is guaranteed that the image is loaded.
;In example 2 the program freezes and the buttonimagegadget is not created.



;This works

Code: Select all

buttonPic=LoadImage(#PB_Any,"https://myhomepage.com/pictures/button.jpg.")
ImageGadget(1,10,10,ImageWidth(buttonPic),ImageHeight(buttonPic),ImageID(buttonPic))


;works:

Code: Select all

LoadSound(2, "sound/sound.wav")
;doesn' work ,sound not loading

Code: Select all

LoadSound(2, "https://myhomepage/sound/sound.wav")
Does anyone have an idea how to get this to work?

// Moved from "Bugs Reports" to "Coding Questions" (Peter)

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 9:33 am
by Peter
Take a look at the SB help on the topic of LoadImage() and LoadSound().
There are the events #PB_Event_Loading and #PB_Event_LoadingError that you should bind.

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 9:39 am
by Stefan
I have the events #PB_Event_Loading and #PB_Event_LoadingError bound.
For the sake of clarity, I wrote the code like this.

As I already wrote, the pictures are loaded, I just can't get them into the ButtonImageGadget.
The sound will not load if the address is an external homepage.

ListIconGadget also does not work if you load the image from an external website.

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 9:58 am
by Peter
Please open the developer console of your browser (<F12>) and then click on the Network tab.
I guess that it contains something similar to:
Access to XMLHttpRequest at [...] from origin 'http://127.0.0.1:9080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 10:05 am
by hoerbie
With external website problems my first thought would be CORS https://en.wikipedia.org/wiki/Cross-ori ... ce_sharing

In general for all Load*() functions the loaded sources should only be used, after the #PB_Event_Loading was received, so a code like

Code: Select all

buttonPic=LoadImage(#PB_Any,"pictures/button.jpg.")
ButtonImageGadget(1,10,10,ImageWidth(buttonPic),ImageHeight(buttonPic),ImageID(buttonPic))
will only work with big luck or random, maybe when the image is in browser cache.
I would define an empty ButtonImageGadget() and then set the image with SetGadgetAttribute(imageid, #PB_Button_Image) from the event procedure where #PB_Event_Loading was received.

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 10:40 am
by Stefan
Try both variants of homepage

Code: Select all



Global homepage.s,pic1,pic2,aSound,testlisticongadget
;homepage="https://myhomepage.com/"
homepage=""

Declare LoadingError(Type, Filename$)
Declare Loading(Type, Filename$, ObjectId)


 InitSound()
 OpenWindow(0,10,100,800,600,"TestWindow")
 
  testlisticongadget=ListIconGadget(#PB_Any, 10,100, 290, 90, "picture", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)

  
  
  aSound=LoadSound(#PB_Any, homepage+"sound.wav") 
  pic1=LoadImage(#PB_Any, homepage+"apic.png") 
  
  
  Procedure Loading(Type, Filename$, ObjectId)
    Debug Filename$ + " loaded (id = " + ObjectId + ")"
    
    If Filename$=homepage+"apic.png"
      AddGadgetItem(testlisticongadget, 1, "Picture 1", ImageID(pic1))
      ImageGadget(#PB_Any,10,10,ImageWidth(pic1),ImageHeight(pic1),ImageID(pic1))
      ButtonImageGadget(#PB_Any,10,50,ImageWidth(pic1),ImageHeight(pic1),ImageID(pic1))
    EndIf
    
    
    
  EndProcedure
    
  Procedure LoadingError(Type, Filename$)
    Debug Filename$ + ": loading error"
  EndProcedure
    
  ; Register the loading event before calling any resource load command
  BindEvent(#PB_Event_Loading, @Loading())
  BindEvent(#PB_Event_LoadingError, @LoadingError())
  
 





Yes, that's right, the sound is blocked on the external homepage, but the pictures are not.
Why doesn't the listicongadget and buttonimagegadget work?

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 10:51 am
by hoerbie
Instead of the variable "pic1" please try "ObjectId" in your "Procedure Loading" for the gadgets.

And please take care of the "; Register the loading event before calling any resource load command"

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 11:42 am
by Stefan
Instead of the variable "pic1" please try "ObjectId" in your "Procedure Loading" for the gadgets.
In this example, the sound and image would have the same objectid, so I took the name of the image.

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 2:21 pm
by Peter
Stefan wrote: Tue Jul 26, 2022 10:40 amWhy doesn't the listicongadget and buttonimagegadget work?
take a look at the developer console:
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
This is the CORS problem that hoerbie and I mentioned.

By the way, it would be an advantage for everyone if you would post executable codes.

Re: Buttongadget and loadsound

Posted: Tue Jul 26, 2022 5:27 pm
by Paul
Working code would look something like this...

https://reelmedia.org/sbtest

Code: Select all

;/ Created with PureVision64 v6.00 x64
;/ Tue, 26 Jul 2022 12:12:13
;/ by Reel Media Productions                      


InitSound()

Declare Loaded(Type, Filename$, ObjectId)
Declare LoadingError(Type, Filename$, ObjectId)
BindEvent(#PB_Event_Loading, @Loaded())
BindEvent(#PB_Event_LoadingError, @LoadingError())

Enumeration 1
  #Window_Main
EndEnumeration

Enumeration 1
  #Gadget_Main_Button
EndEnumeration

Enumeration 1
  #Image_Main_Button
  #Blip
EndEnumeration

;- Load Stuff
LoadImage(#Image_Main_Button,"data/pic.png")
LoadSound(#Blip,"data/blip.mp3")
#LoadedElementsIndex=2



Procedure.i Window_Main()
  If OpenWindow(#Window_Main,0,0,331,197,"Test Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      ButtonImageGadget(#Gadget_Main_Button,185,100,120,65,ImageID(#Image_Main_Button))
      HideWindow(#Window_Main,#False)
    ProcedureReturn WindowID(#Window_Main)
  EndIf
EndProcedure

Procedure CloseWindowEvent()
  WindowID=EventWindow()

  Select WindowID
    Case #Window_Main
      CloseWindow(#Window_Main)
  EndSelect
EndProcedure


;- GadgetEvent Loop
Procedure GadgetEvent()
  GadgetID=EventGadget()

  Select GadgetID
    Case #Gadget_Main_Button
      PlaySound(#Blip)
  EndSelect
EndProcedure


Procedure Loaded(Type, Filename$, ObjectId)
  Static SBLoadedElements

  SBLoadedElements+1
  Debug Str(SBLoadedElements)+": "+Filename$+" loaded"

  If SBLoadedElements=#LoadedElementsIndex
    ;- Main Loop
    If Window_Main()
      BindEvent(#PB_Event_Gadget,@GadgetEvent())
      BindEvent(#PB_Event_CloseWindow,@CloseWindowEvent())
    EndIf
  EndIf
EndProcedure

Procedure LoadingError(Type, Filename$, ObjectId)
  Debug Filename$+": loading error"
EndProcedure