开发者

C# XNA: stopping and playing Cue (sound) objects

开发者 https://www.devze.com 2022-12-19 16:42 出处:网络
I am trying to stop a Cue, then play it again. This code is in the Update() loop: Cue torpedoSound = soundBank.GetCue(\"torpedo\");

I am trying to stop a Cue, then play it again. This code is in the Update() loop:

Cue torpedoSound = soundBank.GetCue("torpedo");
tor开发者_Python百科pedoSound.Stop(AudioStopOptions.AsAuthored);
torpedoSound.Play(); // Invalid Operation Exception

However, I always get an InvalidOperationException at the above location. The documentation says that will occur if Play() is called on a Cue for which isPlaying is true. But if I call Stop() immediately before, how can this be?

This works fine:

soundBank.PlayCue("torpedo");

Placing a call to Stop() immediately after that allows the sound to play anyway, which surprises me:

soundBank.PlayCue("torpedo");
torpedoSound.Stop(AudioStopOptions.Immediate);

Shouldn't this make it never be audible to the user?

Additionally, when the sound is already playing, calling Stop() fails to stop it.

I'm new to C#/XNA.


Once a Cue is stopped it can no longer be used. You have to get a new Cue instance from the audio engine.

Refer to this sample: http://msdn.microsoft.com/en-us/library/dd940205.aspx

Regarding the stopping, the Cue doesn't immediately stop when you call Stop. It can take a frame or two, or even more if there's an ending transition defined.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号