2012-02-03 51 views
0

我正在与音效声卡和我越来越:nullReferenceException当我尝试播放音效?

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

我确实有它的工作,但改变(覆盖了以前的版本)后,我已经收到此错误和我卡。 Java是我的第一语言,使用C#我是初学者。我花了无数小时寻找解决方案。

nullReferenceException来自loadsound(),我想!我在名为resources和build action的文件夹中有声音文件(.wav):resources和copy to output:不要复制(尝试所有选项)。此外,在参考文献中提到了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 

    } 
} 
+0

'LoadSound'是抛出的错误吗? – Xaisoft 2012-02-03 21:42:41

+0

我认为这是在方法结束的时候,我也得到了“不能播放,声音文件问题”,但是如果声音不加载,那么以后是不可避免的。 – 2012-02-03 21:56:49

回答

0

废除我的原始答案,除非您使用XNA在Windows/Xbox上进行游戏开发。 (原来的海报正在使用它的Zune的。)

关于WAV文件:

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

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

+0

嗨,所以声音文件属性应设置为正确的内容? – 2012-02-04 09:35:43

+0

您好zenexer似乎已摆脱或nullreference异常ty ...但它给出一个新的错误:Microsoft.Xna.Framework.dll中发生未处理的异常的类型'System.InvalidOperationException',并且此行突出显示:[代码] //从流中创建SoundEffect Sound = SoundEffect.FromStream(SoundFileInfo.Stream); – 2012-02-04 10:06:45

+0

如果我注释掉所有loadsound(“resources/**** .wav”,out ****);除了1 = loadsound(“resources/drink.wav”,out drink)并且在“private void button_Click”方法中注释掉相应的if之后,它工作正常吗?取消注释,然后再次'System.InvalidOperationException'?我有25个音效,每个音效不超过6秒。有任何想法吗? – 2012-02-04 12:11:57

0

您正在采取艰难的做法。查看ContentManager(可从Game实例中作为this.Content访问)。您可以使用Content.Load<T>(string)来访问您的音频文件,但您需要将音频文件放入内容项目中。不要更改其类型:将其保留为默认值。即使它不是一种资源,放弃它。它将被编译成不同的格式。另外,省略传递给Content.Load<>的参数的文件扩展名。

+0

ty回复的人, – 2012-02-03 22:12:11

+0

这就是为什么我得到nullreferenceexception?是xna.framework的contentManager部分?有没有办法修复现有的代码,因为我实际上有它的工作,直到有些变化阻止它从工作,我不知道哪个更改完成它? – 2012-02-03 22:15:47

+0

ContentManager是XNA框架的一部分,是的。你应该专注于学习使用它,而不是修复你现有的代码。它具有很多优点,包括简单性。 – Zenexer 2012-02-03 22:18:47

0

你开始你正在加载的声音在比赛之前我几乎同样的问题

gameTimer.Start(); 

FrameworkDispatcher.Update(); 

LoadSound("Resources/drink.wav", out drink); 
+0

你的意思是gametimer.start();是在错误的位置或frameworkDispatcher.update(); ? – 2012-02-04 09:37:44

+0

我会在哪里开始游戏?我将gametimer()移动到loadsound方法,但没有运气。 – 2012-02-04 10:17:23

0

。最后我得到了它无意中如下:

StreamResourceInfo streaminfo = Application.GetResourceStream(new Uri("project_name;component/folder_name/Sound3.wav", UriKind.RelativeOrAbsolute)); 
SoundEffect effect1 = SoundEffect.FromStream(streaminfo.Stream); 
effect1.Play(); 

AND: 属性“建设行动” .wav文件必须设置好的,以“资源”和Porperty“复制到输出目录”应设置好的来“复制如果更新“

顺便说一句,我正在使用Silverlight5。