'System.InvalidOperationException'使用xnaFramework在Windows Phone 7应用程序中播放声音效果? [英] 'System.InvalidOperationException' using xnaFramework to play a sound effect in windows phone 7 app?

查看:348
本文介绍了'System.InvalidOperationException'使用xnaFramework在Windows Phone 7应用程序中播放声音效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是奇怪的,在代码我有一个声音效果(我总共有24个音效)如果我注释掉(23效果),除了loadsound(资源/ drink.wav,出饮料)和注释相应的if在private void button_Click方法中,它工作正常?取消注释所有的声音效果,然后'System.InvalidOperationException'?我有24个音效每个不超过6秒长。任何想法任何人...对于可以使用的声音文件的数量有限制,或者我可以清除流或某物?

This is kindof strange, in the code i have one sound effect(i have 24 sound effects in total)if i comment out (23 effects) except loadsound("resources/drink.wav",out drink) and comment out the corresponding "if"'s in "private void button_Click" method, it works fine?? uncomment all sound effects and then 'System.InvalidOperationException'? i have 24 sound effects each no more than 6 sec long. any ideas anyone...is there a limit on the amount of sound files that can be used or maybe i have to clear the stream or something?

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

    }
}


推荐答案

尝试更改文件属性中内容处理器属性下的压缩质量声音文件。在我的情况下,最佳压缩是一个问题。

Try to change the "Compression Quality" property under the "Content Processor" property in "File Properties" of the sound file. In my case the "Best" compression was a problem.

这篇关于'System.InvalidOperationException'使用xnaFramework在Windows Phone 7应用程序中播放声音效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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