views:

253

answers:

1

I have this:

ThreadPool.QueueUserWorkItem(new WaitCallback(FireAttackProc), fireResult);

and FireAttackProc:

    private void FireAttackProc(Object stateInfo)
    {
        // Process Attack/Fire (local)
        lock (_procLock)
        {
            // build status message
            String status = "(Away vs. Home)";

            // get Fire Result state info
            FireResult fireResult = (FireResult)stateInfo;

            // update home grid with attack information
            GameModel.HomeCellStatusSet(fireResult.FireGridLocation, Cell.cellState.Lock);
            this.Invoke(new Action(delegate() { RefreshHomeGrid(); }));

            status = status + "(Attack Coordinate: (" + GameModel.alphaCoords(fireResult.FireGridLocation.Column) +
                "," + fireResult.FireGridLocation.Row + "))(Result: ";

            // play audio data if true
            if (audio)
            {
                String Letters;

                Stream stream;
                SoundPlayer player;

                Letters = GameModel.alphaCoords(fireResult.FireGridLocation.Column);
                stream = Properties.Resources.ResourceManager.GetStream("_" + Letters);
                player = new System.Media.SoundPlayer(stream);
                player.PlaySync();


                Letters = fireResult.FireGridLocation.Row.ToString();
                stream = Properties.Resources.ResourceManager.GetStream("__" + Letters);
                player = new System.Media.SoundPlayer(stream);
                player.PlaySync();

                stream.Dispose();
                player.Dispose();
            }

            if (audio)
            {
                SoundPlayer fire = new SoundPlayer(Properties.Resources.fire);
                fire.PlaySync();
                fire.Dispose();
            }

            // deal with hit/miss
            switch (fireResult.Hit)
            {
                case true:
                    this.Invoke(new Action(delegate()
                    {
                        GameModel.HomeCellStatusSet(fireResult.FireGridLocation, Cell.cellState.Hit);
                        status = status + "(Hit)";
                    }));

                    if (audio)
                    {
                        SoundPlayer hit = new SoundPlayer(Properties.Resources.firehit);
                        hit.PlaySync();
                        hit.Dispose();

                    }
                    break;

                case false:
                    this.Invoke(new Action(delegate() 
                    {
                        GameModel.HomeCellStatusSet(fireResult.FireGridLocation, Cell.cellState.Miss);
                        status = status + "(Miss)";
                    }));

                    GameModel.PlayerNextTurn = NietzscheBattleshipsGameModel.GamePlayers.Home;

                    if (audio)
                    {
                        SoundPlayer miss = new SoundPlayer(Properties.Resources.firemiss);
                        miss.PlaySync();
                        miss.Dispose();
                    }
                    break;
            }

            // refresh home grid with updated data
            this.Invoke(new Action(delegate() { RefreshHomeGrid(); }));

            GameToolStripStatusLabel.Text = status + ")";

            // deal with ship destroyed
            if (fireResult.ShipDestroyed)
            {
                status = status + "(Destroyed: " + GameModel.getShipDescription(fireResult.DestroyedShipType) + ")";

                if (audio)
                {
                    Stream stream;
                    SoundPlayer player;

                    stream = Properties.Resources.ResourceManager.GetStream("_home");
                    player = new System.Media.SoundPlayer(stream);
                    player.PlaySync();
                    player.Dispose();
                    stream.Dispose();

                    string ShipID = fireResult.DestroyedShipType.ToString();
                    stream = Properties.Resources.ResourceManager.GetStream("_" + ShipID);
                    player = new System.Media.SoundPlayer(stream);
                    player.PlaySync();
                    player.Dispose();
                    stream.Dispose();

                    stream = Properties.Resources.ResourceManager.GetStream("_destroyed");
                    player = new System.Media.SoundPlayer(stream);
                    player.PlaySync();
                    player.Dispose();
                    stream.Dispose();
                }
            }

            // deal with win condition
            if (fireResult.Win)
            {
                if (audio)
                {
                    Stream stream;
                    SoundPlayer player;

                    stream = Properties.Resources.ResourceManager.GetStream("_home");
                    player = new System.Media.SoundPlayer(stream);
                    player.PlaySync();
                    player.Dispose();

                    stream = Properties.Resources.ResourceManager.GetStream("_loses");
                    player = new System.Media.SoundPlayer(stream);
                    player.PlaySync();
                    player.Dispose();
                }

                GameModel.gameContracts = new GameContracts();
            }

            // update status message
            if (fireResult.Hit)
            {
                if (!fireResult.Win)
                {
                    status = status + "(Turn: Away)";

                    LockGUIControls();
                }
            }

            // deal with turn logic
            if (GameModel.PlayerNextTurn == NietzscheBattleshipsGameModel.GamePlayers.Home)
            {
                this.Invoke(new Action(delegate()
                {
                    if (!fireResult.Win)
                    {
                        status = status + "(Turn: Home)";

                        AwayTableLayoutPanel.Enabled = true;
                    }
                }));
            }

            // deal with win condition
            if (fireResult.Win)
            {
                this.Invoke(new Action(delegate()
                {
                    status = status + "(Game: Home Loses)";

                    CancelToolStripMenuItem.Enabled = false;
                    NewToolStripMenuItem.Enabled = true;
                    LockGUIControls();
                }));
            }

            // display completed status message
            GameToolStripStatusLabel.Text = status + ")";
        }
    }

The issue is this:

Under Vista/win7 the sound clips in the FireAttackProc plays.

But under XP the logic contained within FireAttackProc gets executed but none of the sound clips play.

Is there a quick solution to this so the sound will play under XP?

I ask for a quick solution because i am happy being able to execute fully in Vista/Win7 but would be great if there was a quick solution so it would be XP compitable also.

Thank you.

+1  A: 

SoundPlayer is really quite lackluster, but that's all .NET 2.0 comes with. Try my player (based on this article) instead, which uses MCI to play sounds:

http://pastebin.com/aVDWBJ45

You can use other audio codecs (such as MP3), and you only have to load the file once (and not every time you attack, which incurs a delay). And you can play sounds asynchronously without creating additional threads.

It is really easy to use. Simply create a new QueuedSamplesPlayer with a generic argument of whatever you'd like to identify sounds (such as an enum, a string or an int). Use the AddSample method to load all your sounds on startup. Then use Play or PlayAsync to play the file synchronously or asynchronously, respectively.

You can call PlayAsync multiple times and the sounds will play in order, one after another (without blocking your current thread). You can even call PlayAsync while sound is playing, and it will be added to the sound queue to be played. When all sounds have finished playing, a QueueEmpty event will be raised.

This player has been tested on Windows XP, Vista and 7.

Allon Guralnek