Why does this Playsound code fail?
BlitzMax Forums/BlitzMax Programming/Why does this Playsound code fail?
| ||
Hiya, the code below runs but no sound is made when space is pressed. If you change the playsound to be PlaySound(Bang) i.e not using a channel it's fine, except you get many of the same sound on different channels. If you change the play sound to be ChannelBang = PlaySound(Bang) it works, but aren't I just making a new TChannel eveytime Space is pressed and isn't this wasteful? Finally, if you change the play sound to be ChannelBang = PlaySound(Bang,ChannelBang), the first time you press space there is no sound! Then it works after that. Note that the channel is reused so you do not get many of the same sound. What's going on? All I can guess is that the channel made at the start with New is somehow not set or ready to play sounds and needs to be configured first somehow, but how? Thanks in advance for any info. Graphics 800,600,0 'test variables Global Bang:TSound = LoadSound("explosion.wav",False) Global ChannelBang:TChannel = New TChannel Repeat Cls If KeyHit(KEY_SPACE) Then PlaySound(Bang,ChannelBang) EndIf Flip Until KeyHit(KEY_ESCAPE) |
| ||
To avoid this, I always create the Bank when the sound needs to be played. I don't know if it's the best way to do it, but it works.Graphics 800,600,0 'test variables Global Bang:TSound = LoadSound("explosion.wav",False) Global ChannelBang:TChannel Repeat Cls If KeyHit(KEY_SPACE) Then if channelbang = null then channelbang = PlaySound(Bang) else if channelbang.IsPlaying() then channelbang.Stop() channelbang = PlaySound(Bang) endIf EndIf Flip Until KeyHit(KEY_ESCAPE) |
| ||
yeah OK, thanks, that's a good method. Still don't understand why my method doesn't work. Also everytime Playsound is called, whatever channelband was pointing at is still in the memory. This would be a memory hole in a language without auto garbage collection, so when exactly does Max decide to clean up these channels? As soon as the sound stops playing, never? |
| ||
I don't know... good question... |
| ||
I think the error is in the following lineGlobal ChannelBang:TChannel =New TChannel It should be Global ChannelBang:TChannel = AllocChannel() |
| ||
aha now that's interestijg, I'll try it out. Thanks! |