Explicitly finishing a SoundInterval within a Sequence


I would like to explicitly finish a SoundInterval which is part of a Sequence. But the result is not as expected.
The audio file encapsulated by the SoundInterval is stopped properly but not the SoundInterval itself.

This is the (test) code:

import os
resourceDir = os.path.dirname(os.path.abspath(__file__)) + <INSERT RES DIR HERE>

from pandac.PandaModules import loadPrcFileData
loadPrcFileData('', 'audio-library-name p3openal_audio')
loadPrcFileData('', 'model-path ' + resourceDir)

import direct.directbase.DirectStart

from direct.showbase.DirectObject import DirectObject
from direct.interval.MetaInterval import Sequence
from direct.interval.FunctionInterval import Func
from direct.interval.SoundInterval import SoundInterval

def doPrint(arg):
	print arg

class Test(DirectObject):
	def __init__(self):
		self.seq = Sequence(Func(doPrint, 'start'),
							SoundInterval(loader.loadSfx(<INSERT ANY AUDIO HERE>)),
							Func(doPrint, 'end')
		self.running = False
		self.accept('space', self.onSpace)
	def onSpace(self):
		if not self.running:
			self.running = True
			for ival in self.seq:
				if isinstance(ival, SoundInterval):

t = Test()


In fact, ‘end’ will not be printed until the SoundInterval is finished, no matter if I press ‘space’ (and audio playback stops) or not.

Any ideas? Are we doing something wrong?


To point it up: take

SoundInterval(loader.loadSfx(<ANY AUDIO>), duration = 20) 

-> start Sequence -> press ‘Space’ -> audio playback is stopped almost immediately -> but ‘end’ will only be printed after ~ 20s

Manually advance the time works.
Insert before the finish() line :


You can’t finish a nested interval within a Sequence. You have to finish the parent Sequence directly. Once the Sequence is constructed, you can no longer safely access the component intervals; they’re now controlled entirely by the parent Sequence.

Intervals are intended for static playback only. If you really want this kind of dynamic control, you should consider using the task system instead.


@ynjh_jo: It works fine, thanks for this hint!

@drwr: Thank you, too, for your explanation. I think your advice, better using tasks, will be taken into account asap.