Advertisement
Manu404

MyBasciAudioPlayer

May 21st, 2011
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. ///
  2. /// Written by Istace Emmanuel
  3. ///
  4. /// More info : http://istacee.wordpress.com/
  5. /// Topic : http://istacee.wordpress.com/2011/05/22/dotnet-mci-wrapper-basic-audio-player-c/
  6. ///
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Runtime.InteropServices;
  13.  
  14. namespace AudioPlayer
  15. {
  16.     public class MyBasicAudioPlayer
  17.     {
  18.         /// <summary>
  19.         ///  Provide you a basic audio player based on MMI
  20.         /// </summary>
  21.         [DllImport("winmm.dll")]
  22.         private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
  23.  
  24.         private string _command;
  25.  
  26.         /// <summary>
  27.         /// Return true if a file is open, false if not
  28.         /// </summary>
  29.         public bool isOpen { get; internal set; }
  30.  
  31.         /// <summary>
  32.         /// Return true if a file is paused, false if not
  33.         /// </summary>
  34.         public bool isPause { get; internal set; }
  35.  
  36.         /// <summary>
  37.         /// Default constructor
  38.         /// </summary>
  39.         public MyBasicAudioPlayer()
  40.         {
  41.             isOpen = false;
  42.             isPause = false;
  43.         }
  44.  
  45.         /// <summary>
  46.         /// Close the audio file
  47.         /// </summary>
  48.         public void Close()
  49.         {
  50.             if (isOpen)
  51.             {
  52.                 _command = "close MediaFile";
  53.                 mciSendString(_command, null, 0, IntPtr.Zero);
  54.                 isOpen = false;
  55.             }
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Open the audio file passed on argument
  60.         /// </summary>
  61.         /// <param name="fileName">Path of the file from the root</param>
  62.         public void Open(string fileName)
  63.         {
  64.             if (!isOpen)
  65.             {
  66.                 _command = "open \"" + fileName + "\" type mpegvideo alias MediaFile";
  67.                 mciSendString(_command, null, 0, IntPtr.Zero);
  68.                 isOpen = true;
  69.                 isPause = false;
  70.             }
  71.         }
  72.  
  73.         /// <summary>
  74.         /// Play the audio file who's loaded whith Open(string filename)
  75.         /// </summary>
  76.         /// <param name="loop">If true, the audio will be play again and again.</param>
  77.         public void Play(bool loop)
  78.         {
  79.             if (isOpen && !isPause)
  80.             {
  81.                 _command = "play MediaFile";
  82.                 if (loop)
  83.                     _command += " REPEAT";
  84.                 mciSendString(_command, null, 0, IntPtr.Zero);
  85.             }
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Pause the current audio file
  90.         /// </summary>
  91.         public void Pause()
  92.         {
  93.             if (isOpen && !isPause)
  94.             {
  95.                 _command = "pause MediaFile";
  96.                 mciSendString(_command, null, 0, IntPtr.Zero);
  97.                 isPause = true;
  98.             }
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Resume after a pause the current audio file
  103.         /// </summary>
  104.         public void Resume()
  105.         {
  106.             if (isOpen && isPause)
  107.             {
  108.                 _command = "resume MediaFile";
  109.                 mciSendString(_command, null, 0, IntPtr.Zero);
  110.                 isPause = false;
  111.             }
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Stop the current audio file
  116.         /// </summary>
  117.         public void Stop()
  118.         {
  119.             if (isOpen)
  120.             {
  121.                 _command = "stop MediaFile";
  122.                 mciSendString(_command, null, 0, IntPtr.Zero);
  123.                 isPause = false;
  124.             }
  125.         }
  126.  
  127.         /// <summary>
  128.         /// Set the volume of the current audio file
  129.         /// </summary>
  130.         /// <param name="volume">Volume to set between 0 & 1000</param>
  131.         public void SetVolume(int volume)
  132.         {
  133.             _command = "setaudio MediaFile volume to " + volume.ToString();
  134.             mciSendString(_command, null, 0, IntPtr.Zero);
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement