Wednesday, August 31, 2011

MCI : Complete Recorder and Player Class


#region NameSpace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
#endregion

namespace EBAudioControl
{
public class Player
{
#region Fields
public long test = 1;
Random randomNumber = new Random();
private StringBuilder msg; // MCI Error message
private StringBuilder returnData; // MCI return data
private int error;
private string Pcommand; // String that holds the MCI command
private ListView playlist; // ListView as a playlist with the song path
public int NowPlaying { get; set; }
public bool Paused { get; set; }
public bool Loop { get; set; }
public bool Shuffle { get; set; }
[DllImport("winmm.dll")]
private static extern int mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
[DllImport("winmm.dll")]
public static extern int mciGetErrorString(int errCode, StringBuilder errMsg, int buflen);
#endregion

public enum Position
{
Begining,
Current
}

#region Constructor
public Player()
{

NowPlaying = 0;
Loop = false;
Shuffle = true;
Paused= false;
msg = new StringBuilder(128);
returnData = new StringBuilder(128);
}
#endregion

#region Methods

public void Close()
{
Pcommand = "close sounddev";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
}

public bool Open()
{
Pcommand = "open new Type waveaudio Alias sounddev";
error = mciSendString(Pcommand, returnData, 128, IntPtr.Zero);
if (error == 0)
return true;
else
return false;
}

public bool Load(string sFileName)
{
Close();
// Try to open as mpegvideo
Pcommand = "open \"" + sFileName + "\" type mpegvideo alias sounddev";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error != 0)
{
// Let MCI deside which file type the song is
Pcommand = "open \"" + sFileName + "\" alias sounddev";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error == 0)
return true;
else
return false;
}
else
return true;
}

public bool LoadNPlay(string fileName)
{
if (Load(fileName))
{
Pcommand = "play sounddev";
error = mciSendString(Pcommand, null,0, IntPtr.Zero);
if (error == 0)
{
return true;
}
else
{
Close();
return false;
}
}
else
return false;
}

public bool Save(string fileName)
{

Pcommand = "save sounddev " + fileName;
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error == 0)
{
Close();
return true;
}
else
{
Close();
return false;
}

}

public bool MoveToEnd()
{
int pos = GetLength();
Pcommand = "seek sounddev to " + pos.ToString();
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error == 0)
{
return true;
}
else
{
return false;
}
}

public bool MoveToBegin()
{
Pcommand = "seek sounddev to 0";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error == 0)
{
return true;
}
else
{
return false;
}
}

public bool MoveBack()
{
int pos= GetCurentMilisecond();
if (pos > 5000) pos -=5000;
else pos = 0;
Pcommand = "seek sounddev to " + pos.ToString();

error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error == 0)
{
return true;
}
else
{
return false;
}
}

public bool MoveForward()
{
int pos = GetCurentMilisecond();
int length = GetLength();

if (length > (5000 + pos)) pos +=5000;
else
{
return false;
}
Pcommand = "pause sounddev ";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);

if (error != 0)

{
return false;
}

Pcommand = "seek sounddev to " + pos.ToString();

error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error == 0)
{
return true;
}
else
{
return false;
}
}

public bool PlayFrom(Position pos)
{
if (pos != Position.Begining)
{
if (GetLength() == GetCurentMilisecond())
{
Pcommand = "play sounddev from 0";
}
else
Pcommand = "play sounddev";
}
else
{
Pcommand = "play sounddev from 0";
}
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
if (error == 0)
{
return true;
}
else
{
return false;
}
}

//public bool Play(int track)
//{
// if (Open(playlist.Items[track].SubItems[1].Text))
// {
// Pcommand = "play sounddev";
// error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
// if (error == 0)
// {
// NowPlaying = track;
// return true;
// }
// else
// {
// Close();
// return false;
// }
// }
// else
// return false;
//}

public void Pause()
{

if(IsPlaying()||IsRecording())
{
Pcommand = "pause sounddev";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
Paused = true;
}
}
//public void RecordPause()
//{
// error = mciSendString("pause sounddev", null, 0, IntPtr.Zero);
//}
public void Stop()
{
Pcommand = "stop sounddev";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
Paused = false;
Close();
}

public void Resume()
{
Pcommand = "resume sounddev";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
}

public bool Record()
{
error = mciSendString("set sounddev bitspersample 16", null, 0, IntPtr.Zero);
error = mciSendString("set sounddev samplespersec 44100", null, 0, IntPtr.Zero);
Pcommand = "record sounddev";
error = mciSendString(Pcommand, null, 128, IntPtr.Zero);
if (error == 0)

return true;
else return false;

}

