2011-04-15 113 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(); 
相關問題