Skip to content

Instantly share code, notes, and snippets.

@robc
Created October 18, 2008 01:05
Show Gist options
  • Save robc/17572 to your computer and use it in GitHub Desktop.
Save robc/17572 to your computer and use it in GitHub Desktop.
Implementation of a Background Music service using XNA Game Studio 3.0
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
namespace XELibrary
{
public class DefaultBackgroundMusicPlaybackService: IBackgroundMusicPlaybackService
{
public DefaultBackgroundMusicPlaybackService(ContentManager contentManager)
{
Content = contentManager;
}
private ContentManager Content { get; set; }
private Song CurrentSong { get; set; }
#region IBackgroundMusicPlaybackService Members
public void StartBackgroundMusic(string assetName)
{
if (CurrentSong != null)
{
StopBackgroundMusic();
CurrentSong.Dispose();
CurrentSong = null;
}
// As we're doing more inline game transitions, maybe we should
// be doing this in a background thread?
CurrentSong = Content.Load<Song>(assetName);
MediaPlayer.Play(CurrentSong);
}
public void PauseBackgroundMusic()
{
if (CurrentSong == null) return;
if (MediaPlayer.State != MediaState.Playing) return;
MediaPlayer.Pause();
}
public void StopBackgroundMusic()
{
if (CurrentSong == null) return;
if (MediaPlayer.State == MediaState.Stopped) return;
MediaPlayer.Stop();
}
public void ResumeBackgroundMusic()
{
if (CurrentSong == null) return;
if (MediaPlayer.State == MediaState.Stopped) return;
MediaPlayer.Stop();
}
public float Volume
{
get { return MediaPlayer.Volume; }
set { MediaPlayer.Volume = value; }
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment