Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The SoundEffect.Play method allows you to specify the pitch and volume of a sound to play. However, after you call Play, you cannot modify the sound. Using SoundEffectInstance for a given SoundEffect allows you to change the pitch and volume of a sound at any time during playback.
Complete Sample
The code in this topic shows you the technique for changing a sound's pitch or volume. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
Download ChangePitchAndVolumeWithoutXACT_Sample.zip
Change Pitch and Volume of Sound
To adjust the pitch and volume of a sound
Declare SoundEffect and Stream by using the method shown in Playing a Sound. In addition to the method described in Playing a Sound, declare SoundEffectInstance.
SoundEffectInstance soundInstance;
In the Game.LoadContent method, set the SoundEffectInstance object to the return value of SoundEffect.CreateInstance.
soundfile = TitleContainer.OpenStream(@"Content\tx0_fire1.wav"); soundEffect = SoundEffect.FromStream(soundfile); soundInstance = soundEffect.CreateInstance();
Adjust the sound to the desired level using the SoundEffectInstance.Pitch and SoundEffectInstance.Volume properties.
// Play Sound soundInstance.Play();
Play the sound using SoundEffectInstance.Play.
// Pitch takes values from -1 to 1 soundInstance.Pitch = pitch; // Volume only takes values from 0 to 1 soundInstance.Volume = volume;
Concepts
- Playing a Sound
Demonstrates how to play a simple sound by using SoundEffect. - Looping a Sound
Demonstrates how to loop a sound. - Creating and Playing Sounds
Provides overviews about audio technology, and presents predefined scenarios to demonstrate how to use audio.
Reference
- SoundEffect Class
Provides a loaded sound resource. - SoundEffectInstance Class
Provides a single playing, paused, or stopped instance of a SoundEffect sound.