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

Using mciSendString to play media files

, 22 Jan 2007
Rate this:
Please Sign up or sign in to vote.
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.

private Media media = new Media();

Here is the code for the Play button:

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:

//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:

//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:

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:

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)

About the Author

DAT HAN

United States United States
No Biography provided

Comments and Discussions

 
QuestionAnyone know how to get the status of ODD Tray? Pinmemberwooutstanding21-Nov-12 22:07 
GeneralMy vote of 5 Pinmemberkhosrow parizi31-Aug-12 3:04 
GeneralmciSendString feature on WinXP x64 Pinmemberkanabiozz4-Feb-09 3:20 
GeneralASX url PinmemberLuigi.G29-Nov-07 21:00 

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.140709.1 | Last Updated 22 Jan 2007
Article Copyright 2007 by DAT HAN
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid