0

Here is the setup I am rolling at the moment. Given this, how can I tell when the _BGMusic SoundEfect is over? I am running a game loop that cycles a few times a second and I ultimately want to loop this song.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

    SoundEffect _BGMUSIC;

    public Page1() {
     ...

     LoadSound("sfx/piano.wav", out _BGMUSIC);
     ...
    }

    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);
            FrameworkDispatcher.Update();
        } catch (NullReferenceException) {
            // Display an error message
            MessageBox.Show("Couldn't load sound " + SoundFilePath);
        }
    }
APerson
  • 8,140
  • 8
  • 35
  • 49
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111

2 Answers2

0

From this code it looks like you have just stored the sound in Sound but you don't show where you implement the Sound.Play() method. While there isn't a Sound.IsPlaying boolean which would probably solve your problem, there is a Sound.Duration property which you may use to solve your problem, especially if you couple that with a Timer (which can be used in tandem to set off a flag to show whether or not the sound is playing.

Also something I have never used but just found is the Sound.IsDisposed property. I would definitely look into this as well because it may be exactly for what you are looking.

tmwoods
  • 2,353
  • 7
  • 28
  • 55
  • Sound effects are not disposed upon completion. If they were you would have to reload them after every call of Soundeffect.Play(). Also there is already a game timer running so I don't think using a second would be very efficient. Thanks for your response. I am aware of the Duration property but I don't know how to use it which is basically what this question is all about. Though it's not specifically stated. – DotNetRussell Mar 02 '13 at 18:38
  • Is that original timer in use during the sound playing time? Because you could either 'borrow' it or else just note the value of it and compare it with the duration value. – tmwoods Mar 02 '13 at 18:40
  • Yes and no. The game time is running however the music starts after the timer is started. – DotNetRussell Mar 02 '13 at 18:41
  • So can you use the value of the timer during the song playing? Is there a chance that the timer will be stopped at all? – tmwoods Mar 02 '13 at 18:41
  • Yes for efficiency there are points in which the game timer stops because there is no need for it. (It is a silverlight game) – DotNetRussell Mar 02 '13 at 18:43
  • Then I think you might be between a rock and a hard place here. I've been searching for something that sets off a flag when a SoundEffect finishes but I've not found anything. It sounds like efficiency is a top priority but I can't immediately think of something other than a timer. I will continue to try to think of something but maybe for now I would try to implement something like it. – tmwoods Mar 02 '13 at 18:45
  • Okay I appreciate the effort, Thanks – DotNetRussell Mar 02 '13 at 18:54
0

Alright so the answer to this question is actually very simple and outlined in Microsoft's MSDN page very nicely (for once).

To implement it with what I have up above currently you need to do the following.

Create a global SoundEffectInstance

 SoundEffectInstance _BGMUSICINSTANCE;

In my example I initialize the SoundEffect in my main method so underneath it I want to initialize the SoundEffectInstance.

_BGMUSICINSTANCE = _BGMUSIC.CreateInstance();

Now that the instance is loaded and ready we can set its loop property to true. You can do this also in the main method or wherever you want. It's a global

 _BGMUSICINSTANCE.IsLooped = true;

Finally play the song

 _BGMUSICINSTANCE.Play();

Reference page http://msdn.microsoft.com/en-us/library/dd940203(v=xnagamestudio.31).aspx

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111