LoadSound + PlaySound (?)

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Posts: 73
Joined: Wed Dec 14, 2022 1:13 pm

LoadSound + PlaySound (?)

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

Re: LoadSound + PlaySound (?)

Post 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
AZJIO
Posts: 73
Joined: Wed Dec 14, 2022 1:13 pm

Re: LoadSound + PlaySound (?)

Post 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)
User avatar
useful
Posts: 116
Joined: Tue Feb 25, 2014 1:15 pm

Re: LoadSound + PlaySound (?)

Post 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.
2B or not 2B = FF
AZJIO
Posts: 73
Joined: Wed Dec 14, 2022 1:13 pm

Re: LoadSound + PlaySound (?)

Post 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
User avatar
useful
Posts: 116
Joined: Tue Feb 25, 2014 1:15 pm

Re: LoadSound + PlaySound (?)

Post 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")
2B or not 2B = FF
Post Reply