Windows phone 7 ';系统无效操作异常';使用xnaFramework在windows phone 7应用程序中播放音效?

Windows phone 7 ';系统无效操作异常';使用xnaFramework在windows phone 7应用程序中播放音效?,windows-phone-7,c#-4.0,xna,Windows Phone 7,C# 4.0,Xna,这有点奇怪,在代码中我有一个音效(我总共有24个音效),如果我注释掉(23个音效),除了loadsound(“resources/drink.wav”,out drink),并注释掉“private void button\u Click”方法中相应的“if”,效果很好??取消注释所有音效,然后“System.InvalidOperationException”?我有24个音效,每个都不超过6秒。任何人有什么想法…可以使用的声音文件数量有限制吗,或者我必须清除流或其他什么 using Syste

这有点奇怪,在代码中我有一个音效(我总共有24个音效),如果我注释掉(23个音效),除了loadsound(“resources/drink.wav”,out drink),并注释掉“private void button\u Click”方法中相应的“if”,效果很好??取消注释所有音效,然后“System.InvalidOperationException”?我有24个音效,每个都不超过6秒。任何人有什么想法…可以使用的声音文件数量有限制吗,或者我必须清除流或其他什么

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)//there are 23 more here

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

            // Create and load SoundEffect objects.
            LoadSound("Resources/drink.wav", out drink);//there are 23 more here

        }

        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

    }
}

如果您正在创建XNA WP7应用程序,则应该从ContentManager加载声音:

  • 确保您的音效文件设置为
    • 构建帐户:编译
    • 内容导入器:WAV音频文件-XNA框架
    • 内容处理器音效-XNA框架
  • 从ContentManager加载声音
  • 代码:

    drink=ContentManager.Load(“Resources/drink.wav”);
    
    尝试更改声音文件“文件属性”中“内容处理器”属性下的“压缩质量”属性。在我的例子中,“最佳”压缩是一个问题。

    hi,我已将构建操作设置为“内容”,如果更新,则复制。我在微软网站上找到了应用程序msdn.microsoft.com/en us/library/ff431744(v=vs.92)的代码。aspx他们有一个示例应用程序,可以通过这种方式下载,链接是go.microsoft.com/fwlink/?LinkID=229130,但你说的是XNA。我告诉过你如何在XNA中实现……你提到的样本不是在XNA中实现的。我刚才提到的是实现它的方法。你试过了吗?如果我没记错的话,这实际上是一个Zune应用程序。ContentManager是否可用于此目的?上一次我告诉他使用ContentManager和内容管道,他似乎运气不太好。嗨,我看了一下ContentManager的msdn资料,我对c#和wp7以及zune都是新手,所以如果没有示例代码可供参考,那就没什么意义了。我尝试了这一行-drink=ContentManager.Load(“Resources/drink.wav”);使用build-action:compile直接出现了一个错误,我不知道如何修复它。。问题是,我不确定从现有代码中提取什么,以及使用content manager应该实现什么。
    drink = ContentManager.Load<SoundEffect>("Resources/drink.wav");