Audio Playback

From TRCCompSci - AQA Computer Science
Revision as of 10:32, 26 May 2024 by Admin (talk | contribs)
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);

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;