首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NAudio,windows服务,多线程

NAudio,windows服务,多线程
EN

Stack Overflow用户
提问于 2015-03-22 21:23:54
回答 1查看 616关注 0票数 1

我有windows服务,通过*.wav播放NAudio.dll文件(多线程)

代码语言:javascript
复制
    public static void PlayFileBackground(string fileName)
    {
        Task.Run(() => { PlayFile(fileName); });
    }

    private static void PlayFile(string fileName)
    {
        if (File.Exists(fileName) == true)
        {
            try
            {
                var manualResetEvent = new ManualResetEvent(false);
                using (var wfr = new WaveFileReader(fileName))
                using (WaveChannel32 wc = new WaveChannel32(wfr) { Volume = 1, PadWithZeroes = false })
                using (var audioOutput = new DirectSoundOut())
                {
#region play
                    audioOutput.Init(wc);                        
                    audioOutput.Play();
                    audioOutput.PlaybackStopped += (sender, e) => { manualResetEvent.Set(); }; 
                    manualResetEvent.WaitOne();
                    audioOutput.Stop();
#endregion
                }
            }
            catch (Exception exception)
            {                
                logger.Error("Error NAudio.\n{0}", exception.ToString()); //<-never don't catch
            }
        }
    }

如果该区域的“play”注释为->,则所有工作

如果区域‘播放’未注释的->,有时我的服务停止,没有任何错误。

我尝试使用调试器附加:在NAudio.dll中出现了一个未处理的‘NAudio.dll’类型的异常。

另外:System.AccessViolationException类型的第一次例外发生在NAudio.dll中

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-23 08:03:11

我找到了替换DirectSoundOut > WaveOutEvent的建议。现在一切都正常了。谢谢你,斯塔德。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29200032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档