Click here to Skip to main content
Click here to Skip to main content

Audio Player

, 21 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Using API function to play audio file with random drawing


I had some trials to create audio player using "Microsoft.DirectX.AudioVideoPlayback", but I found the mciSendString function (from Windows API) is better. So, I created a class using mciSendString functions to play audio files, then compile it to a DLL file. The purpose of this article is not to use my player, but the goal is to answer some questions asked by some readers, so the reader will read about the following methods in this article:

  • How to get Random number
  • Draw examples: how to draw string and draw line
  • Using FillRectangle method to draw a point
  • Using (SendMessage) API to move a form (read it in source file)
  • Using (SetWindowPos) API to make form topmost (read it in source file)
  • How to use the position of audio file as Timer (read it in source file)


To use my DLL file, please add MediaControl.dll to your project reference, then declare an instance as follows:

private MediaControl.AudioPlayer MyPlayer = new MediaControl.AudioPlayer(); 

if your code is under C#, or:

Private MyPlayer As MediaControl.AudioPlayer = new MediaControl.AudioPlayer() 

if your code is under VB.NET.

I created two projects, I wrote the code of one under C# (2003) and wrote another under VBNet (2003). The code in this article will be under C#, you can go to VB project and read the code from the source file.

Please extract file to read more about the C# code and extract file to read about the VB code.

In the following table, you can read about some commands of the player as examples.

Command Explanation Example
Open Open music file. (*.mp3, *.ram, *.wav, ...) MyPlayer.Open(FileName);
Play Play music file MyPlayer.Play();
Pause Stop the song temporary MyPlayer.Pause = true;
Close Stop playing MyPlayer.Close();
Volume Set and get volume MyPlayer.Volume = Value;
Position Set and get position MyPlayer.position = Value;
Duration Get the duration label1.Text = MyPlayer.Duration();
SetAudioOff Set voice off MyPlayer.SetAudioOff();
SetAudioOn Set voice on MyPlayer.SetAudioOn();

You can test this project with the file ..\..\Music\S1.mp3 or any audio file.

Using the Code

Open file dialog to load music file:

private void LoadMusicFile()
   DialogResult ResultDialog;
   FoundIt = false;
      this.OpenDialog.Filter = "Music Formats|" + 
      "*.mp3;*.ram;*.rm;*.wav;*.wma;*.mid|" + 
      "mp3 (*.mp3)|*.mp3|ram (*.ram)|*.ram|rm (*.rm)|*.rm|" + 
      "wav (*.wav)|*.wav|wma (*.wma)|*.wma|mid (*.mid)|*.mid";
      this.OpenDialog.RestoreDirectory = true;
      this.OpenDialog.FileName = "";
      this.OpenDialog.Title = "Choose Music File:";
      this.OpenDialog.CheckFileExists = true;
      this.OpenDialog.CheckPathExists = true;
      this.OpenDialog.Multiselect = false; //choose one file
      ResultDialog = OpenDialog.ShowDialog();
      if (ResultDialog == DialogResult.OK)
         MusicFileName = this.OpenDialog.FileName;
         FoundIt = true;
      string Msg = "Not Sound file, Search for another..!";

Draw lines and use the FillRectangle method to draw a point when playing audio:

private void DrawSomething()
int PageWidth = PicView.Width;
int PageHeight = PicView.Height;
int yStart = PageHeight - 20;
double Pi = Math.PI;

if(Muted == false)
MyPlayer.Volume = this.tbVolume.Value;

MyPlayer.Volume = MyPlayer.Pause == true? 0 : this.tbVolume.Value;

/* you can use the following code instead previous line:
if(MyPlayer.Pause == true)
MyPlayer.Volume = 0;
MyPlayer.Volume = this.tbVolume.Value; */

//ratio between PicView.Height and maximum of player volume 
double yRatio = Convert.ToDouble(PageHeight)/1000;

Graphics g;
// Create graphics object
g = PicView.CreateGraphics();
System.Threading.Thread.Sleep(150); // waiting a moment before clear drawing

Random randNum = new Random();
double W = PageWidth/20;

Pen NewPen = new Pen(Color.SteelBlue);
SolidBrush brush = new SolidBrush(Color.SkyBlue);

