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

Audio Player using winmm.dll and WPL File

, 9 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Opens a Windows Media Playlist, and plays it
Sample Image - Audio_Player__with_Winmm.jpg

Introduction

After looking for a simple, yet free MP3 player, I decided to make my own. I based mine on a commercial package that shall remain nameless [Let's just say it that package was expensive considering it was importing the winmm.dll.]  Almost every example I found for playing audio file [in my case MP3s] using the winmm.dll only had play/stop capabilities (and sometimes pause). That is fine, I just wanted a bit more, and still wanted to keep it simple. Of course, if I had wanted a full blown MP3 player, I probably would have downloaded one of the many free applications out there. That wasn't my purpose.

So the code is fairly simple. It uses the winmm.dll from Windows to play, stop, pause, etc. It also controls the volume of the left/right channels (if there are more than 1). Moreover, I had fun trying my hand at parsing XML, so the application gets audio file information from a Windows Media Playlist [Making one is easy, just use Windows Media Player].

Just please keep in mind, that I'm not a programmer by profession and this is my first contribution to The Code Project. I know that I'm not re-inventing the wheel here, just hoping this will help someone somewhere.

The project has a couple of files:

  • Player.cs [which has all of the winmm.dll string commands]: More can be found here, which in my opinion is a pain to understand sometimes.
  • readPlaylist.cs: This opens up an open dialog window and helps you select a *.wpl file.
  • MainPlayer.cs: Basically the buttons and click events, etc.

Player.cs

I tried putting as many relevant comments in the code as I code without going overboard like I'm doing writing this article.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace MP3player
{
    class Player
    {
        private string Pcommand;
        private bool isOpen;

        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand,
		StringBuilder strReturn, int iReturnLength, int bla);

        /// <SUMMARY>
        /// Not much to construct here
        /// </SUMMARY>
        public Player()
        {
        }

        /// <SUMMARY>
        /// Stops currently playing audio file
        /// </SUMMARY>
        public void Close()
        {
            Pcommand = "close MediaFile";
            mciSendString(Pcommand, null, 0, 0);
            isOpen = false;
        }

        /// <SUMMARY>
        /// Opens audio file to play
        /// </SUMMARY>
        /// <param name="sFileName" />This is the audio file's path and filename</param />
        public void Open(string sFileName)
        {
            Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
            mciSendString(Pcommand, null, 0, 0);
            isOpen = true;
        }

        /// <SUMMARY>
        /// Plays selected audio file
        /// </SUMMARY>
        /// <param name="loop" />If True,audio file will repeat</param />
        public void Play(bool loop)
        {
            if (isOpen)
            {
                Pcommand = "play MediaFile";
                if (loop)
                    Pcommand += " REPEAT";
                mciSendString(Pcommand, null, 0, 0);
            }
        }

        /// <SUMMARY>
        /// Pauses currently playing audio file
        /// </SUMMARY>
        public void Pause()
        {
            Pcommand = "pause MediaFile";
            mciSendString(Pcommand, null, 0, 0);
        }

        /// <SUMMARY>
        /// Returns the current status player: playing, paused, stopped, etc.
        /// </SUMMARY>
        public string Status()
        {
            int i = 128;
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
            mciSendString("status MediaFile mode", stringBuilder, i, 0);
            return stringBuilder.ToString();
        }

        /// <SUMMARY>
        /// Get/Set Left Volume Factor
        /// </SUMMARY>
        public int LeftVolume
        {
            get
            {
                return 0; //Guess could be used to return Volume level: I don't need it
            }
            set
            {
                mciSendString(string.Concat
		("setaudio MediaFile left volume to ", value), null, 0, 0);
            }
        }

        /// <SUMMARY>
        /// Get/Set Right Volume Factor
        /// </SUMMARY>
        public int RightVolume
        {
            get
            {
                return 0; //Guess could be used to return Volume level: I don't need it
            }
            set
            {
                mciSendString(string.Concat
		("setaudio MediaFile right volume to ", value), null, 0, 0);
            }
        }

        /// <SUMMARY>
        /// Get/Set Main Volume Factor
        /// </SUMMARY>
        public int MasterVolume
        {
            get
            {
                return 0; //Guess could be used to return Volume level: I don't need it
            }
            set
            {
                mciSendString(string.Concat
		("setaudio MediaFile volume to ", value), null, 0, 0);
            }
        }

        /// <SUMMARY>
        /// Get/Set Bass Volume Factor
        /// </SUMMARY>
        public int Bass
        {
            get
            {
                return 0;
            }
            set
            {
                mciSendString(string.Concat
		("setaudio MediaFile bass to ", value), null, 0, 0);
            }
        }

        /// <SUMMARY>
        /// Get/Set Treble Volume Factor
        /// </SUMMARY>
        public int Treble
        {
            get
            {
                return 0;
            }
            set
            {
                mciSendString(string.Concat
		("setaudio MediaFile treble to ", value), null, 0, 0);
            }
        }
    }
}

Now for reading the *.wpl file to get MP3s [or whatever] to play.

readPlaylist.cs

using System;
using System.Collections;
using System.Text;
using System.Xml;

