2

この同じトピックに関するスレッドがたくさんあることは知っていますが、まだ理解できない理由で、これはうまくいきません。

私はこのプロジェクトツリーを持っています:

プロジェクトツリー

Project->Properties->Resources メニューから、alarm.wav を .resx ファイルに埋め込みました。

コードのさまざまな組み合わせを試しましたが、何も機能しません。

現時点では、これは私が試しているコードです。

using System;
using System.Media;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.ComponentModel;
using System.Resources;
using AlarmForm;

namespace Alarm
{
    public partial class Form1 : Form
    {
        private bool estado = false;
        private SoundPlayer sonido;

        public Form1()
        {
            InitializeComponent();
            ResourceManager resources = new ResourceManager(typeof(Form1));
            sonido = new SoundPlayer(resources.GetStream("alarma"));
        }
    }
}

コンパイル時または実行時にエラーは表示されませんが、サウンドの代わりにエラー ビープが聞こえます。

編集済み: Alarm.Propertiesを使用しようとして見つけたエラー

Alarm.Properties エラー

4

1 に答える 1

2

resources.GetStream()を使用してファイルを直接リンクできるのに、なぜ使用しようとしているのAlarm.Propertiesですか? ずっと簡単だと思います。sonido新しいSoundPlayer. _ 使用方法を示す簡単な例を次に示します。SoundPlayer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Media;
using AlarmForm.
using AlarmForm.Properties; //Required to call 'Resources' directly

namespace Alarm
{
    public partial class Form1 : Form
    {
        private bool estado = false;
        private SoundPlayer sonido;

        public Form1()
        {
            InitializeComponent();
            //ResourceManager resources = new ResourceManager(typeof(Form1)); //We do not actually need this
            sonido = new SoundPlayer(Resources.alarma); //Initialize a new SoundPlayer linked to our sound file (or Alarm.Properties.Resources.alarma if Alarm.Properties was not imported)
            sonido.Play(); //Required if you would like to play the file
        }
    }
}

次のことに注意してください: 呼び出しようとしている voidが静的である場合、名前の新しいクラスを表すUNLESS の下で定義されているためSoundPlayer、実行することでいつでも再生を停止できます。sonido.Stop()sonidoSoundPlayerpublic partial class Form1: Formsonido

ありがとう、
これがお役に立てば幸いです:)

于 2012-11-02T01:00:41.853 に答える