Created
July 27, 2021 11:00
-
-
Save IT-Delinquent/ba7af72868b78b3d466eaf75bd1d2096 to your computer and use it in GitHub Desktop.
repeatBackgroundMusic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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