namespace MP3player
{
    class readPlaylist
    {
        private ArrayList name = new ArrayList();
        private string m_xmlFile;

        /// <SUMMARY>
        /// The Windows Media Playlist Path xxx.wpl file
        /// </SUMMARY>
        public string playListPath
        {
            get
            {
                return m_xmlFile;
            }
            set
            {
                m_xmlFile = value;
                Makeplaylist();
            }
        }

        /// <SUMMARY>
        /// Return an Arraylist of file found in Windows Media Playlist file
        /// </SUMMARY>
        public ArrayList PlayList
        {
            get
            {
                return name;
            }
        }

        /// <SUMMARY>
        /// Fills up an Arraylist with titles found in the
        /// Windows Media Playlist file.
        /// Using XmlTextReader
        /// </SUMMARY>
        private void Makeplaylist()
        {
            XmlTextReader readList = new XmlTextReader(m_xmlFile);
            while (readList.Read())
            {
                if (readList.NodeType == XmlNodeType.Element)
                {
                    if (readList.LocalName.Equals("media"))
                    {
                        name.Add(readList.GetAttribute(0).ToString());
                    }
                }
            }
        }
    }
}

Let's use the code now.

Here's a part of the file with all the click events and so on.
So here's the start, make sure you got all the references...

MediaPlayer.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MP3player
{
    public partial class MainPlayer : Form
    {
        ArrayList nowPlaylist = new ArrayList();
        Player pl = new Player();

        public MainPlayer()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            lstBoxPlayList.Items.Clear();
            openPlaylistDialog.ShowDialog();
            readPlaylist readList = new readPlaylist();
            readList.playListPath = openPlaylistDialog.FileName;
            nowPlaylist = readList.PlayList;
            for (int x = 0; x < nowPlaylist.Count; x++)
            {
                lstBoxPlayList.Items.Add(nowPlaylist[x]);
            }
            lstBoxPlayList.SetSelected(0, true);
        }

The above code is pretty straight forward. The btnOpen_click event will open an OpenFileDialog window, get the selected *.wpl file and send it to the readPlaylist class, which will in turn parse through it, return an Arraylist with all the file names and their paths. Once that is done, loop through it to display its content [in this case a listbox]. Now you have all your files ready to be played, all that's needed is to select one and press play. Or double-click on it to start it (I won't show that part here).

To play the selected file in the listbox:

private void btnPlay_Click(object sender, EventArgs e)
{
    if (lstBoxPlayList.Items.Count > 0)
    {
        pl.Open(lstBoxPlayList.SelectedItem.ToString());
        pl.Play(false);
    }
}

This is a simple way of checking that a file is really selected before playing.
Besides that, pretty simple. Of course, if you've never done this, it's not. It's called the learning process.
As mentioned above, I think the comments in the code are pretty good. With that and the MSDN link that's at the top... you should do fine.

Another thing, I also tried my hand at controlling the Bass/Treble. Don't know if it works, since the speaker system on my computer isn't great, and I'm hard of hearing [hence the reason my speaker system is crappy]. But if I read the MSDN right, it seems to work the same way as the volume control.

Guess that's about it, hope this little example will help someone out.

History

  • 9th November, 2006: Initial post

License

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

Share

About the Author

loneferret

Canada Canada
Studied English Teaching as a second language at university here in Montreal(you wouldn't know it the way I type would ya), after that did some networking course with a bit of programming. Mostly working as a network admin these days.
I also like to write short stories, and draw..mostly black and white stuff...

Comments and Discussions

 
GeneralCannot hear Bass or Treble changes PinmemberTeeJayMule10119-May-10 6:25 
GeneralRe: Cannot hear Bass or Treble changes PinmemberJason Newland8-Apr-13 20:51 
Generalcannot play chinese name mp3 PinmemberMember 43388682-Jan-08 20:01 
QuestionWhy Player doesent work on Large MP3 file? Pinmembernasserdw11-Nov-07 23:10 
AnswerRe: Why Player doesent work on Large MP3 file? PingroupMember 583052229-Mar-11 4:24 
QuestionWhich is better Pinmemberhenu8-Jan-07 8:28 
GeneralPlaying Audio From Internet PinmemberAl_Pennyworth26-Nov-06 5:35 
AnswerRe: Playing Audio From Internet Pinmemberloneferret26-Nov-06 5:40 
GeneralRe: Playing Audio From Internet PinmemberMartin Raychev2-Dec-06 9:43 
GeneralRe: Playing Audio From Internet Pinmemberloneferret2-Dec-06 15:13 
GeneralRe: Playing Audio From Internet Pinmemberexxonvald11-Feb-07 23:44 
GeneralRe: Playing Audio From Internet PinmemberUmut Comlekcioglu3-Sep-14 12:47 
QuestionLegal? Pinmemberjimmypalm22-Nov-06 20:52 
AnswerRe: Legal? Pinmemberloneferret23-Nov-06 1:38 
GeneralRe: Legal? PinmemberCCosgrove8113-Mar-07 3:31 
GeneralRe: Legal? Pinmemberloneferret25-Mar-07 3:34 

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
Web02 | 2.8.141022.1 | Last Updated 9 Nov 2006
Article Copyright 2006 by loneferret
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid