I wanna play background music and
face detection
simultaneously. But i couldn't play the music, though the playMusic()
function works fine individually(in a separate project). When i call the
function inside the
face detection system then no music heard. i can't find out where the problems are.
Environment:
Luxand FaceSDK 3.0
C#.NET Framework 3.5
The Following Programs (Play Background Music) Works Fine // CLASS: BackGroundMusicGP
namespace BackGroundMusicGP
{
class BkGrndMusic
{
private string _command;
private bool isOpen;
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
public void Close()
{
_command = "close MediaFile";
mciSendString(_command, null, 0, IntPtr.Zero);
isOpen = false;
}
public void Open(string sFileName)
{
_command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendString(_command, null, 0, IntPtr.Zero);
isOpen = true;
}
public void Play(bool loop)
{
if(isOpen)
{
_command = "play MediaFile";
if (loop)
_command += " REPEAT";
mciSendString(_command, null, 0, IntPtr.Zero);
}
}
public void
playMusic()
{
string path = "E:\\abc\\ss.mp3";
string filename = Path.GetFileName(path);
Open(filename);
Play(false);
}
}
}
///////////////////////////
public partial class Form1 : Form
{
BkGrndMusic objBkMusic;
public Form1()
{
InitializeComponent();
objBkMusic = new BkGrndMusic();
}
private void button1_Click(object sender, EventArgs e)
{
objBkMusic.playMusic();
// it works
}
But the following code doesn't play the music when i call
objBkMusic.playMusic();
inside the face detection system. For clarification, here is a portion of the program(FaceSDK) is given.
public void showCamera(PictureBox picBox)
{
while (!needClose)
{
Int32 imageHandle = 0;
if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera
{
Application.DoEvents();
continue;
}
IntPtr hbitmapHandle = IntPtr.Zero; // to store the HBITMAP handle
FSDK.SaveImageToHBitmap(imageHandle, ref hbitmapHandle);
Image frameImage = Image.FromHbitmap(hbitmapHandle);
Graphics gr = Graphics.FromImage(frameImage);
string[] strResult = new string[2];
Pen xPen = new Pen(Color.Red, 5);
// if a face is detected, we can recognize it
if (FSDK.FSDKE_OK == FSDK.DetectFace(imageHandle, ref facePosition))
{
gr.DrawRectangle(xPen, facePosition.xc -
facePosition.w / 2, facePosition.yc - facePosition.w / 2,
facePosition.w, facePosition.w);
//gr.DrawEllipse(xPen, facePosition.xc -
facePosition.w / 2, facePosition.yc - facePosition.w / 2,
facePosition.w, facePosition.w);
// create a new face template
byte[] template = new byte[FSDK.TemplateSize];
FSDK.GetFaceTemplateInRegion(imageHandle, ref facePosition, out template);
strResult = MatchTemplateWithDatabase(template);
// if face matches with database's face data
if (!strResult[0].Equals(""))
{
xVoice = new SpeechSynthesizer();
xVoice.Speak("Hello " + strResult[1]); //speak by name
xVoice.Dispose();
}
}
else // if there is no face in the screen, then background music will be played
{
objBkMusic.playMusic();
// doesn't work
}
picBox.Image = frameImage;
FSDK.FreeImage(imageHandle); // delete the FSDK image handle
DeleteObject(hbitmapHandle); // delete the HBITMAP object
GC.Collect(); // collect the garbage after the deletion
// make UI controls accessible
Application.DoEvents();
}
FSDKCam.CloseVideoCamera(cameraHandle);
FSDKCam.FinalizeCapturing();
}
///////////////////////
My problem is here
if (FSDK.FSDKE_OK == FSDK.DetectFace(imageHandle, ref facePosition))
{
//
//
}
else //
if there is no face in the screen, then background music will be played
{
objBkMusic.playMusic();
// doesn't work
}