Skip to content

Commit 1d01897

Browse files
authored
Fix unexpected crash from background music (#675)
1 parent 8c1026c commit 1d01897

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

DXMainClient/DXGUI/Generic/MainMenu.cs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -929,20 +929,20 @@ private void PlayMusic()
929929
if (!isMediaPlayerAvailable)
930930
return; // SharpDX fails at music playback on Vista
931931

932-
if (themeSong != null && UserINISettings.Instance.PlayMainMenuMusic)
932+
try
933933
{
934-
isMusicFading = false;
935-
MediaPlayer.IsRepeating = true;
936-
MediaPlayer.Volume = (float)UserINISettings.Instance.ClientVolume;
937-
938-
try
934+
if (themeSong != null && UserINISettings.Instance.PlayMainMenuMusic)
939935
{
936+
isMusicFading = false;
937+
MediaPlayer.IsRepeating = true;
938+
MediaPlayer.Volume = (float)UserINISettings.Instance.ClientVolume;
939+
940940
MediaPlayer.Play(themeSong);
941941
}
942-
catch (InvalidOperationException ex)
943-
{
944-
Logger.Log("Playing main menu music failed! " + ex.ToString());
945-
}
942+
}
943+
catch (Exception ex)
944+
{
945+
Logger.Log("Playing main menu music failed! " + ex.ToString());
946946
}
947947
}
948948

@@ -956,15 +956,22 @@ private void FadeMusic(GameTime gameTime)
956956
if (!isMediaPlayerAvailable || !isMusicFading || themeSong == null)
957957
return;
958958

959-
// Fade during 1 second
960-
float step = SoundPlayer.Volume * (float)gameTime.ElapsedGameTime.TotalSeconds;
959+
try
960+
{
961+
// Fade during 1 second
962+
float step = SoundPlayer.Volume * (float)gameTime.ElapsedGameTime.TotalSeconds;
961963

962-
if (MediaPlayer.Volume > step)
963-
MediaPlayer.Volume -= step;
964-
else
964+
if (MediaPlayer.Volume > step)
965+
MediaPlayer.Volume -= step;
966+
else
967+
{
968+
MediaPlayer.Stop();
969+
isMusicFading = false;
970+
}
971+
}
972+
catch (Exception ex)
965973
{
966-
MediaPlayer.Stop();
967-
isMusicFading = false;
974+
Logger.Log("Fading music failed! Message: " + ex.ToString());
968975
}
969976
}
970977

@@ -979,17 +986,24 @@ private void FadeMusicExit()
979986
return;
980987
}
981988

982-
float step = MEDIA_PLAYER_VOLUME_EXIT_FADE_STEP * (float)UserINISettings.Instance.ClientVolume;
983-
984-
if (MediaPlayer.Volume > step)
989+
try
985990
{
986-
MediaPlayer.Volume -= step;
987-
AddCallback(new Action(FadeMusicExit), null);
991+
float step = MEDIA_PLAYER_VOLUME_EXIT_FADE_STEP * (float)UserINISettings.Instance.ClientVolume;
992+
993+
if (MediaPlayer.Volume > step)
994+
{
995+
MediaPlayer.Volume -= step;
996+
AddCallback(new Action(FadeMusicExit), null);
997+
}
998+
else
999+
{
1000+
MediaPlayer.Stop();
1001+
ExitClient();
1002+
}
9881003
}
989-
else
1004+
catch (Exception ex)
9901005
{
991-
MediaPlayer.Stop();
992-
ExitClient();
1006+
Logger.Log("Fading music on exit failed! Message: " + ex.ToString());
9931007
}
9941008
}
9951009

0 commit comments

Comments
 (0)