2008-10-15 4 views
4

J'utilise la méthode p/invoke pour jouer wav sons. Il existe des moyens d'appeler Windows Media Player pour jouer un son mp3, mais son lent & lourd.est-il un moyen propre de jouer un son mp3 dans le cadre compact 3.5

Existe-t-il un moyen facile de lire un fichier court mp3?

Ceci est principalement pour l'application incitant et signaux sonores lorsque vous ne cherchez pas à l'écran et non la musique.

Merci!

+0

vous pouvez jouer .wav avec le soun classe dplayer, pas besoin d'utiliser p/invoke. Je ne sais pas pour le mp3 ... – pmlarocque

Répondre

5

Framework CF 3.5 prend désormais en charge la lecture des fichiers .WAV:

Namespace System.Media.SoundPlayer

fichiers WAV court pour recueillir des indices et des effets sonores pourraient même jouer plus vite que les MP3, car ils sont « prêts à : permet de lire » ...

14

Si vous êtes à la recherche d'un moyen de jouer: 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

J'ai dû modifier un peu ce code pour qu'il ne supprime pas mes fichiers mp3, mais à part ça, ça marche à merveille une fois qu'il a été corrigé. – Alex

+0

J'utilisais principalement la surcharge PlaySound (byte [], WaitCallback), c'est pourquoi la suppression était en place ... bon point cependant ... – kape123

+0

Un exemple plus simple de [Lecture de MP3 et WMA en utilisant le .Net Compact Framework] (http : //www.koushikdutta.com/2008/07/playing-mp3s-and-wmas-using-net-compact.html). – Trisped

Questions connexes