Monday, February 6, 2012

[Csharp] Play Audio use winmm.dll

Today, I really early go to my company. It's a nice day.

In this post, I want show you a class C# to play audio use API winmm.dll, here is code snippet:



   1:      public class AudioPlayHelper
   2:      {
   3:          [DllImport("winmm.dll")]
   4:          private static extern long mciSendString(
   5:              string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
   6:   
   7:          #region Play Audio
   8:   
   9:          public static void Play(string fileName, bool repeat)
  10:          {
  11:              mciSendString("open \"" + fileName + "\" type mpegvideo alias MediaFile",
  12:                          null, 0, IntPtr.Zero);
  13:   
  14:              mciSendString("play MediaFile" + (repeat ? " repeat" : String.Empty), null,
  15:                            0, IntPtr.Zero);
  16:          }
  17:   
  18:          public static void Play(byte[] embeddedResource, bool repeat)
  19:          {
  20:              ExtractResource(embeddedResource, Path.GetTempPath() + "resource.tmp");
  21:              mciSendString("open \"" + Path.GetTempPath() + "resource.tmp" +
  22:                            "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
  23:              mciSendString("play MediaFile" + (repeat ? " repeat" : String.Empty),
  24:                            null, 0, IntPtr.Zero);
  25:          }
  26:   
  27:          public static void Pause()
  28:          {
  29:              mciSendString("stop MediaFile", null, 0, IntPtr.Zero);
  30:          }
  31:   
  32:          public static void Stop()
  33:          {
  34:              mciSendString("close MediaFile", null, 0, IntPtr.Zero);
  35:          }
  36:   
  37:          private static void ExtractResource(IEnumerable<byte> res, string filePath)
  38:          {
  39:              if (File.Exists(filePath)) return;
  40:   
  41:              var fs = new FileStream(filePath, FileMode.OpenOrCreate);
  42:              var bw = new BinaryWriter(fs);
  43:   
  44:              foreach (var b in res) bw.Write(b);
  45:   
  46:              bw.Close();
  47:              fs.Close();
  48:          }
  49:   
  50:          #endregion
  51:   
  52:          #region Record
  53:   
  54:          public static void Record()
  55:          {
  56:              mciSendString("open new type waveaudio alias MediaFile", null, 0, IntPtr.Zero);//cannot record mp3
  57:              mciSendString("record MediaFile", null, 0, IntPtr.Zero);
  58:          }
  59:   
  60:          public static void Save(string fileName)
  61:          {
  62:              mciSendString("save MediaFile \"" + fileName + "\"", null, 0, IntPtr.Zero); //extension .wav
  63:              mciSendString("close MediaFile ", null, 0, IntPtr.Zero);
  64:          }
  65:   
  66:          #endregion
  67:      }
Before add this code you need using System.Runtime.InteropServices;
This application will be play mp3 sound and record wav sound by easy call static function.
Hope you enjoy it!

No comments:

Post a Comment