Evgenie Tivaniuk

Evgenie Tivaniuk

  • NA
  • 82
  • 11.8k

MediaElements don't work as expected

May 29 2018 3:17 AM
There is something weird going on, with MediaElements in my game. I'll do my best to explain, but since the code is quite long, and I don't know where the problem can be, I am not sure I can provide the right sections of it. Please tell me, if you need any kind of update.
 
Now, to the problem.
 
First, the background.
 
I am making a video game. For each sound, voiceover, music and video, there is a separate MediaElement. Since this causes for a large amount of RAM to be consumed, I set all file sources to "null", then assign the original source string to relevant files when they are loaded. So far, it worked flawlessly. Some time ago, I started doing major changes to the code, since I learnt a way ways to optimize it. Because of it, I can't be sure when I made the change that caused the current problem, and can't go back to track it.
 
What problem is exactly
 
When the main window loads (relevant event), two MediaElements are loaded, the Intro and one of music tracks. This part works as should. However, after the game starts, no other sound files can be played, and neither the original music track can be shut down!
  1. private void wdwMain_Loaded(object sender, RoutedEventArgs e)
  2. {
  3.     // This timer releases the unuased memory once in a while
  4.     tmrDispose.Tick += new EventHandler(tmrRepeatDispose);
  5.     tmrDispose.Interval = new TimeSpan(0, 0, 0, 0, 10000);
  6.     UnloadCameFrom();
  7.     
  8.     // All media elements sources are saved as lists for future use
  9.     DefineAllMedia();
  10.     // All images sources are saved as lists for future use, the
  11.     //original xaml elements set to null
  12.     DefinePathImages();
  13.     DefineImages();
  14.     HideAllImages();
  15.     
  16.     // All voiceover sources are saved as lists for future use, then original values set to null
  17.     DefinePathVoices();
  18.     HideAllVoices();
  19.     
  20.     // All sounds sources are saved as lists for future use, then original values set to null
  21.     DefinePathSounds();
  22.     HideAllSounds();
  23.     
  24.     // All music sources are saved as lists for future use, then original values set to null
  25.     DefinePathMusic();
  26.     HideAllMusic();
  27.     DefineAllVideos();
  28.     DefinePathVideos();
  29.     HideAllVideos();
  30.     Dispose();
  31.     
  32.     // Movies
  33.     SteamUse.Instance.Init();
  34.     HideEverything();
  35.     LoadOptions();
  36.     // Sets volume for sound elements
  37.     AssignMusicVolume();
  38.     // Here, the Intro is loaded, and first music track starts
  39.     areaMain.Visibility = Visibility.Hidden;
  40.     areaVideo.Visibility = Visibility.Visible;
  41.     areaVideo.IsEnabled = true;
  42.     areaVideo.Margin = new Thickness(0, 0, 0, 0);
  43.     LoadVideo(movIntro);
  44.     AssignPathMusic(musTheme3);
  45. }
SO, as I say, the original code works. But then, when I am trying to play any other sound file, it doesn't work.
 
Like here:
  1. musTheme3.Stop();
  2. musTheme3.Close();
  3. AssignPathMusic(musTheme1);
This should make music track 3 stop, and music track 1 to start working. But neither of it happens. Also, when I try to interact with game objects, a sound or voiceover should be played.
 
  1. lblTextOutput.Content = "I woke up in this stasis pod."; AssignPathVoice(vcoInteract_Hibernation_Room_I_woke_up_in_this_stasis_pod;
This how the sound files are activated:
  1. public void AssignPathVoice(MediaElement vcoThis)
  2. {
  3.     if (voice_path.ContainsKey(vcoThis.Name.ToString()))
  4.     {
  5.         string path = voice_path[vcoThis.Name.ToString()];
  6.         vcoThis.Source = new Uri(path, UriKind.Relative);
  7.         vcoThis.Play();
  8.     }
  9. }
When sound file starts (MediaOpened event):
  1. private void AnySoundStart(object sender, RoutedEventArgs e)
  2. {
  3.     vcoCurrent = sender as MediaElement;
  4. }
When sound file play ends (MediaEnded event):
  1. private void AnySoundEnd(object sender, RoutedEventArgs e)
  2. {
  3.     if ((vcoCurrent != null) && (vcoCurrent != musTheme1) && (vcoCurrent != musTheme2) && (vcoCurrent != musTheme3)
  4.         && (vcoCurrent != musTheme_lower_deck_light) && (vcoCurrent != musTheme_lower_deck_dark))
  5.     {
  6.         vcoCurrent.Stop();
  7.         vcoCurrent.Source = null;
  8.         vcoCurrent.Close();
  9.         Dispose();
  10.         GC.Collect();
  11.     }
  12.     lblTextOutput.Content = "";
  13. }
What I checked so far, for possible errors:
  1. The source string is assigned correctly, it isn't null and the path to file is correct
  2. The volume of sound files is not zero
  3. The media elements aren't set to "mute", or hidden, or enabled=false.
  4. If I try to run other media files at the window load event, they play correctly, but cause same results - don't stop and other sounds can't be played.
  5. If I load no sound at start, still other sounds can't be played during the game.
  6. I can't stop music file from the window load event by any means, even by trying to set it's source to null.
As I said, it worked with earlier version of game. If it matters, I can explain the changes I did with words. Earlier, I didn't know how to make a loop, the goes through all xaml elements of specific class, and make necessary changes, so had to write each required element name manually. After I learned to do it, I replaced a huge amount of code lines with a few "foreach" loops. As I remember, that didn't have any connection to media elements.
 
What else can be the problem? I am out of ideas, maybe you can suggest something?
 
Thank you in advance, Evgenie