BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"ChannelPlaying and PlaySound bug"
, by thechange
When using ChannelPlaying and PlaySound in a certain combination, ChannelPlaying will return invalid information, making it impossible to control sounds playing simultaneously.
Code
;------------------------------------------------------------------------------; ; This bug was first discovered when running the simple program below. There are ; 2 different sounds used, called '1.WAV' and '2.WAV'. When running this program ; it should be able to play 2 sounds simultaneously at all times. But at some ; point, when fiddling with the keys, only 1 sound will be played at a time. ; Hold '1' and/or '2' to play the sounds and press 'Esc' to stop. Graphics 640,480 Sound1 = LoadSound( "1.WAV" ) Sound2 = LoadSound( "2.WAV" ) Repeat If KeyDown( 1 ) Then End If KeyDown( 2 ) Then If Not ChannelPlaying( Sound1Channel ) Then Sound1Channel = PlaySound( Sound1 ) End If End If If KeyDown( 3 ) Then If Not ChannelPlaying( Sound2Channel ) Then Sound2Channel = PlaySound( Sound2 ) End If End If Forever End ; The working of the above program should be pretty straight-forward. I've been ; able to solve the problem by separating the ChannelPlaying() calls from the ; PlaySound() calls. The result is displayed below. Graphics 640,480 Sound1 = LoadSound( "1.WAV" ) Sound2 = LoadSound( "2.WAV" ) Repeat If KeyDown( 1 ) Then End If KeyDown( 2 ) Then If Not ChannelPlaying( Sound1Channel ) PlaySound1 = True EndIf EndIf If KeyDown( 3 ) Then If Not ChannelPlaying( Sound2Channel ) PlaySound2 = True EndIf EndIf If PlaySound1 PlaySound1 = False Sound1Channel = PlaySound( Sound1 ) EndIf If PlaySound2 PlaySound2 = False Sound2Channel = PlaySound( Sound2 ) EndIf Forever End ; As you can see I used an extra (security) step before actually playing the ; specific sound. Another solution to this problem is by using a special sound ; wrapper function which is also listed in the sound/music code database named ; 'Enhanced PlaySound()'. This routine has no background CPU processing so is ; not a CPU intensive method. However another feasible solution is the one below ; which *is* a CPU intensive method, checking if a sound has finished playing ; every cycle. Graphics 640,480 Sound1 = LoadSound( "1.WAV" ) Sound2 = LoadSound( "2.WAV" ) Repeat If KeyDown( 1 ) Then End If Sound1Channel If Not ChannelPlaying( Sound1Channel ) Sound1Channel = False EndIf Else If KeyDown( 2 ) Then Sound1Channel = PlaySound( Sound1 ) EndIf EndIf If Sound2Channel If Not ChannelPlaying( Sound2Channel ) Sound2Channel = False EndIf Else If KeyDown( 3 ) Then Sound2Channel = PlaySound( Sound2 ) EndIf EndIf Forever End ; If you experience any problems playing sound, similar to the display problem ; above, I'd highly recommend using the enhanced PlaySound() function listed in ; the sound/music code database. ; There are numerous ways to separate the ChannelPlaying() from the PlaySound() ; command. This problem doesn't seem common so most people are able to make this ; separation by nature. ; The bug seems to appear according to either the order in which the ; ChannelPlaying() and PlaySound() functions are called or the time between the ; calls. I am not able to put my finger on the exact problem. If you experience ; any peculiar problems, perhaps even completely new ones, don't be affraid to ; let me know :) ; Have fun :D ;------------------------------------------------------------------------------;
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations