简体   繁体   中英

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

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'. 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'

Sorry is this is a dumb question, I am just starting out. If it makes any difference, I am using Windows 8.1 N.

EDIT: I guess I fixed it. Apparently you cannot add resources by going (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 在此处输入图片说明

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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