xna声音效果无法加载

时间:2017-02-01 20:40:58

标签: c# audio xna monogame

我无法加载XNA音效。我尝试通过cwd选择文件以及完整的静态目录,但是当我尝试构建时,声音效果仍然显示为null。我已经三次检查文件是否在正确的位置,以及它是否包含在项目中,但它仍然无法识别。

以下是我正在使用的代码:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace Prototype0217
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Color clearColor = Color.CornflowerBlue;
        SoundEffect noise;
        string cwd = System.IO.Directory.GetCurrentDirectory();
        string ngnl1 = "A:\\DrawingBoard\\Prototype0217\\Prototype0217\\Content\\ngnl1.mp3";

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();


        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            noise = Content.Load<SoundEffect>("Content\\ngnl.mp3");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
                clearColor = Color.Green;

            if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
                clearColor = Color.Red;

            if (GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed)
                clearColor = Color.Blue;

            if (GamePad.GetState(PlayerIndex.One).Buttons.Y == ButtonState.Pressed)
                clearColor = Color.Orange;

            if (GamePad.GetState(PlayerIndex.One).Buttons.RightShoulder == ButtonState.Pressed)
                noise.Play();
            // TODO: Add your update logic here



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(clearColor);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

0 个答案:

没有答案