该类System.Media.SystemSound不以这种方式使用,该类仅用于表示系统声音类型,例如Asterisk、Beep、或。可以通过播放系统声音。HandQuestionExclamationSystemSounds
例子
SystemSounds.Asterisk.Play();
这个例子将播放系统声音 Asterisk。这类似于System.Drawing.Brush你不能说System.Drawing.Brush.Black但你可以说System.Drawing.Brushes.Black。System.Drawing.Brush仅用于Black在新的名称类中将名称对象定义System.Drawing.Brushes为颜色Black。我们可以在定义中看到这一点System.Drawing.Brushes
//
// Summary:
// Gets a system-defined System.Drawing.Brush object.
//
// Returns:
// A System.Drawing.Brush object set to a system-defined color.
public static Brush Black { get; }
此外,如果您想播放新的Wave Sound,您可以创建一个新类,该类控制Sound Wave (.wav) 文件SoundPlayer中声音的播放
例子
SoundPlayer _SoundPlayer = new SoundPlayer(); //Initializes a new SoundPlayer of name _SoundPlayer
_SoundPlayer.SoundLocation = @"D:\Resources\International\Greetings.wav"; //Sets the target Wave Sound file to D:\...\Greetings.wav
_SoundPlayer.Play(); //Plays the target Wave Sound file
如果您仍想使用 Generic List。那么,也许下面的例子可以帮助你
例子
List<string> WaveSoundCollections = new List<string> { @"D:\Resources\International\Greetings.wav", @"D:\Resources\International\Good-Bye.wav" }; //Initializes a new Generic Collection of class List of type string and injecting TWO strings into the Generic Collection
SoundPlayer NewPlayer = new SoundPlayer(); //Initializes a new SoundPlayer
if (Client.Start) //Client refers to a particular Class that has Start and End as bool
{
NewPlayer.SoundLocation = WaveSoundCollections[0]; //Set the SoundLocation of NewPlayer to D:\Resources\International\Greetings.wav
NewPlayer.Play(); //Plays the target Wave Sound file
}
else if (Client.End)
{
NewPlayer.SoundLocation = WaveSoundCollections[1]; //Set the SoundLocation of NewPlayer to D:\Resources\International\Good-Bye.wav
NewPlayer.Play(); //Plays the target Wave Sound file
}
注意:SoundPlayer仅用于播放Wave Sound文件。如果您想播放其他媒体文件,例如.mp3、.mpeg等...然后,您可以使用System.Windows.Media.MediaPlayer可以从其中导入的文件Object Browser来创建新类MediaPlayer并播放特定文件。
例子
string TargetFile = @"D:\File.mp3";
MediaPlayer _MediaPlayer = new MediaPlayer();
_MediaPlayer.Open(new Uri(TargetFile));
_MediaPlayer.Play();
重要提示:要使用MediaPlayer,您必须添加WindowsBase.
要添加 的新引用WindowsBase,请尝试以下操作:
谢谢,
我希望你觉得这有帮助:)