Need help with Button

Using Javascript from SpiderBasic
Andy
Posts: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Need help with Button

Post by Andy »

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: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Need help with Button

Post by Peter »

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: 46
Joined: Sat Feb 15, 2020 5:19 pm
Location: Larnaca, Cyprus
Contact:

Re: Need help with Button

Post by Andy »

Thanks Peter
Post Reply