0

你好如何List<SystemSound>从资源中创建一个音乐?

我试试这个,但我得到以下异常:

    List<System.Media.SystemSound> music = new List<System.Media.SystemSound>
    {
        global::Launcher.Properties.Resources.music_quit_1,
        global::Launcher.Properties.Resources.music_quit_2,
        global::Launcher.Properties.Resources.music_quit_3,
        global::Launcher.Properties.Resources.music_quit_4,
    };

“参数 1:无法从 System.IO.UnmanagedMemoryStream 转换为 System.Media.SystemSound。”

从那我会随机播放音乐并播放它。

4

1 回答 1

1

该类System.Media.SystemSound不以这种方式使用,该类仅用于表示系统声音类型,例如AsteriskBeep、或。可以通过播放系统声音HandQuestionExclamationSystemSounds

例子

SystemSounds.Asterisk.Play();

这个例子将播放系统声音 Asterisk。这类似于System.Drawing.Brush你不能说System.Drawing.Brush.Black但你可以说System.Drawing.Brushes.BlackSystem.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,请尝试以下操作:

  • 右键单击解决方案资源管理器中的引用并选择添加引用...
  • 选择WindowsBase并单击OK
  • 确保您刚刚添加的新引用将其名称Copy Local的 bool 属性设置为True,以便客户端在初始化期间不会遇到错误

     在这种情况下,将 CopyLocal 设置为 True 很重要

谢谢,
我希望你觉得这有帮助:)

于 2012-11-11T15:45:03.953 回答