我正在制作一个带有声音效果的音板,我正在接受:
"A first chance exception of type 'System.NullReferenceException'"
when building after 'UI Task' (Managed):
Loaded 'Microsoft.Xna.Framework.dll'
我确实让它工作但是在更改之后(覆盖了以前的版本)我遇到了这个错误而且我被卡住了。 Java是我的第一语言,使用C#我是初学者。我花了无数个小时寻找解决方案。
我认为nullReferenceException来自loadsound()!我将声音文件(.wav)放在一个名为resources和build action的文件夹中:资源并复制到输出:不要复制(在这里尝试了所有选项)。另外在参考文献中引用了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 :(得分:0)
你正在采取艰难的方法。查看ContentManager
(可从this.Content
实例中访问Game
)。您可以使用Content.Load<T>(string)
访问音频文件,但是您需要将音频文件放在内容项目中。请勿更改其类型:将其保留为默认值。即使它不是一种资源,也不是它。它将被编译成不同的格式。另外,从传递给Content.Load<>
的参数中省略文件扩展名。
答案 1 :(得分:0)
除非您在Windows / Xbox上使用XNA进行游戏开发,否则请删除原始答案。 (原始海报正在将它用于Zune。)
关于WAV文件:
首先,您需要将Copy to Output Directory
设置为Copy if newer
。 (您可以使用Always
,但这是不必要的。)
第二个问题是它的类型需要设置为Content
。
答案 2 :(得分:0)
在加载声音之前,您正在开始游戏:
gameTimer.Start();
FrameworkDispatcher.Update();
LoadSound("Resources/drink.wav", out drink);
答案 3 :(得分:0)
我遇到了几乎相同的问题。最后我意外地得到了如下:
StreamResourceInfo streaminfo = Application.GetResourceStream(new Uri("project_name;component/folder_name/Sound3.wav", UriKind.RelativeOrAbsolute));
SoundEffect effect1 = SoundEffect.FromStream(streaminfo.Stream);
effect1.Play();
和: 必须将.wav文件的“Build Action”属性设置为“Resource”,并将Porperty“Copy to Output Directory”设置为“Copy if newer”
顺便说一下,我正在使用Silverlight5。