Quote:
i have found that a marker is placed at the time it stops and it indicates the start of a managed heap garbage collection (GC)
That does kinda say exactly what the problem is!
Your player is going out of scope at the end of teh method, so there are no references in your app to it any more:
private void PlaySound()
{
Uri uri = new Uri(@"C:\User Program Files\ConsoleApp1\visual studio\Mini test game 1\Mini test game 1\resources\Sad Background Music 1.wav");
var player = new MediaPlayer();
player.Open(uri);
player.Play();
}
As a result, when the GC kicks in it finds an unreferenced object and deletes it. That stops the player playing!
Move your player outside the method and it'l retain the reference and prevent teh GC removing it:
private MediaPlayer player = new MediaPlayer();
private void PlaySound()
{
Uri uri = new Uri(@"C:\User Program Files\ConsoleApp1\visual studio\Mini test game 1\Mini test game 1\resources\Sad Background Music 1.wav");
player.Open(uri);
player.Play();
}
But do yourself a favour: don't hard code your URL: use a settings file, or a relative-to-the-app-executable folder instead. That way, your app doesn't go tits-up when you give a copy to your mate.