oh great, now i feel like an idiot!

Blitz3D Forums/Blitz3D Beginners Area/oh great, now i feel like an idiot!

chwaga(Posted 2007) [#1]
I've been stumped by something that should be simple, something that was very simple on a Pygame engine (python made graphic).

How would i go about looping the same sound while a key is held down? I can get it to play, but unfortunately, it bombs it about 1000 times a second, every time the loop processes. Ouch.

I can also get it to loop once the key is hit, but I can't STOP looping it, once the key is no longer held down. I'm afraid to use the "freesound" command, cause loading and freeing a sound every time a key is held, could really bog up the harddrive.

Any help is appreciated. Thanks :D


puki(Posted 2007) [#2]
You need to use the SoundChannel commands.


Sledge(Posted 2007) [#3]
mySoundPlaying=False
mySound=LoadSound("mySample.wav")
LoopSound mySound

While Not KeyHit(1)
	If KeyDown(57); SPACE KEY
		If mySoundPlaying=False
			myChannel=PlaySound(mySound)
			mySoundPlaying=True
		EndIf	
	Else
		StopChannel myChannel
		mySoundPlaying=False
	EndIf
Wend

FreeSound mySound



chwaga(Posted 2007) [#4]
great! at first it didn't work, so i went around tinkering with a few things, i'm not sure what i did, but it worked! thanks!


Sledge(Posted 2007) [#5]
great! at first it didn't work

That's odd -- it should just need LoadSound pointing towards a legitimate wav file. No matter... glad you're sorted.


GfK(Posted 2007) [#6]
Why not use ChannelPlaying() instead of using a separate var:
mySound=LoadSound("mySample.wav")
LoopSound mySound

While Not KeyHit(1)
	If KeyDown(57); SPACE KEY
		If Not ChannelPlaying(myChannel)
			myChannel=PlaySound(mySound)
		EndIf	
	Else
		If ChannelPlaying(myChannel)
			StopChannel myChannel
		EndIf
	EndIf
Wend

FreeSound mySound



Sledge(Posted 2007) [#7]
Why not use ChannelPlaying() instead of using a separate var

Because he might eventually want to restrict several sounds to the same channel and manage their priority manually.