Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从资源中播放WAV音频文件?_C#_Visual Studio_Audio - Fatal编程技术网

C# 如何从资源中播放WAV音频文件?

C# 如何从资源中播放WAV音频文件?,c#,visual-studio,audio,C#,Visual Studio,Audio,如何从项目资源中播放WAV音频文件?我的项目是C语言中的Windows窗体应用程序。因为mySoundFile是一个流,所以您可以利用SoundPlayer的重载构造函数,它接受流对象: 您需要小心垃圾收集器在声音仍在播放时释放声音使用的内存。虽然它很少发生,但当它发生时,你只会玩一些随机记忆。这里有一个解决方案,包括实现您所需的源代码: 滚动至社区内容部分的最底部 Stream str = Properties.Resources.mySoundFile; RecordPlayer r

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

因为mySoundFile是一个流,所以您可以利用SoundPlayer的重载构造函数,它接受流对象:


您需要小心垃圾收集器在声音仍在播放时释放声音使用的内存。虽然它很少发生,但当它发生时,你只会玩一些随机记忆。这里有一个解决方案,包括实现您所需的源代码:

滚动至社区内容部分的最底部

  Stream str = Properties.Resources.mySoundFile;
  RecordPlayer rp = new RecordPlayer();
  rp.Open(new WaveReader(str));
  rp.Play();

从。

当您必须在项目中添加声音时,您可以通过播放.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;使用我的\u wave\u文件.playsync

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

从菜单工具栏视图打开解决方案资源管理器,或只需按Ctrl+Alt+L。 单击属性下拉列表。 然后选择Resource.resx并按enter键。 现在从组合框列表中选择音频。 然后单击添加资源,选择音频文件.wav并单击打开。 选择音频文件并将持久性属性更改为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并享受您的声音。
祝你一切顺利

据我所知,有两种方法,如下所示:

使用文件路径 首先将文件放在项目的根文件夹中,然后无论您在调试或发布模式下运行程序,都可以确保访问该文件。然后使用SoundPlayer类来播放它。 但通过这种方式,如果要将项目发布给用户,则需要复制声音文件及其文件夹层次结构,而不是bin目录下的文件夹发布层次结构

        var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
        SoundPlayer player = new SoundPlayer();
        player.SoundLocation = Path.Combine(basePath, @"./../../Reminder.wav");
        player.Load();
        player.Play();
使用资源 按照下面的动画操作,将现有文件添加到项目中

这种方式的优点是:
运行程序时,只需复制bin目录下的文件夹版本。

这两行可以完成此操作:

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

这将在Windows CE中引发异常,因为它不会自动将资源从字节[]转换为流。我发现下面的答案在这种情况下有效。把它留给其他人:我们实际上不需要声明一个单独的流变量@托梅恩斯:当然,但它向人们展示了资源的类型和所使用的SoundPlayer构造函数的重载。这个答案比标记的答案好,因为你不需要额外的可能过时的类。同样,作者也输入了名称空间。这正是我想要的!这些照片帮助很大。你知道我是否可以通过这种方式嵌入fontawesome这样的字体,以便在我的程序中使用吗?当我添加wav文件时,它会添加一个Resources2.Designer.cs,它似乎是Resources.Designer.cs的副本,从而给我带来冲突。这才刚刚开始,你知道会发生什么吗?
        SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);
        player.Play();
SoundPlayer sound = new SoundPlayer(Properties.Resources.solo);     
                sound.Play();