2008-10-15 42 views
4

我使用p/invoke方法来播放wav声音。有些方法可以调用Windows媒体播放器来播放mp3声音,但其缓慢的操作很麻烦。有没有一个干净的方式来播放紧凑的框架中的MP3声音3.5

有没有简单的方法来播放短文件mp3

这主要是应用提示声音提示当您不看屏幕,而不是音乐。

谢谢!

+0

您可以用soun播放.wav dplayer类,不需要使用p/invoke。我不知道对于MP3 ... – pmlarocque 2008-10-15 12:49:18

回答

5

CF框架3.5现在包括用于播放.WAV文件支持:

Namespace System.Media.SoundPlayer

短WAV文件提示和声音效果甚至可能起到更快比的MP3,因为他们是“准备 - 播放” ...

14

如果你正在寻找一种方式来播放MP3:

 
public class Sound 
{ 
    [DllImport("aygshell.dll", SetLastError = true)] 
    private static extern IntPtr SndPlaySync(string pszSoundFile, uint dwFlags); 
    [DllImport("aygshell.dll")] 
    private static extern uint SndOpen(string pszSoundFile, ref IntPtr phSound); 
    [DllImport("aygshell.dll")] 
    private static extern uint SndPlayAsync(IntPtr hSound, uint dwFlags); 
    [DllImport("aygshell.dll")] 
    private static extern uint SndClose(IntPtr hSound); 
    [DllImport("aygshell.dll")] 
    private static extern uint SndStop(int SoundScope, IntPtr hSound); 

    const int SND_SCOPE_PROCESS = 0x1; 
    private static Random _random = new Random(); 

    // init startup path... where you'll hold temp mp3s 
    private static string _startupPath; 
    public static string StartupPath 
    { 
     get { return Sound._startupPath; } 
     set { Sound._startupPath = value; } 
    } 

    private enum SND 
    { 
     SYNC = 0x0000, 
     ASYNC = 0x0001, 
     NODEFAULT = 0x0002, 
     MEMORY = 0x0004, 
     LOOP = 0x0008, 
     NOSTOP = 0x0010, 
     NOWAIT = 0x00002000, 
     ALIAS = 0x00010000, 
     ALIAS_ID = 0x00110000, 
     FILENAME = 0x00020000, 
     RESOURCE = 0x00040004 
    } 

    public static void PlaySound(string fileName) 
    { 
     PlaySound(fileName, null); 
    } 

    public static void PlaySound(string fileName, WaitCallback callback) 
    { 
     SndStop(SND_SCOPE_PROCESS, IntPtr.Zero); 
     ThreadPool.QueueUserWorkItem(playSoundProcess, 
      new object[] {fileName, callback }); 
    } 

    private static void playSoundProcess(object o) 
    { 
     object[] par = (object[])o; 
     string fileName = (string)par[0]; 
     WaitCallback callback = (WaitCallback)par[1]; 
     SndPlaySync(fileName, 0); 

     try 
     { 
      File.Delete(fileName); 
     } 
     catch 
     { } 

     if (callback != null) 
      callback.Invoke(fileName); 
    } 

    public static void ClearSounds() 
    { 
     SndStop(SND_SCOPE_PROCESS, IntPtr.Zero); 
     try 
     { 
      string[] oldFiles = Directory.GetFiles(StartupPath, "*.mp3"); 
      foreach (string f in oldFiles) 
       File.Delete(f); 
     } 
     catch 
     { } 
    } 



    public static void PlaySound(byte[] mp3, WaitCallback callback) 
    { 
     string temp = string.Format("{0}\\{1}-{2}.mp3", StartupPath, DateTime.Now.Ticks, _random.Next()); 
     using (FileStream fs = File.Create(temp)) 
     { 
      fs.Write(mp3, 0, mp3.Length); 
     } 

     PlaySound(temp, callback); 
    } 
} 
+3

我不得不修改此代码,所以它不会删除我的MP3文件,但除此之外,它修复奇迹后,它已被修复。 – Alex 2010-03-26 07:57:28

相关问题