Skip to content

Instantly share code, notes, and snippets.

@IT-Delinquent
Created July 27, 2021 11:00
Show Gist options
  • Select an option

  • Save IT-Delinquent/ba7af72868b78b3d466eaf75bd1d2096 to your computer and use it in GitHub Desktop.

Select an option

Save IT-Delinquent/ba7af72868b78b3d466eaf75bd1d2096 to your computer and use it in GitHub Desktop.
repeatBackgroundMusic
//Create a new MediaPlayer object
private static readonly MediaPlayer _backgroundMusic = new MediaPlayer();
public static void StartBackgroundMusic(){
//Open the temp WAV file saved in the temp location and called "backgroundmusic.wav"
_backgroundMusic.Open(new Uri(Path.Combine(Path.GetTempPath(), "backgroundmusic.wav")));
//Add an event handler for when the media has ended, this way
//the music can be played on a loop
_backgroundMusic.MediaEnded += new EventHandler(BackgroundMusic_Ended);
//Start the music playing
_backgroundMusic.Play();
}
private static void BackgroundMusic_Ended(object sender, EventArgs e){
//Set the music back to the beginning
_backgroundMusic.Position = TimeSpan.Zro;
//Play the music
_backgroundMusic.Play();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment