mciSendString in C#

The mciSendString function sends a command string to an MCI device. The device that the command is sent to is specified in the command string.

Code:

using System.Runtime.InteropServices;

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

public string playCommand;
public string fileName;
public bool isPlay = false;
public bool isStop = false;

private void frmPlayer_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(fileName))
{
try
{
if (File.Exists(fileName))
{
if (isPlay)
{
playCommand = "Close MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
isPlay = false;
isStop = true;
}
playCommand = "open \"" + fileName + "\" type mpegvideo alias MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
playCommand = "play MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
isPlay = true;
btnPlayPlayer.Enabled = true;
btnPause.Enabled = true;
btnStop.Enabled = true;
}
else
{
isPlay = false;
btnPlayPlayer.Enabled = false;
btnPause.Enabled = false;
btnStop.Enabled = false;
MessageBox.Show("Invalid File!");
}
}
catch (Exception ex)
{
isPlay = false;
btnPlayPlayer.Enabled = false;
btnPause.Enabled = false;
btnStop.Enabled = false;
MessageBox.Show(ex.Message);
}
}
}


private void btnPlayPlayer_Click(object sender, EventArgs e)
{
if (!isPlay)
{
if (isStop)
{
playCommand = "open \"" + fileName + "\" type mpegvideo alias MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
playCommand = "play MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
isPlay = true;
isStop = false;
}
else
{
playCommand = "play MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
isPlay = true;
}
}
}


private void btnPause_Click(object sender, EventArgs e)
{
if (isPlay)
{
playCommand = "pause MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
isPlay = false;
}
}

private void btnStop_Click(object sender, EventArgs e)
{
if (isPlay)
{
playCommand = "Close MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
isPlay = false;
isStop = true;
}
}


This coding will help us to play media file…

....S.VinothkumaR.

1 comment:

Magnus said...

Don't forget: using System.Text;