'System.InvalidOperationException' using x

2019-09-02 01:29发布

问题:

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

    }
}

回答1:

If your creating a XNA WP7 application you should be loading your sounds from the ContentManager:

  1. Make sure your sound effect files are set to
    • Build Acction: Compile
    • Content Importer: WAV Audio File - XNA Framework
    • Content Processor Sound Effect - XNA Framework
  2. Load your sound from the ContentManager

Code:

drink = ContentManager.Load<SoundEffect>("Resources/drink.wav");


回答2:

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.