简体   繁体   English

C#嵌入式音频无法播放

[英]C# Embedded Audio Not Playing

I have embedded an audio file (high.wav) in the resx file for my Windows Form (by just double clicking on Form1.resx in solution explorer, Ctrl+F4, then clicking 'Add Resource'), but using 我已经在Windows窗体的resx文件中嵌入了一个音频文件(high.wav)(只需在解决方案资源管理器中按Ctrl + F4双击Form1.resx,然后单击“添加资源”),但是使用

Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.ResourceManager.GetStream("high");
SoundPlayer sp = new SoundPlayer(embeddedfile);
sp.Play();

only plays a system sound, not the embedded one. 仅播放系统声音,而不播放嵌入式声音。 I have tried all of the combinations of changing the Persistence from 'Linked at compile time' to 'Embedded in .resx', and Build Action from 'None' to 'Embedded Resource'. 我尝试了将“持久性”从“编译时链接”更改为“嵌入.resx”并将“构建操作”从“无”更改为“嵌入资源”的所有组合。 I have also tried 我也尝试过

Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.high;

which doesn't even compile, saying that 'WindowsFormsApplication2.Properties.Resources' does not contain a definition for 'high' 甚至没有编译,说“ WindowsFormsApplication2.Properties.Resources”不包含“ high”的定义

Sorry is this is a dumb question, I am just starting out. 抱歉,这是一个愚蠢的问题,我才刚开始。 If it makes any difference, I am using Windows 8.1 N. 如果有什么不同,我正在使用Windows 8.1N。

EDIT: I guess I fixed it. 编辑:我想我已修复它。 Apparently you cannot add resources by going (formname).cs --> (formname).resx . 显然,您无法通过(formname).cs->(formname).resx添加资源。 You must go right click the application name, go to properties, and on the sidebar thing go to resources. 您必须右键单击应用程序名称,转到属性,然后在边栏上转到资源。 I don't know what the difference is, as things added the way that works shows up in the place where it doesn't work to add it. 我不知道有什么区别,因为添加的东西的工作方式显示在添加它不起作用的地方。

For adding resource right click on the project name from project explorer then click on resources after add resource arrow ==> add Existing file from anyware 要添加资源,请右键单击项目资源管理器中的项目名称,然后在添加资源箭头后单击资源==>从anyware添加现有文件 在此处输入图片说明

and this is code for playing this file added: 这是添加播放此文件的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Media;

namespace play_Wav
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SoundPlayer sp = new SoundPlayer(play_Wav.Properties.Resources.myFile);
            sp.Play();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM