从Windows服务(C#)播放波形文件 [英] Play wave file from a Windows Service (C#)

查看:244
本文介绍了从Windows服务(C#)播放波形文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从C#应用程序的运行为Windows服务中发挥wav文件。我曾经尝试都System.Media.SoundPlayer和P / Invoke调用到WINMM.DLL(这可能是什么声音播放正在做)。

I need to play a wav file from a C# application running as a Windows Service. I have tried both System.Media.SoundPlayer and a P/Invoke call to WinMM.dll (which is probably what SoundPlayer is doing).

[DllImport("WinMM.dll")]
private static extern bool PlaySound(string fname, int Mod, int flag); 

如果我跑我的code作为控制台应用程序,声音播放。当我从一个服务,没有运气运行它,我想我并不感到惊讶。

If I run my code as a console application, the sounds play. When I run it from a service, no luck, and I guess I'm not surprised.

那么,有没有打从一个窗口服务完善的一种方式?会像DirectSound的帮助吗?或者我会被卡住写一个控制台应用程序,并具有Windows服务的应用程序与它作为中介沟通?

So is there a way to play a sound from a windows service? Would something like DirectSound help? Or am I going to be stuck writing a console application and having the windows service app communicate with it as an intermediary?

在此先感谢

推荐答案

从服务播放wav文件是绝对有可能的,至少在Windows 7(和最有可能的Vista系统),通过使用Windows核心音频的A​​PI。我最近用做一个小的测试服务,验证了这一点 n音讯。我刚刚下载的n音讯来源,并从他们NAudioDemo项目复制WSAPI部分。这是在Windows 7企业版64位,但我不认为事情。该服务使用本地系统帐户。结果
为了记录在案,从服务播放声音是在嵌入式环境做一个完全合法的事情。

Playing a wav file from a service is definitely possible, at least on Windows 7 (and most likely Vista), by using the Windows Core Audio APIs. I recently verified this by making a small test service using NAudio. I just downloaded the NAudio sources and copied the "Wsapi" parts from their NAudioDemo project. This was on Windows 7 Enterprise 64bit, but I don't think that matters. The service was using the LocalSystem account.
For the record, playing sounds from a service is a perfectly legitimate thing to do in an embedded setting.

这篇关于从Windows服务(C#)播放波形文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