Blitz3D+ Command Reference

ChannelPlaying ( channel )

Parameters

channel - channel handle returned by PlaySound or PlayMusic

Description

Returns True while a channel still has a sound on it, False once it has finished or been stopped.

This is how a game knows a sound has run its course. Poll the channel of your background track and start the next one when it comes back False, so a playlist advances by itself. Test before playing a voice line or an alarm so you do not trigger a second copy on top of the first. Check whether an object's looping sound is still alive before deciding whether to restart it.

It is a Channel command and needs the handle you kept from PlaySound or PlayMusic; there is no way to ask a loaded sound whether any of its playbacks are running.

One thing to watch: a channel paused with PauseChannel still reports True, because it is holding its place rather than having ended. Treat this as "does this channel still exist" rather than "is sound coming out right now", and track paused state yourself if you need to tell the difference. A channel stopped with StopChannel reports False.

A looping sound never returns False on its own - that is the point of a loop - so stop it explicitly when you are done with it.

See also: PlaySound, StopChannel, PauseChannel, ResumeChannel, PlayMusic.

Example

; ChannelPlaying Example
; ----------------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; Load a short explosion - long enough to watch the status lamp
snd=LoadSound("media/boom.wav")

channel=0

While Not KeyDown(1)

    ; Space fires the explosion on a fresh channel
    If KeyHit(57) Then channel=PlaySound(snd)

    ; ChannelPlaying returns 1 while the channel is still sounding,
    ; and 0 once it has finished (or was never started)
    playing=ChannelPlaying(channel)

    Cls

    ; Status lamp: lit while the explosion is audible
    If playing Then
        Color 80,255,80
        Oval 296,190,48,48,True
    Else
        Color 70,70,70
        Oval 296,190,48,48,False
    End If

    Color 255,255,255
    Text 320,260,"ChannelPlaying(channel) = "+playing,True

    Text 0,0,"Space: play explosion, then watch the lamp go out   Esc: exit"

    Flip

Wend

End

Index