【问题标题】:How to play WAV audio file from Resources?如何播放资源中的 WAV 音频文件?
【发布时间】:2011-05-06 18:11:08
【问题描述】:

如何从项目的资源中播放 WAV 音频文件?我的项目是 C# 中的 Windows 窗体应用程序。

【问题讨论】:

标签: c# visual-studio audio


【解决方案1】:
Stream str = Properties.Resources.mySoundFile;
RecordPlayer rp = new RecordPlayer();
rp.Open(new WaveReader(str));
rp.Play();

来自How to play WAV audio file from resources in C#

【讨论】:

    【解决方案2】:

    当您必须在项目中添加声音时,您可以通过播放 .wav 文件来实现。然后你必须像这样添加.wav 文件。

    using System.Media; //write this at the top of the code
    
    SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav");
    my_wave_file.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish
    

    记住,文件的路径必须用正斜杠/)格式写,不要使用反斜杠\)给出文件的路径,否则会出错。

    另请注意,如果您希望在播放声音时发生其他事情,您可以将my_wave_file.PlaySync(); 更改为my_wave_file.PlayAsync();

    【讨论】:

      【解决方案3】:

      这两行可以做到:

      SoundPlayer sound = new SoundPlayer(Properties.Resources.solo);     
      sound.Play(); 
      

      【讨论】:

        【解决方案4】:

        据我所知,有两种方法可以做到这一点,请在下面列出:

        1. 使用文件路径

        先把文件放到项目的根目录下,然后不管你在Debug或者Release模式下运行程序,都可以肯定的访问到文件。然后使用 SoundPlayer 类来玩它。
        但是这样的话,如果你想将项目发布给用户,你需要复制声音文件及其文件夹层次结构,除了“bin”目录下的“Release”文件夹。

        var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
        SoundPlayer player = new SoundPlayer();
        player.SoundLocation = Path.Combine(basePath, @"./../../Reminder.wav");
        player.Load();
        player.Play();
        
        1. 使用资源

        按照下面的动画,将“现有文件”添加到项目中。

        SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);
        player.Play();
        

        这种方式的优势在于:
        运行程序时只需要复制“bin”目录下的“Release”文件夹即可。

        【讨论】:

          【解决方案5】:

          a) 好的,首先将音频文件 (.wav) 添加到项目资源中。

          1. 从菜单工具栏(“视图”)打开“解决方案资源管理器”或直接按 Ctrl+Alt+L。
          2. 点击“属性”下拉列表。
          3. 然后选择“Resource.resx”并回车。

          1. 现在从组合框列表中选择“音频”。

          1. 然后点击“添加资源”,选择音频文件(.wav)并点击“打开”。

          1. 选择音频文件并将“Persistence”属性更改为“Embedded in .resx”。

          b) 现在,只需编写这段代码来播放音频。

          在这段代码中,我在表单加载事件中播放音频。

          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows.Forms;
          
          using System.Media; // at first you've to import this package to access SoundPlayer
          
          namespace WindowsFormsApplication1
          {
              public partial class login : Form
              {
                  public login()
                  {
                      InitializeComponent();
                  }
          
                  private void login_Load(object sender, EventArgs e)
                  {
                      playaudio(); // calling the function
                  }
          
                  private void playaudio() // defining the function
                  {
                      SoundPlayer audio = new SoundPlayer(WindowsFormsApplication1.Properties.Resources.Connect); // here WindowsFormsApplication1 is the namespace and Connect is the audio file name
                      audio.Play();
                  }
              }
          }
          

          就是这样。
          全部完成,现在运行项目(按 f5)并享受您的声音。
          一切顺利。 :)

          【讨论】:

          • 正是我想要的!图片帮了大忙。你知道我是否可以以这种方式嵌入字体(如 fontawesome)以在我的程序中使用?
          • 当我添加一个 wav 文件时,它会添加一个 Resources2.Designer.cs,这似乎是 Resources.Designer.cs 的副本 - 从而给我带来了冲突。这刚刚开始发生。任何想法可能会发生什么? screencast.com/t/xAVpE5v6b0
          【解决方案6】:

          因为mySoundFileStream,您可以利用SoundPlayer 的重载构造函数,它接受Stream 对象:

          System.IO.Stream str = Properties.Resources.mySoundFile;
          System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str);
          snd.Play();
          

          SoundPlayer Class Documentation (MSDN)

          【讨论】:

          • 这将在 Windows CE 中引发异常,因为它不会自动将资源从 byte[] 转换为流。我发现以下答案在这种情况下有效。留给其他人:stackoverflow.com/questions/1900707/…
          • 我们实际上不需要声明一个单独的 Stream 变量 ;)
          • @TomeeNS:当然,但它向人们展示了资源的类型以及所使用的SoundPlayer 构造函数的重载。
          • 此答案比标记的答案更好,因为您不需要可能已过时的额外课程。作家也确实输入了命名空间。
          【解决方案7】:

          当声音仍在播放时,您需要小心垃圾收集器释放声音使用的内存。虽然它很少发生,但当它发生时,你只是在玩一些随机记忆。有一个解决方案,完整的源代码可以在这里实现你想要的:http://msdn.microsoft.com/en-us/library/dd743680(VS.85).aspx

          滚动到最底部的“社区内容”部分。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-06-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多