3

私はこれを持っています:

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

および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 + ")";
        }
    }

問題はこれです:

Vista / win7では、FireAttackProcのサウンドクリップが再生されます。

ただし、XPでは、FireAttackProcに含まれるロジックは実行されますが、サウンドクリップは再生されません。

XPでサウンドが再生されるようにするための簡単な解決策はありますか?

Vista / Win7で完全に実行できることを嬉しく思うので、迅速な解決策を求めますが、迅速な解決策があれば、XPにも対応できるので素晴らしいでしょう。

ありがとうございました。

4

1 に答える 1

1

SoundPlayerは非常に光沢がありませんが、.NET2.0に付属しているのはそれだけです。代わりに、MCIを使用してサウンドを再生するプレーヤー(この記事に基づく)を試してください。

http://pastebin.com/aVDWBJ45

他のオーディオコーデック(MP3など)を使用することができ、ファイルをロードする必要があるのは1回だけです(攻撃するたびにではなく、遅延が発生します)。また、追加のスレッドを作成せずに、非同期でサウンドを再生できます。

本当に使いやすいです。QueuedSamplesPlayerサウンドを識別したいもの(、、、、など)の一般的な引数を使用して新しいを作成enumするだけです。このメソッドを使用して、起動時にすべてのサウンドをロードします。次に、またはを使用して、ファイルをそれぞれ同期または非同期で再生します。stringintAddSamplePlayPlayAsync

PlayAsync複数回呼び出すことができ、サウンドは順番に(現在のスレッドをブロックせずに)再生されます。サウンドの再生中に電話をかけることもできPlayAsync、サウンドキューに追加されて再生されます。すべてのサウンドの再生が終了すると、QueueEmptyイベントが発生します。

このプレーヤーは、Windows XP、Vista、および7でテストされています。

于 2010-04-26T08:29:56.793 に答える