Buttongadget and loadsound

Just starting out? Need help? Post your questions and find answers here.
Stefan
Posts: 160
Joined: Mon Feb 05, 2018 9:44 pm

Re: Buttongadget and loadsound

Post by Stefan »

Here is working code that describes the problem.

Code: Select all




Global homepage.s,pic1,pic2,aSound,testlisticongadget
;homepage="https://doko-lounge.de/"
homepage=""

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

  
  ; Register the loading event before calling any resource load command
  BindEvent(#PB_Event_Loading, @Loading())
  BindEvent(#PB_Event_LoadingError, @LoadingError())
  
 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+"freuen.png") 
  
  
  Procedure Loading(Type, Filename$, ObjectId)
    Debug Filename$ + " loaded (id = " + ObjectId + ")"
    
    If Filename$=homepage+"freuen.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
  
 




User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Buttongadget and loadsound

Post by Paul »

Did you check the developer console like Peter suggested and see if you are being blocked for security reasons? (linking to external resources)
Stefan
Posts: 160
Joined: Mon Feb 05, 2018 9:44 pm

Re: Buttongadget and loadsound

Post by Stefan »

The sound is blocked, I already wrote. The images are loaded. Hence my question, why does a loaded image work in an imagegadget, but not in a buttonimagegadget.
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Buttongadget and loadsound

Post by Paul »

Updating my example code to reflect your layout and placing the resources in the proper folders, I'm seeing everything work as expected.
https://reelmedia.org/sbtest/indexb.html

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())


#Window_Main=1
Global pic1,asound,testlisticongadget,testbutton

;- Load Stuff
Global pic1=LoadImage(#PB_Any,"data/pic.png")
Global asound=LoadSound(#PB_Any,"data/blip.wav")
#LoadedElementsIndex=2



Procedure.i Window_Main()
  If OpenWindow(#Window_Main,0,0,331,197,"Test Window",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
      testlisticongadget=ListIconGadget(#PB_Any, 10,100, 290, 90, "picture", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
      AddGadgetItem(testlisticongadget, 1, "Picture 1", ImageID(pic1))
      ImageGadget(#PB_Any,10,10,ImageWidth(pic1),ImageHeight(pic1),ImageID(pic1))
      testbutton=ButtonImageGadget(#PB_Any,10,50,ImageWidth(pic1),ImageHeight(pic1),ImageID(pic1))
    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 testbutton
      PlaySound(asound)
  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
Stefan
Posts: 160
Joined: Mon Feb 05, 2018 9:44 pm

Re: Buttongadget and loadsound

Post by Stefan »

Maybe you didn't understand the problem, Paul?
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Buttongadget and loadsound

Post by Paul »

Stefan wrote: Tue Jul 26, 2022 9:55 pm Maybe you didn't understand the problem, Paul?
I guess I did not ;)

I was trying to show that if you code it right and have the files in the right place, everything will work fine.
If you insist on cross linking resources, you will cause browser (CORS) errors and whatever else behind the scenes that will cause your app to break. Forms and gadgets may fail or not display correctly, things could become unresponsive, a gadget might display an image and then the browser stops running the script due to the internal errors before the next gadget gets a chance to display properly. Any number of issues.

Anyway, best of luck to you.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Buttongadget and loadsound

Post by Peter »

@Stefan:

ButtonImageGadget() and ListIconGadget() internally use the canvas command toDataURL(). This fails if CORS is missing.

The error message is: Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

(You will see this error message in the Developer Console, as I mentioned earlier).

The ImageGadget does not use toDataURL() but drawImage(), which seems to work.

For this reason, the image is displayed in the ImageGadget but not in the ButtonImageGadget or ListIconGadget.
Stefan
Posts: 160
Joined: Mon Feb 05, 2018 9:44 pm

Re: Buttongadget and loadsound

Post by Stefan »

Very nice explanations, Peter and Paul, thank you very much!
Post Reply