Advertisement
Guest User

Musicplayer implementation using mciSendString() API

a guest
Feb 17th, 2015
4,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9.  
  10. namespace MusicGuru
  11. {
  12.     public class MusicPlayerOld
  13.     {
  14.  
  15.         //System.Media.SoundPlayer Player;
  16.         //WMPLib.WindowsMediaPlayer Player;
  17.         public bool IsBeingPlayed = false;
  18.         private bool IsLooping = false;
  19.         public string FileName;
  20.         public string TrackName;
  21.         //private int Timer = 0;//in minutes
  22.         //BackgroundWorker Player;
  23.         private long lngVolume = 500; //between 0-1000
  24.  
  25.  
  26.         public MusicPlayerOld(string fileName)
  27.         {
  28.             this.TrackName = fileName;
  29.             if (fileName.Contains("\\"))
  30.                 this.FileName = fileName;
  31.             else
  32.                 this.FileName = AppDomain.CurrentDomain.BaseDirectory + fileName;
  33.             //Player = new BackgroundWorker();
  34.             //Player.DoWork += new DoWorkEventHandler(Player_DoWork);
  35.  
  36.             //Player = new WMPLib.WindowsMediaPlayer();
  37.             //Player.URL = AppDomain.CurrentDomain.BaseDirectory + "local.wav";
  38.             //Player = new System.Media.SoundPlayer(fileName);
  39.         }
  40.  
  41.         ////play the track for n minutes
  42.         //public void StartPlaying()
  43.         //{
  44.         //    try
  45.         //    {
  46.         //        //this.Timer = timer;
  47.         //        //IsBeingPlayed = true;
  48.         //        PlayLoop();
  49.         //        //ThreadStart ts = new ThreadStart(Loop);
  50.         //        //LoopThread = new Thread(ts);
  51.         //        //LoopThread.Start();
  52.  
  53.         //        //if (!Player.IsBusy)
  54.         //            //Player.RunWorkerAsync();
  55.         //    }
  56.         //    catch (Exception ex)
  57.         //    {
  58.         //        frmMain.WriteLog("Error occured in StartPlaying: " + ex.Message);
  59.         //    }
  60.         //}
  61.  
  62.         private void PlayWorker()
  63.         {
  64.             StringBuilder sb = new StringBuilder();
  65.             int result = mciSendString("open \"" + FileName + "\" type waveaudio  alias " + this.TrackName , sb, 0, IntPtr.Zero);
  66.             mciSendString("play " + this.TrackName, sb, 0, IntPtr.Zero);
  67.             IsBeingPlayed = true;
  68.             //loop
  69.             sb = new StringBuilder();
  70.             mciSendString("status " + this.TrackName + " length", sb, 255, IntPtr.Zero);
  71.             int length = Convert.ToInt32(sb.ToString());
  72.             int pos = 0;
  73.             long oldvol = lngVolume;
  74.  
  75.             //set the initial volume - Changed by Prahlad for phase-2
  76.             //sb = new StringBuilder("................................................................................................................................");
  77.             //mciSendString("setaudio " + this.TrackName + " volume to " + lngVolume.ToString(), sb, sb.Length, IntPtr.Zero);
  78.  
  79.  
  80.             while (IsBeingPlayed)
  81.             {
  82.                 sb = new StringBuilder();
  83.                 mciSendString("status " + this.TrackName + " position", sb, 255, IntPtr.Zero);
  84.                 pos = Convert.ToInt32(sb.ToString());
  85.                 if (pos >= length)
  86.                 {
  87.                     if (!IsLooping)
  88.                     {
  89.                         IsBeingPlayed = false;
  90.                         break;
  91.                     }
  92.                     else
  93.                     {
  94.                         mciSendString("play " + this.TrackName + " from 0", sb, 0, IntPtr.Zero);
  95.                     }
  96.                 }
  97.  
  98.                 if (oldvol != lngVolume) //volume is changed by user
  99.                 {
  100.                     //set new volume - Changed by Prahlad for phase-2
  101.                     sb = new StringBuilder("................................................................................................................................");
  102.                     string cmd = "setaudio " + this.TrackName + " volume to " + lngVolume.ToString();
  103.                     long err = mciSendString(cmd, sb, sb.Length, IntPtr.Zero);
  104.                     System.Diagnostics.Debug.Print(cmd);
  105.                     if (err != 0)
  106.                     {
  107.                         System.Diagnostics.Debug.Print("ERror " + err);
  108.                     }
  109.                     else
  110.                     {
  111.                         System.Diagnostics.Debug.Print("No errors!");
  112.                     }
  113.                     oldvol = lngVolume;
  114.                 }
  115.                 //Player.openPlayer( AppDomain.CurrentDomain.BaseDirectory + "local.wav");
  116.                 //Player.Play();
  117.                 Application.DoEvents();
  118.                 //Thread.Sleep(500);
  119.             }
  120.             mciSendString("stop " + this.TrackName, sb, 0, IntPtr.Zero);
  121.             mciSendString("close " + this.TrackName, sb, 0, IntPtr.Zero);
  122.         }
  123.  
  124.         //volume between 0-10
  125.         public int GetVolume()
  126.         {
  127.             return (int)this.lngVolume / 100;
  128.         }
  129.  
  130.         //volume between 0-10
  131.         public void SetVolume(int newvolume)
  132.         {
  133.             this.lngVolume = newvolume * 100;
  134.             //mciSendString("setaudio " + strAlias + " volume to " & lngVolume, "", 0, 0&);
  135.         }
  136.  
  137.         public void Play(bool Looping)
  138.         {
  139.             try
  140.             {
  141.                 if (IsBeingPlayed)
  142.                     return;
  143.                 if (!File.Exists(FileName))
  144.                 {
  145.                     IsBeingPlayed = true;
  146.                     return;
  147.                 }
  148.                 this.IsLooping = Looping;
  149.                 ThreadStart ts = new ThreadStart(PlayWorker);
  150.                 Thread WorkerThread = new Thread(ts);
  151.                 WorkerThread.Start();
  152.                 //DateTime t = DateTime.Now;
  153.                 //PlaySound(FileName, IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC | SoundFlags.SND_NODEFAULT | SoundFlags.SND_LOOP);// | SoundFlags.SND_NOSTOP );
  154.                 //mciSendString("Open \"" + AppDomain.CurrentDomain.BaseDirectory + "local.wav\" alias local",new StringBuilder(),0,IntPtr.Zero);
  155.                 //mciSendString("play local", new StringBuilder(), 0, IntPtr.Zero);
  156.                 //PlaySound(null, IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC | SoundFlags.SND_LOOP);
  157.                 //Timer = 0;//reset the timer
  158.             }
  159.             catch (Exception ex)
  160.             {
  161.                 frmMain.WriteLog("Error occured in Loop: " + ex.Message);
  162.             }
  163.         }
  164.  
  165.         public void StopPlaying()
  166.         {
  167.             IsBeingPlayed = false;
  168.             //PlaySound(null, IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC | SoundFlags.SND_LOOP);
  169.             //mciSendString("stop local", new StringBuilder(), 0, IntPtr.Zero);
  170.             //mciSendString("close local", new StringBuilder(), 0, IntPtr.Zero);
  171.             //if (LoopThread !=null && LoopThread.ThreadState==ThreadState.Running)
  172.             //    LoopThread.Abort();
  173.         }
  174.  
  175.         //sound api functions
  176.         [DllImport("winmm.dll")]
  177.         static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
  178.  
  179.         [DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  180.         static extern bool PlaySound(
  181.             string pszSound,
  182.             IntPtr hMod,
  183.             SoundFlags sf);
  184.  
  185.         // Flags for playing sounds.  For this example, we are reading
  186.         // the sound from a filename, so we need only specify
  187.         // SND_FILENAME | SND_ASYNC
  188.  
  189.         [Flags]
  190.         public enum SoundFlags : int
  191.         {
  192.             SND_SYNC = 0x0000,  // play synchronously (default)
  193.             SND_ASYNC = 0x0001,  // play asynchronously
  194.             SND_NODEFAULT = 0x0002,  // silence (!default) if sound not found
  195.             SND_MEMORY = 0x0004,  // pszSound points to a memory file
  196.             SND_LOOP = 0x0008,  // loop the sound until next sndPlaySound
  197.             SND_NOSTOP = 0x0010,  // don't stop any currently playing sound
  198.             SND_PURGE = 0x40, // <summary>Stop Playing Wave</summary>
  199.             SND_NOWAIT = 0x00002000, // don't wait if the driver is busy
  200.             SND_ALIAS = 0x00010000, // name is a registry alias
  201.             SND_ALIAS_ID = 0x00110000, // alias is a predefined ID
  202.             SND_FILENAME = 0x00020000, // name is file name
  203.             SND_RESOURCE = 0x00040004  // name is resource name or atom
  204.         }
  205.  
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement