我使用soundeffect来播放来自字节数组的声音并且一切正常,但我不能停止播放声音,因为Soundeffect中没有名为stop()的方法,我怎么能阻止它?
代码:
private void playFile(byte[] file)
{
try
{
if (file == null || file.Length == 0) return;
SoundEffect se = new SoundEffect(file, microphone.SampleRate, AudioChannels.Mono);
SoundEffect.MasterVolume = 0.7f;
if (stop != 1)
{
FrameworkDispatcher.Update();
se.Play();
}
else
{
//Here should stop, how !
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
答案 0 :(得分:0)
您是否尝试在.Dispose()
对象上调用SoundEffect
?根据{{3}},
当SoundEffect对象被销毁时,所有SoundEffectInstance 之前由SoundEffect 创建的对象将停止播放和 变得无效。
此外,documentation使用MediaElement
代替,除非您使用XNA Game Studio 4.0进行开发。因此,如果这适用于您,您可以考虑使用 拥有Microsoft recommends的MediaElement
。
答案 1 :(得分:0)
尝试使用SoundEffectInstance
这是解决方案的链接
SoundEffectInstance
以上IDisposeable
实施dispose()
,因此它会有stop()
方法以及{{1}}音效。
答案 2 :(得分:0)
为SoundEffectInstance
创建一个变量,如下所示
SoundEffectInstance soundEffectInstance;
然后在加载文件后创建soundEffect
的实例..
soundEffectInstance = se.CreateInstance();
之后使用此soundEffectInstance
到Stop
,Play
或Pause
您想要的特定范围。
soundEffectInstance.Play();
请注意,SoundEffect
用于fire和forget类型实例,如果您想控制SoundEffect
的任何特定实例,则需要使用SoundEffectInstance
。
我同意Benoit Catherinet的观点。如果您试图在某个事件上停止特定声音,那么您必须将停止代码放在该事件方法中,并且您的SoundEffect实例必须是全局的而不是本地的。
如果您能解释您想要实现的目标,我们可以更好地帮助您。