Is it acceptable to use SoundEffectInstance for background music? - c#

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.

Related

XNA: make a sound instance play just once upon activation

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.

Synchronize playback of audio to a timer using NAudio

I want to synchronize the playback of a song to a timer so that I can keep the beats of a song in sync with things rendered on the screen. Any way of accomplishing this using NAudio?
Several out the output devices in NAudio support the IWavePosition interface, which gives a more accurate indication of where the soundcard is currently up to in the buffer it is playing. Usually this is reported in terms of number of bytes that have been played since playback started - so it does not necessarily correspond to the position within the file you are playing or within a song. So if you use this you will need to keep track of when you started playing.
Usually you would keep the things rendered on screen synchronized to the audio playback position, rather than the other way round.

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.

Monotouch C# play many sounds simultaneously with volume control

I'm making a War Game for iOS using Monotouch and C#. I'm running into some problem with the audio sound effects.
Here's what I require: The ability to play many sound effects simultaneously (possibly up to 10-20 at once) and the ability to adjust volume (for example, if the user zooms in on the battlefield the gun shot volume gets louder).
Here are my problems:
With AVAudioPlayer, I can adjust volume but I can only play 1 sound per thread. So if I want to play multiple sounds I have to have dozens and dozens of threads going just incase they overlap... This is a war game, picture 20 soldiers on the battlefield. Each soldier would have a "sound thread" to play gun fire sounds when they shoot because It is possible that every soldier could just happen to fire at the same exact time. I don't have a problem with making lots of threads, but my game already has dozens of threads running all the time and adding dozens more could get me into trouble... right? So I'd rather not go this road of adding dozens of more threads unless i have too...
With SystemSound, I can play as many sounds as I want in the same thread, but I can't adjust the volume.... So my work around here is, for every sound effect i have - save it like 4 times at 4 different volumes. That is a big pain... Any way to adjust volume with SystemSounds??
Both of these answer some of my requirements, but neither seems to be a seamless fit. Should I just go the AVAudioPlayer multi-threading nightmare road? Or the SystemSound multi-file-with-different-volume-levels nightmare road? Or is there a better way to do this?
Thanks in advance.
Finally found the solution to my problem. AVAudioPlayer IS capable of playiing multiple sounds at once but only with certain file formats... The details are available in this link. The reason why I couldn't play my sound effects simultaneously was because the file format was compressed and the iphone only has 1 hardware decompressor.
http://brainwashinc.wordpress.com/2009/08/14/iphone-playing-2-sounds-at-once/

Xact vs AudioContent

I am using XNA to develop a game which requires both sound effects and music. I'm trying to figure out how to implement the sound engine. Microsoft provides the ability to use the Content Pipeline to load and play audio. However, I also seen people use Xact to do the same thing. My question is, whats the difference and what would be the better approach to making a sound engine?
Xact is feature rich but complex to use. It was originally the only way to play sound but people wanted something simpler so Microsoft added the content pipeline method.
Use the Content Pipeline if you want:
To play a sound (2d or 3d)
To not have to invest a lot of time
learning an audio framework
Use Xact if you want:
Categories of sounds that can be
interdependently controlled, like
mute game sounds but not menu sounds
Real time advanced control over sound
pitch, volume. For things like
turrets spinning up, cars
accelerating etc.
To have multiple varieties of a
single sound effect like a seven
different pain sounds and have Xact
choose which one to play.
To have a sound play with slightly
different pitch, volume, filter or 3d
pan every time it is played. This is
really good for bullets and
repetitive things like that. There
is nothing that says fake computer
simulation like a repeating sound
with no variance.
To allow a game designer or sound
designer full control to edit and
change sounds without touching the
code.
To have sound banks (collections of
sound) that you can load or unload as
a group, which can use different
compression settings and can be in
memory or streaming.
To mix the volume, pitch and priority
of sounds in an editor.
To apply filtering to a sound.
To control whether the sound is
looping or not.
To use DSP Effects
One of my favourite things about Xact is editing and previewing of sound functions in editor. For example a volume fade on a turret overheat sound. With XACT you can sit down with the sound designer, even if he's not a technical guy and edit the control curves until he's happy with the sound. Once you've set it up it's really easy to change later on. In this example image here a turret is overheated. At the end of the overheat cycle the hissing steam noise volume is reduced, but because it's a curve I have a lot of control over how the sound fades out. I've used this with a beam weapon as well, dropping the frequency according to a curve as it runs out of ammo.

Categories

Resources