for (double X = -3*Pi ; X < 3*Pi; X+=0.5) 
double Z = Math.Sin(X);
double Y = Math.Abs(Z);
double PX = PageWidth/2 + W*X; // Multiply X * W to enlarge X.
int maxY = (int)(yRatio * MyPlayer.Volume);
if(maxY < 40)
maxY = 40;
double H = randNum.Next(5, maxY - 30); // Random height:
double PY = yStart - H*Y; // Multiply Y * H to enlarge Y.
Point p1 = new Point((int)PX, PageHeight - 10);
Point p2 = new Point((int)PX, (int)PY);
// draw Line
g.DrawLine(NewPen, p1, p2);
Point dPoint = new Point((int)PX, (int)PY);
dPoint.X = dPoint.X - 1;
dPoint.Y = dPoint.Y - 10;
Rectangle rect = new Rectangle(dPoint, new Size(3, 3)); // size of Rectangle as point
//using FillRectangle to draw a point
g.FillRectangle(brush, rect);

// Release graphics object

Draw string:

private void DrawText()
PicView.Image = null;

int PageWidth = PicView.Width;
int PageHeight = PicView.Height;

// Initiate 'ImageToDraw' Object:
Bitmap ImageToDraw = new Bitmap(PageWidth, PageHeight);
// Create graphics object:
Graphics g = Graphics.FromImage(ImageToDraw);
// Draw "Hello World..." string:
Font txtFont = new Font("Tahoma", 24, FontStyle.Bold);
string txt = "Hello World...";
g.DrawString(txt, txtFont, new SolidBrush(Color.SeaGreen), 20, 60);
// Draw "Audio Player" string:
txtFont = new Font("Monotype Corsiva", 18, FontStyle.Bold | FontStyle.Italic);
txt = "Audio Player";
g.DrawString(txt, txtFont, new SolidBrush(Color.Chocolate), 240, 100);

// Display the bitmap:
PicView.Image = ImageToDraw;
// Release graphics object
ImageToDraw = null;


When extract the *.zip files, you can find MediaControl.dll file in (..\..\bin) folder. You can read about commands: Play, Pause, Stop, ... etc.
You can also read about resizing the form and the work of all buttons.

Final Words

I hope this article is useful. If you have any ideas, please tell me. Thanks to Code Project and thanks to all.

Mostafa Kaisoun


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

QuestionProblem in MyPlayer.Duration() PinmemberRockford Sol27-Sep-14 3:33 
GeneralGreat JOB! Pinmemberalecs.mkd10-Apr-14 8:21 
Questionmyplayer.duration Pinmemberpeejay_cadungog14-Nov-13 5:32 
QuestionHow to add Previous and Next button in the given C# sample music player? PinmemberMember 102375528-Sep-13 8:08 
Questionhow to get .dll file PinmemberVipin Sharma6-Aug-12 21:14 
AnswerRe: how to get .dll file PinmemberMostafa Kaisoun7-Aug-12 9:49 
QuestionChange Playback Rate PinmemberMahmoud Jannesari2-Aug-12 19:44 
AnswerRe: Change Playback Rate PinmemberMostafa Kaisoun3-Aug-12 6:13 
AnswerRe: Change Playback Rate PinmemberMostafa Kaisoun4-Aug-12 14:27 
GeneralRe: Change Playback Rate PinmemberOm2Kool29-Aug-13 15:52 
AnswerRe: Change Playback Rate PinmemberMostafa Kaisoun5-Aug-12 4:49 
GeneralRe: Change Playback Rate PinmemberOm2Kool29-Aug-13 15:53 
Questionابداااااااااااااااع Pinmemberno-mercy201222-Jul-12 22:27 
AnswerRe: ابداااااااااااااااع PinmemberMostafa Kaisoun25-Jul-12 6:05 
QuestionSpeed Increasing and Decreasing Pinmemberjaypee197913-Jul-12 18:37 
AnswerRe: Speed Increasing and Decreasing PinmemberMostafa Kaisoun14-Jul-12 21:53 
QuestionMyPlayer.Position can you ? Pinmemberhatecows4-Feb-11 6:00 
AnswerRe: MyPlayer.Position can you ? PinmemberMostafa Kaisoun4-Feb-11 10:40 
GeneralRe: MyPlayer.Position can you ? Pinmemberhatecows7-Feb-11 3:09 
GeneralRe: MyPlayer.Position can you ? PinmemberMostafa Kaisoun8-Feb-11 5:43 
GeneralRe: MyPlayer.Position can you ? Pinmemberhatecows9-Feb-11 1:36 
GeneralRe: MyPlayer.Position can you ? PinmemberMostafa Kaisoun9-Feb-11 8:57 
GeneralLoad data from memory Pinmembersuperior_new17-Sep-10 4:11 
GeneralRe: Load data from memory PinmemberMostafa Kaisoun22-Sep-10 8:41 
GeneralMy vote of 2 PinmemberS. Mostafa Rahmani23-Aug-10 1:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 22 Aug 2010
Article Copyright 2010 by Mostafa Kaisoun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid