Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / C#
Article

Using mciSendString to play media files

Rate me:
Please Sign up or sign in to vote.
3.65/5 (12 votes)
22 Jan 2007CPOL2 min read 114.2K   2.6K   25   6
Using mciSendString to playback media files and notify when the playback completes.

Sample Image - a_mini_media_player.jpg

Introduction

When I was trying to use MCI to play a media file, I searched and got some ideas from the Internet on how to use mciSendSting. But, I had a need to know when the playback would finish. In this demo project, I have mentioned how to send a command to open, play with notify flag, and close a media, and how to receive a message when the playback is done.

It's very simple. You select a media file (MP3, Wav, ...), then click on the Play button. The Stop button will be enabled so that you can stop the playback. When the playback is completed, it will send a notification message to the form. Then, you will see the Stop button dim.

Background

For more information about MCI, please refer to MSDN. I also got some ideas from the article: Playing MP3s using MCI.

Using the code

I put all of the code work with MCI in a Media class. So, when you want to play a media file, you just need to create a Media object.

C#
private Media media = new Media();

Here is the code for the Play button:

C#
private void playButton_Click(object sender, EventArgs e)
{
    if (File.Exists(mediaFileTextBox.Text))
    {
        media.Play(mediaFileTextBox.Text, this);
        RefreshStop(true);
    }
    else
    {
        MessageBox.Show("The media file does not exist.", 
            "File Not Found", MessageBoxButtons.OK, 
            MessageBoxIcon.Warning);
    }
}

The Play method of Media needs two parameters: file name and form. Using this form handle, MCI will send a message to it when the playback is finished. If you want to get the message, you need to override the WinProc method of System.Windows.Form:

C#
//Override the WndProc function in the form
//to receive the notify message when the playback complete
protected override void WndProc(ref Message m)
{
    if (m.Msg == Media.MM_MCINOTIFY)
    {
        RefreshStop(false);
    }
    base.WndProc(ref m);
}

When the playback is complete, it will send a MM_MCINOTIFY message. You need to check to see if it is a MM_MCINOTIFY message. In this case, I set the Stop button on dim.

The interface of mciSendString defined in the Media class:

C#
//mciSendString 
[DllImport("winmm.dll")]
private static extern long mciSendString(
    string command,
    StringBuilder returnValue,
    int returnLength,
    IntPtr winHandle);

The winHandle is the handle of the form we need to be notified when the playback completes. Here is the code for the Play method of the Media class:

C#
private void PlayMediaFile()
{
    if (isOpen)
    {
        string playCommand = "Play " + mediaName + " notify";
        mciSendString(playCommand, null, 0, notifyForm.Handle);
    }
}

With the notify and the notifyForm.Handle, MCI will send the MM_MCINOTIFY message to the form when the playback completes.

Here is the full source code for the Media class:

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MiniPlayer
{
    public class Media
    {
        public const int MM_MCINOTIFY = 0x3B9;

        private string fileName;
        private bool isOpen = false;
        private Form notifyForm;
        private string mediaName = "media";

        //mciSendString 
        [DllImport("winmm.dll")]
        private static extern long mciSendString(
            string command,
            StringBuilder returnValue,
            int returnLength,
            IntPtr winHandle);

        private void ClosePlayer()
        {
            if (isOpen)
            {
                String playCommand = "Close " + mediaName;
                mciSendString(playCommand, null, 0, IntPtr.Zero);
                isOpen = false;
            }
        }


        private void OpenMediaFile()
        {
            ClosePlayer();
            string playCommand = "Open \"" + fileName + 
                                "\" type mpegvideo alias " + mediaName;
            mciSendString(playCommand, null, 0, IntPtr.Zero);
            isOpen = true;
        }


        private void PlayMediaFile()
        {
            if (isOpen)
            {
                string playCommand = "Play " + mediaName + " notify";
                mciSendString(playCommand, null, 0, notifyForm.Handle);
            }
        }


        public void Play(string fileName, Form notifyForm)
        {
            this.fileName = fileName;
            this.notifyForm = notifyForm;
            OpenMediaFile();
            PlayMediaFile();
        }

        public void Stop()
        {
            ClosePlayer();
        }

    }
}

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to start at a certain time into the media file? Pin
Member 133916532-Dec-21 18:42
Member 133916532-Dec-21 18:42 
Generalthanks,it worked fine. Pin
phiree28-Sep-15 1:18
phiree28-Sep-15 1:18 
QuestionAnyone know how to get the status of ODD Tray? Pin
wooutstanding21-Nov-12 22:07
wooutstanding21-Nov-12 22:07 
GeneralMy vote of 5 Pin
khosrow parizi31-Aug-12 3:04
khosrow parizi31-Aug-12 3:04 
GeneralmciSendString feature on WinXP x64 Pin
kanabiozz4-Feb-09 3:20
kanabiozz4-Feb-09 3:20 
GeneralASX url Pin
Luigi.G29-Nov-07 21:00
Luigi.G29-Nov-07 21:00 
Hi... What's a command to open "asx" Url ?

In Windows Media Player command is "Open URL".. Cry | :((

Example (Subasio ItalianWebRadio):

http://www.xdevel.com/radio/subasio.asx


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

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