2011-04-15 80 views
0

我在我的应用程序中运行1个后台进程...它连续检查一些输入...当输入正确的输入时发现..它会播放一些wav文件..声音没有在我的应用程序中播放

我已经添加了1个wav填充名为“ding.wav”的资源..

我在应用程序中编写了以下代码... 我正在使用System.Media命名空间。使用.NET 4.0

SoundPlayer player = new SoundPlayer(); 
player.Stream = Properties.Resources.ding; 
player.Play(); 

,但声音不玩....

你能告诉我什么,我做错了.. !! enter image description here

回答

2

试试这个:

SoundPlayer player = new SoundPlayer(Properties.Resources.ding); 
player.Play(); 

你也可以试试这个:

using System; 
using System.Runtime.InteropServices; 
using System.Resources; 
using System.IO; 
namespace Win32 
{ 
    public class Winmm 
    { 
    public const UInt32 SND_ASYNC = 1; 
    public const UInt32 SND_MEMORY = 4; 

    [DllImport("Winmm.dll")] 
    public static extern bool PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags); 
    public Winmm() { } 
    public static void PlayWavResource(string wav) 
    { 
     // get the namespace 
     string strNameSpace= 
     System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString(); 

     // get the resource into a stream 
     Stream str = 
     System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(  strNameSpace +"."+ wav); 
     if (str == null) return; 
    // bring stream into a byte array 
    byte[] bStr = new Byte[str.Length]; 
    str.Read(bStr, 0, (int)str.Length); 
    // play the resource 
    PlaySound(bStr, IntPtr.Zero, SND_ASYNC | SND_MEMORY); 
    } 
    } 
} 
1

我认为你需要做的不止于此。查看this文章。

1

我认为你必须确保你开始玩之前,该文件被加载。

SoundPlayer player = new SoundPlayer(Properties.Resources.ding); 
player.Load(); 
player.Play(); 
相关问题