Audio Playback

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Sound Effects

You will need to declare a SoundEffect:

SoundEffect effect;

In LoadContent you can then load in your sound effect:

effect = Content.Load<SoundEffect>("MySoundEffect");

You can then use the play method to play it when you want:

effect.Play();

Or you can set these and then play the sound effect:

float volume = 1.0f;
float pitch = 0.0f;
float pan = 0.0f;
effect.Play(volume, pitch, pan);

To set the master volume of all sound effects you can do:

SoundEffect.MasterVolume-=0.05f;
SoundEffect.MasterVolume+=0.05f;

If you have set the volume of each SoundEffect this will be multiplied by the master volume.

Background Music

You will need to declare a Song:

Song song;

You will then need to load the song in LoadContent:

song = Content.Load<Song>("song_name");

Finally when you want to play the music you can:

MediaPlayer.Play(song);

You can also do:

MediaPlayer.Pause();
MediaPlayer.Resume();
MediaPlayer.Stop();

If you want the song to repeat forever then you can do:

MediaPlayer.IsRepeating = true;

Controlling the volume of the music is easy:

MediaPlayer.Volume+=0.05f;
MediaPlayer.Volume+=0.05f;