[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why playSound (API) can't run in a BackgroundWorker object.

Posted on 2006-05-30
6
Medium Priority
?
669 Views
Last Modified: 2012-06-22
I'm trying to write a project that alow a thread can be play a list of sound file.

So I use write a playsound class. For simple here, I've just use the PlaySound API and have a class like this:
===============
    class MCIPlaySound
    {
        #region "DLL Import command"
        [DllImport("winmm.dll")] //inports the winmm.dll used for sound
        public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags);
    }
==============

When I use this class to play a sound in a main function, every thing is OK.
---------------------
            MCIPlaySound.PlaySound("sounds_cache\\hoa2.wav", IntPtr.Zero, PlaySoundFlags.SND_SYNC);
----------------------

But for my perpose I have to use a thread to play a list of file, and I create a Class that inherited from BackgroundWorker like this:
==============
    class test : BackgroundWorker
    {
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            MCIPlaySound.PlaySound("sounds_cache\\hoa2.wav", IntPtr.Zero, PlaySoundFlags.SND_SYNC);
            MCIPlaySound.PlaySound("sounds_cache\\d9a8ng5.wav", IntPtr.Zero, PlaySoundFlags.SND_SYNC);
            MCIPlaySound.PlaySound("sounds_cache\\nho7n.wav", IntPtr.Zero, PlaySoundFlags.SND_SYNC);
            base.OnDoWork(e);
        }
    }
==============

and I call it in the main function:
--------------
            test t = new test();
            t.RunWorkerAsync();
--------------
But there is something wrong! I can't hear any things!

So why when I use playSound directly it is work but when use it in a thread with the same way, it doesn't work?

Please so me the way to solve this problem to finish my project!

Thanks for read this!
0
Comment
Question by:tunhien
  • 4
6 Comments
 
LVL 5

Accepted Solution

by:
eltic earned 700 total points
ID: 16799226
Why don't you use the flag SND_ASYNC (0x0001) to play the file asynchronously ?
0
 

Author Comment

by:tunhien
ID: 16799607
Just because I need it run synchronous! File to file!

And I don't know why! When I start a new project for test the mci Class! The PlaySound function above is work!

But I don't really want to use it to play my sound! So that I use the mciSendString to play file instead of the playSound like above. And It's still not work!

MCIPlaySound Class is implemented like this:
====================================
    class MCIPlay
    {
        [DllImport("winmm.dll", EntryPoint = "mciSendStringA",
            CharSet = CharSet.Ansi)]
        public static extern int mciSendString(string lpszCommand,
            StringBuilder lpszReturnString, int cchReturn, IntPtr hwndCallback);

        public static long Play(string strFilePath, bool bWait)
        {
            string strCommand;
            long lStatus;

            strCommand = string.Format("open \"{0}\" type mpegvideo Alias nhon_hoa", strFilePath);
            lStatus = mciSendString(strCommand, null, 0, IntPtr.Zero);

            strCommand = "play nhon_hoa";
            if (bWait)
                strCommand += " wait";
           
            lStatus = mciSendString(strCommand, null, 0, IntPtr.Zero);

            if (bWait)
            {
                lStatus = mciSendString("close nhon_hoa", null, 0, IntPtr.Zero);
            }
            return lStatus;
        }        
====================================

and the inherited BackgroundWorker class to play sound files is:
=====================================
    class test2 : BackgroundWorker
    {
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            MCIPlay.Play("E:\\_NNLT_HD\\_phat_am\\SoundsWS\\App_Data\\sounds\\hoa2.wav", true); // This not work!
            MCIPlay.Play("G:\\Internet\\KaZaDownLoad\\Coldplay - Fix You.mp3", true); // This not work too!
        }
    }
=====================================

and the main function to call an run:
=====================================
        [STAThread]
        static void Main()
        {
            MCIPlay.Play("G:\\Internet\\KaZaDownLoad\\Coldplay - Fix You.mp3", true); // This code is work
            test2 t = new test2();
            t.RunWorkerAsync();    // The same calling with static Play function but not work!!!!
            Application.Run(new Form1());
        }
=====================================
0
 

Author Comment

by:tunhien
ID: 16799729
I don't know Why! If I Add this fucntion into MCIPlay class:

====================================
        public static long Open(string strFilePath)
        {
            string strCommand;
            long lStatus;

            strCommand = string.Format("open \"{0}\" type mpegvideo Alias nhon_hoa", strFilePath);
            lStatus = mciSendString(strCommand, null, 0, IntPtr.Zero);
            m_bIsOpened = (lStatus == 0);
            return lStatus;
        }
====================================

and in the main function I use:
=====================================
        [STAThread]
        static void Main()
        {
            MCIPlay.Open("G:\\Internet\\KaZaDownLoad\\Coldplay - Fix You.mp3"); // This code is changed from Play into Open!!!

            test2 t = new test2();
            t.RunWorkerAsync();    // Now it work!
            Application.Run(new Form1());
        }
=====================================


is any one can explain why this happen?
Thanks
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:tunhien
ID: 16834297
I've solve it! Please delete this question!
Thanks
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16834715
Please post how you solved the problem along with any relevant code.

Then post a request here for a PAQ/Refund:
http://www.experts-exchange.com/Community_Support/
0
 

Author Comment

by:tunhien
ID: 16834829
For this problem!
done as what I asked before!
that is:
use this:
MCIPlay.Open("G:\\Internet\\KaZaDownLoad\\Coldplay - Fix You.mp3"); // This code is changed from Play into Open!!!
=====================================
        [STAThread]
        static void Main()
        {
            MCIPlay.Open("G:\\Internet\\KaZaDownLoad\\Coldplay - Fix You.mp3"); // This code is changed from Play into Open!!!

            test2 t = new test2();
            t.RunWorkerAsync();    // Now it work!
            Application.Run(new Form1());
        }
=====================================
before Play with background!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question