XNA: make a sound instance play just once upon activation - c#

I am making a game in XNA and when the player levels up I have a sound playing. The sound plays over and over, it isn't set to loop and setting .islooped to false doesn't stop it.
Is there any simple command to make it just play once?

Be sure to use the SoundEffect class given in the Microsoft.Xna.Framework.Audio library.
The sound effect content is loaded just like any other content in xna using Content.Load method. The SoundEffect class contains a method Play(), which will play your sound only once. If you are using SoundEffects then be sure to check if there are any unwanted loops happening when playing that sound.

Related

In unity, is it possible to have a small animation that doesn't freeze even when a large scene is loading?

In unity, is it possible to have a small animation that doesn't freeze even when a large scene is loading? Maybe multi threading? I have seen games that doesn't freeze at all even when they are loading their game, like noita
Yes. Use SceneManager.LoadSceneAsync and play the animation. For more information about LoadAsync check out the link below.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

C# WPF Invoking Windows Media Player

I'm currently passing my time by creating a piano app. Each Key is represented of a simple button with a command which fires at click. This leads to executing this Method in ViewModel:
private void PlaySound(object parameter)
{
var mediaPlayer = new MediaPlayer();
mediaPlayer.Open(new System.Uri(#SoundBar.GetSoundPathByIdent(int.Parse(parameter.ToString()))));
mediaPlayer.Play();
{
I think the problem is that the MediaPlayer leaves a WeakReference which prevents the GarbageCollector from collecting it. Leading to overloading your RAM after playing a while.
The solution i found was to call: mediaPlayer.Close();
But this should only happen after the sound has finished playing, otherwise it will be cut.
Is there a way to check if the played sound has finished playing?
I have already spent some time doing research and testing but i couldn't come up with a working solution.
The Position and NaturalDuration properties give you details about where in the stream you're at (ie. Position / NaturalDuration gives you a value between 0.0 and 1.0 that represents the playback position as a percentage)
But you may want to build an "orchestrator" for your media playback. Assuming that you don't want to play all sounds at the same time, an orchestrator could be responsible for managing the lifetime of your media players, and determining where in the playback they are.
In your application you could create a single instance of the orchestrator on start up. The orchestrator could create and manage a pool of media players it could reuse when it needs to play a chord. Then your piano app could support a certain number of chords at the same time and have a single poller that determines which media players are free and which are "busy" playing audio.

Is it acceptable to use SoundEffectInstance for background music?

The reason I want to do this is to be able to layer the background music. (e.g, simple song starts playing, player triggers something, adds an instrument). I can work out the timing issues, if any.
I thought I could do that with MediaPlayer/Song, but it wouldn't work.
All I'm really looking for is the downsides to use SoundEffectInstance.
p.s, I don't use XACT, since I'll be changing over to MonoGame eventually.
Thanks
Actually, that's what the SoundEffectInstance is for!
It has limitations though, depending on the platform your game is running:
On Windows Phone, a game can have a maximum of 16 total playing
SoundEffectInstance instances at one time, combined across all loaded
SoundEffect objects. The only limit to the total number of loaded
SoundEffectInstance and SoundEffect objects is available memory.
However, the user can play only 16 sound effects at one time. Attempts
to play a SoundEffectInstance beyond this limit will fail. On Windows,
there is no hard limit. Playing too many instances can lead to
performance degradation. On Xbox 360, the limit is 300 sound effect
instances loaded or playing. Dispose of old instances if you need
more.
Oh and by the way, it's been a long time since I played with XNA but I'm pretty sure that the XACT tool was no longer necessary by the end of it's life cycle.
I seem to recall that you could load an mp3 on the Content folder and play it via the SoundEffectInstance object.
Actually, I think you'll find using the MediaPlayer class combined with the Song class is the recommended way to play background music.
Provides methods and properties to play, pause, resume, and stop songs. MediaPlayer also exposes shuffle, repeat, volume, play position, and visualization capabilities.
I think the primary difference is that the MediaPlayer can stream the data into memory rather than loading it all in at once. So, for long playing music tracks this is the way to go.
Also, in MonoGame these classes are implemented by wrapping around the platform specific classes that do the same thing. For example, on Android the SoundEffectInstance uses the Android SoundPool (intended for sound effects) and the MediaPlayer uses the Android MediaPlayer (intended for music). See this post on the MonoGame forums for reference.
slygamer says: MediaPlayer for background music and SoundEffect for sound effects is how it is designed to be used.

Delay starting XNA game switching to Fullscreen messing up intro screen

I'm making a Windows game using XNA 4.0. I have an quick little intro screen that shows our studio logo and plays a sound. It lasts 1.5 seconds and looks and works as desired in windowed mode.
We want to run the game full screen. So all I added was "graphics.IsFullScreen = true" to the Game subclass constructor after the GraphicsDeviceManager is instantiated and we've set the preferred backbuffer dimensions. When the game starts the video card just glitches my monitors for like 1 or 2 seconds switching resolutions, etc. - and that is all a customary and understandable delay between the video card, the device drivers and my monitors all figuring out this change, but XNA is running the game loop while all this nonsense is going on.
This means my intro starts, runs and is over by the time the system gets around to actually displaying what I'm drawing and by then the intro is over. What I'd really like is a way to detect when the video card is actually rendering before I start drawing and playing sound and timing things assuming the player can see them. Searching around online, I've seen reference to a "graphics.EnsureDevice()" call that seems to have been deprecated and is no longer available in XNA 4.0.
I guess this is kind of dirty, but you could fire off a thread that continually checks to see if the DeviceParameter IsFullscreen is set to true on active graphics device, after you set the GraphicsDeviceManager.IsFullscreen property to true.
Once it is set, then you would start your game loop.
You could also write it in a way that the thread would only fire off if you set GraphicsDeviceManager.IsFullscreen to true. That way it would support both modes.

How do I control background music from app?

I have a c#/xaml app. I'd like to play instruction sounds, but if the user is playing background music, I'd like to mute it, or lower the volume temporarily while the sounds are being played.
Currently, I'm using xaudio2 to play my audio in C++, and using some delegates to make calls into C# where I'm using the MediaPlayer class from the xna framework to mute the background media.
However, after pausing the music, when I go to play my instruction through xaudio2, I get System.InvalidOperationException exception from the xna framework. If I comment out the line for playing the sound, there is no exception.
I suppose it's worth noting that this is not happening on the UI thread...
Am I going about this the right way? I can't find any links to information on how to accomplish this - I'm grasping at straws.
It turns out I wasn't calling FrameworkDispatcher.Update() before I called MediaPlayer.Pause(). But this is possible to do if your app requires it.

Categories

Resources