//public bool Record()
//{
// error = mciSendString("set recsound bitspersample 16", null, 0, IntPtr.Zero);
// error = mciSendString("set recsound samplespersec 44100", null, 0, IntPtr.Zero);
// Pcommand = "record recsound";
// error = mciSendString(Pcommand, null, 128, IntPtr.Zero);
// if(error==0)

// return true;
// else return false;

//}
public bool ResumeRecord()
{

Pcommand = "record sounddev";
error = mciSendString(Pcommand, null, 128, IntPtr.Zero);
if (error == 0)
return true;
else
return false;

}
#endregion

#region Status

public bool IsPlaying()
{
Pcommand = "status sounddev mode";
error = mciSendString(Pcommand, returnData, 128, IntPtr.Zero);
if ((returnData.Length >0) && (returnData.ToString().Contains( "playing")))

return true;

else
return false;
}

public bool IsRecording()
{
Pcommand = "status sounddev mode";
error = mciSendString(Pcommand, returnData, 128, IntPtr.Zero);

if (returnData.Length > 7 && (returnData.ToString().Contains("recording")))
return true;
else
return false;
}

public bool IsOpen()
{
Pcommand = "status sounddev mode";
error = mciSendString(Pcommand, returnData, 128, IntPtr.Zero);
if (returnData.Length == 4 && returnData.ToString().Substring(0, 4) == "open")
return true;
else
return false;
}

public bool IsPaused()
{
Pcommand = "status sounddev mode";
error = mciSendString(Pcommand, returnData, 128, IntPtr.Zero);
if (returnData.Length == 6 && returnData.ToString().Substring(0, 6) == "paused")
return true;
else
return false;
}

public bool IsStopped()
{
Pcommand = "status sounddev mode";
error = mciSendString(Pcommand, returnData, 128, IntPtr.Zero);
if (returnData.Length == 7 && returnData.ToString().Substring(0, 7) == "stopped")
return true;
else
return false;
}
#endregion

#region Logic
public void GetRecPosition()
{
Pcommand = "set playmsound time format samples";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
Pcommand = "play playmsound";
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
StringBuilder retStr = new StringBuilder(1000);
string sCommand = "status playmsound position ";
mciSendString(sCommand, retStr, 1000, IntPtr.Zero);
}

public int GetCurentMilisecond()
{
Pcommand = "status sounddev position";
error = mciSendString(Pcommand, returnData, returnData.Capacity, IntPtr.Zero);
if (returnData.ToString() != "")
return int.Parse(returnData.ToString());
else
return 0;
}

public int GetLength()
{
Pcommand = "Set sounddev time format milliseconds";
error = mciSendString(Pcommand, returnData, returnData.Capacity, IntPtr.Zero);
Pcommand = "status sounddev length";
error = mciSendString(Pcommand, returnData, returnData.Capacity, IntPtr.Zero);
int length;
int.TryParse(returnData.ToString(),out length);
if(returnData.Length!=0)
{

return length;
}
return 0;

}
//public int GetCurentMilisecond()
//{
// Pcommand = "status sounddev position";
// error = mciSendString(Pcommand, returnData, returnData.Capacity, IntPtr.Zero);
// return int.Parse(returnData.ToString());
//}

public void SetPosition(int miliseconds)
{
if (IsPlaying())
{
Pcommand = "play sounddev from " + miliseconds.ToString();
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
}
else
{
Pcommand = "seek sounddev to " + miliseconds.ToString();
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
}
}

public int GetSongLenght()
{
if (IsPlaying())
{
Pcommand = "status sounddev length";
error = mciSendString(Pcommand, returnData, returnData.Capacity, IntPtr.Zero);
return int.Parse(returnData.ToString());
}
else
return 0;
}

#endregion

#region AudioVolume
public bool SetVolume(int volume)
{
if (volume >= 0 && volume <= 1000)
{

Pcommand = "setaudio sounddev volume to " + volume.ToString();

// Pcommand = "set sounddev volume to " + volume.ToString();
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
return true;
}
else
return false;
}

public bool SetBalance(int balance)
{
if (balance >= 0 && balance <= 1000)
{
Pcommand = "set sounddev left volume to " + (1000 - balance).ToString();
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
Pcommand = "set sounddev right volume to " + balance.ToString();
error = mciSendString(Pcommand, null, 0, IntPtr.Zero);
return true;
}
else
return false;
}

#endregion

}
}

No comments: