Page 1 of 1

LoadSound + PlaySound (?)

Posted: Wed Dec 28, 2022 11:08 pm
by AZJIO
I have the following code (cut from the main code). MessageRequester("alarm") works for me, but file playback does not work. It also doesn't throw any errors (LoadingError). Any ideas?

Code: Select all

#Sound = 0
#Timer = 0
Global flgAlarm = 1, flgSound

Procedure Loading(Type, Filename$, ObjectId)
; 	Debug Filename$ + " loaded (id = " + ObjectId + ")"
	flgSound = 1
EndProcedure

Procedure LoadingError(Type, Filename$)
	flgSound = 0
	MessageRequester(Filename$ + ": loading error")
EndProcedure

Procedure TimerEvents()
	Select EventTimer()
		Case #Timer
			If flgSound
				LoadSound(#Sound, "/strogate/emulated/0/Alarms/1.wav")
				PlaySound(#Sound)
			EndIf
			MessageRequester("alarm")
			FreeSound(#Sound)
; 			GetNextAlarm()
	EndSelect
EndProcedure

; GUI

If flgAlarm
	InitSound()
EndIf

If flgAlarm
	BindEvent(#PB_Event_Timer, @TimerEvents())
	BindEvent(#PB_Event_Loading, @Loading())
	BindEvent(#PB_Event_LoadingError, @LoadingError())
EndIf

Re: LoadSound + PlaySound (?)

Posted: Thu Dec 29, 2022 2:40 pm
by Peter
You cannot use the sound until the "Loading" callback has been called.

Try to load all resources directly at the start of the program.

Something like this:

Code: Select all

If flgAlarm
  
  BindEvent(#PB_Event_Timer, @TimerEvents())
  BindEvent(#PB_Event_Loading, @Loading())
  BindEvent(#PB_Event_LoadingError, @LoadingError())
  
  LoadSound(#Sound, "/strogate/emulated/0/Alarms/1.wav")
  
EndIf

Re: LoadSound + PlaySound (?)

Posted: Fri Dec 30, 2022 5:22 am
by AZJIO
I realized my mistake. The flgSound flag is set to 1 after the LoadSound() function is executed. That is, the flag is initially 0 and the LoadSound() function is not executed. I redid it and I get an error opening the file LoadingError().

For testing, I need to open the file at once so I don't have to wait for the alarm to go off.

I can't figure out how to specify the file path. If I use OpenFileRequester(), I still don't get the file path and can't use that with LoadSound()

Code: Select all

#Sound = 0
InitSound()

Procedure Loading(Type, Filename$, ObjectId)
; 	Debug Filename$ + " loaded (id = " + ObjectId + ")"
EndProcedure

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

; LoadSound(#Sound, "C:\sb\1.wav")
If LoadSound(#Sound, "/strogate/emulated/0/Alarms/1.wav"); Or LoadSound(#Sound, "/Data/Alarms/1.wav")
; If LoadSound(#Sound, "/Data/Alarms/1.wav")
; If LoadSound(#Sound, "/Alarms/1.wav")
	PlaySound(#Sound)
EndIf
MessageRequester("alarm") ; message blocks the operation of the code, so using it to play back
FreeSound(#Sound)

Re: LoadSound + PlaySound (?)

Posted: Fri Dec 30, 2022 8:14 am
by useful
https://www.spiderbasic.com/documentati ... nd.sb.html
The example super affordably illustrates the asynchronous nature of SB(JS).
PlaySound can only be called as a response to an event and nothing else.

Re: LoadSound + PlaySound (?)

Posted: Fri Dec 30, 2022 8:52 am
by AZJIO
In my case there are events

Code: Select all

BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
I already posted the source

tested the suggested code with 3 file paths, doesn't work

Re: LoadSound + PlaySound (?)

Posted: Fri Dec 30, 2022 11:42 am
by useful
Resource directory
The path of the resource directory where all the app assets belongs to. It can be only one resource directory specified. The whole directory will be automatically copied to the app root directory.
in apk (If you unzip )
assets\www\ -->
I, for example, chose the name of the folder "s" in which I put
lazer.wav
siren.ogg
and
...
LoadSound(0, "./s/lazer.wav")
LoadSound(1, "./s/siren.ogg")
i.e. the path to the resources available to the loadxxx functions
within the apk
/assets/www/s/lazer.wav
/assets/www/s/siren.ogg
And in sb
LoadSound(0, "./s/lazer.wav")
LoadSound(1, "./s/siren.ogg")