gpt4 book ai didi

c# - 为什么我的 WPF 应用程序中的声音会中断?

转载 作者:行者123 更新时间:2023-11-30 14:00:58 25 4
gpt4 key购买 nike

所以这很奇怪...在我的 C#、WPF 应用程序中,我有一个按钮,单击该按钮会播放一段充当帮助文本的简短音频剪辑。大约 50% 的时间它工作得很好……另外 50% 的时间它被切断了,我不明白为什么。甚至它被切断的点也各不相同。

我想,也许,代码超出了范围之类的,然后停止了,所以我把它放在另一个线程中……根本不起作用……所以现在我不确定该怎么做.

这是我的按钮点击事件的代码。

try
{
string path = System.IO.Path.Combine(ConfigurationManager.AppSettings["MediaPath"], Global.Language);
path = System.IO.Path.Combine(path, "visitorTypes.mp3");

if (Global.PlaySound)
{
string p = path.ToString();
MediaPlayer mplayer = new MediaPlayer();
mplayer.Open(new Uri(p, UriKind.Absolute));
mplayer.Play();
}

//a bunch of other code

}
catch (Exception ex)
{
Util.HandleError(ex);
}

想法?

最佳答案

可能是 mplayer 正在被垃圾回收,因为它具有本地作用域。尝试使媒体播放器对象成为具有应用程序生命周期的类的成员变量。

关于c# - 为什么我的 WPF 应用程序中的声音会中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9471631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com