Click here to Skip to main content
15,886,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
// 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.

C#
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

         }
Posted
Updated 23-Oct-10 23:33pm
v4

I would suggest that you test the return code from mciSendString. That should tell you why that call is failing (assuming it is, of course). You can also use the mciGetErrorString function to get more information.
 
Share this answer
 
Your architecture is wrong, you are trying to do face detection (a long calculation) on the main thread, making your app unresponsive. Adding some Application.DoEvent() calls is not the solution, you need to use a separate thread, maybe a BackgroundWorker.

PS: I also dislike your GC.Collect(); if you think you need it, you probably are handling your objects in the wrong way too.

:)
 
Share this answer
 
C#
public void Play(bool loop)
        {
            long a = 0;
            if (isOpen)
            {
                _command = "play MediaFile";
                if (loop)
                    _command += " REPEAT";
                a = mciSendString(_command, null, 0, IntPtr.Zero);
            }
        }

it returns 4476859547532656903; What does it mean?
 
Share this answer
 
Comments
0x3c0 24-Oct-10 10:45am    
Please edit your additions into the main question - it makes it easier to locate. As for your question - pass the return value to mciGetErrorString.
For what it's worth, mciSendString returns an integer, not a long. Your value is going to be odd. Fix this first.
Toli Cuturicu 26-Oct-10 8:18am    
Fake answer!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900