Using Javascript from SpiderBasic
Andy
 
Posts: 17
Joined: Sat Feb 15, 2020 5:19 pm

Need help with Button

by Andy Fri Mar 13, 2020 2:46 pm

Hi, I am trying to load a local audio file to play with webaudio. The below code works but i want to attach it to a spiderbasic button.

Code: Select all
! $('body').append('<input type="file" id="fileInput"><br>')
 ! fileInput.onchange = function(e) {
 ! obj = URL.createObjectURL(this.files[0])
 ! }
User avatar
Peter
 
Posts: 689
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080

Re: Need help with Button

by Peter Fri Mar 13, 2020 7:52 pm

Code: Select all
EnableExplicit

Procedure ReadCallback(Status, Filename.s, File, Size)
 
  If Status = #PB_Status_Loaded
   
    Debug "File: " + Filename + " - Size: " + Size + " bytes"
   
    ! audio_player.src = URL.createObjectURL(spider.file.objects.Get(v_file).localFile);
    ! audio_player.play();
   
    CloseFile(0)
   
  ElseIf Status = #PB_Status_Error
   
    Debug "Error when loading the file: " + Filename
   
  EndIf
 
EndProcedure

Procedure OpenFileRequesterCallback()
  If NextSelectedFile()
    OpenFile(0, SelectedFileID(), @ReadCallback(), #PB_LocalFile)
  EndIf
EndProcedure

Procedure ChooseFileEvent()
  OpenFileRequester("*.mp3", @OpenFileRequesterCallback())
EndProcedure

Define HTML.s

HTML = "<audio id='audio_player' />"

! $("body").append(v_html);

OpenWindow(0, 0, 0, 300, 50, "Read file example", #PB_Window_ScreenCentered)

ButtonGadget(0, 10, 10, 280, 30, "Choose a file...")

BindGadgetEvent(0, @ChooseFileEvent())
Andy
 
Posts: 17
Joined: Sat Feb 15, 2020 5:19 pm

Re: Need help with Button

by Andy Sat Mar 14, 2020 8:27 am

Thanks Peter
Return to Javascript

Who is online

Users browsing this forum: No registered users and 2 guests