当我尝试播放音效时出现 nullReferenceException? [英] nullReferenceException when i try to play a sound effect?

查看:33
本文介绍了当我尝试播放音效时出现 nullReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个带有音效的音板,我得到了:

I'm making a soundboard with sound effects and I'm getting :

"A first chance exception of type 'System.NullReferenceException'" 
   when building after 'UI Task' (Managed): 
   Loaded 'Microsoft.Xna.Framework.dll'

我确实让它工作了,但在更改(覆盖以前的版本)后,我遇到了这个错误,我被卡住了.Java 是我的第一门语言,而 C# 是我的初学者.我花了无数个小时寻找解决方案.

I did have it working but after changes (overwrote previous version) I have been getting this error and I'm stuck. Java is my first language and with C# I'm a beginner. I have spent countless hours looking for a solution.

nullReferenceException 来自loadsound(),我想!我在名为资源的文件夹中有声音文件(.wav)并构建操作:资源并复制到输出:不要复制(在这里尝试了所有选项).在参考文献中还提到了 Microsoft.Xna.Framework

The nullReferenceException is coming from loadsound(), I think! I have the sound files(.wav) in a folder called resources and build action:resources and copy to output:do not copy(have tried all options here). Also in references a reference was made to Microsoft.Xna.Framework

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Resources;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Craggy_Island
{
    public partial class MainPage : PhoneApplicationPage
    {
        // The Resources to play
        private SoundEffect drink;//(plus 23 more effects)

        // Flag that indicates if we need to resume Zune playback upon exiting.
        bool resumeMediaPlayerAfterDone = false;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Timer to simulate the XNA game loop (SoundEffect class is from the XNA Framework)
            GameTimer gameTimer = new GameTimer();
            gameTimer.UpdateInterval = TimeSpan.FromMilliseconds(33);

            // Call FrameworkDispatcher.Update to update the XNA Framework internals.
            gameTimer.Update += delegate { try { FrameworkDispatcher.Update(); } catch { } };

            // Start the GameTimer running.
            gameTimer.Start();

            // Prime the pump or we'll get an exception.
            FrameworkDispatcher.Update();

            //LoadSound("Resources/drink.wav", out drink);
            // Create and load SoundEffect objects.
            LoadSound("Resources/drink.wav", out drink);

        }

        private void LoadSound(String SoundFilePath, out SoundEffect Sound)
        {
            // For error checking, assume we'll fail to load the file.
            Sound = null;

            try
            {
                // Holds informations about a file stream.
                StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));

                // Create the SoundEffect from the Stream
                Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
            }
            catch (NullReferenceException)
            {
                // Display an error message
                MessageBox.Show("Couldn't load sound " + SoundFilePath);
            }
        }



        private void button_Click(object sender, RoutedEventArgs e)
        {
            Button Current = sender as Button;

            try
            {
                if (Current.Equals(button1))
                    drink.Play();//(other buttons here for other sound effects)



            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Can't play, sound file problem.");
            }

        }
        #region Zune Pause/Resume

        private void ZunePause()
        {
            // Please see the MainPage() constructor above where the GameTimer object is created.
            // This enables the use of the XNA framework MediaPlayer class by pumping the XNA FrameworkDispatcher.

            // Pause the Zune player if it is already playing music.
            if (!MediaPlayer.GameHasControl)
            {
                MediaPlayer.Pause();
                resumeMediaPlayerAfterDone = true;
            }
        }

        private void ZuneResume()
        {
            // If Zune was playing music, resume playback
            if (resumeMediaPlayerAfterDone)
            {
                MediaPlayer.Resume();
            }
        }

        #endregion Zune Pause/Resume

    }
}

推荐答案

除非您在 Windows/Xbox 上使用 XNA 进行游戏开发,否则请放弃我原来的答案.(原海报是在 Zune 上使用它.)

Scrap my original answer unless you're using XNA for game development on Windows/Xbox. (Original poster is using it for the Zune.)

关于WAV文件:

第一个问题是您需要将Copy to Output Directory设置为Copy if newer.(您可以使用 Always,但这是不必要的.)

First problem is that you need to set Copy to Output Directory to Copy if newer. (You could use Always, but that would be unnecessary.)

第二个问题是它的类型需要设置为Content.

Second problem is that its type needs to be set to Content.

这篇关于当我尝试播放音效时出现 nullReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