TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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!
private
void
wdwMain_Loaded(
object
sender, RoutedEventArgs e)
{
// This timer releases the unuased memory once in a while
tmrDispose.Tick +=
new
EventHandler(tmrRepeatDispose);
tmrDispose.Interval =
new
TimeSpan(0, 0, 0, 0, 10000);
UnloadCameFrom();
// All media elements sources are saved as lists for future use
DefineAllMedia();
// All images sources are saved as lists for future use, the
//original xaml elements set to null
DefinePathImages();
DefineImages();
HideAllImages();
// All voiceover sources are saved as lists for future use, then original values set to null
DefinePathVoices();
HideAllVoices();
// All sounds sources are saved as lists for future use, then original values set to null
DefinePathSounds();
HideAllSounds();
// All music sources are saved as lists for future use, then original values set to null
DefinePathMusic();
HideAllMusic();
DefineAllVideos();
DefinePathVideos();
HideAllVideos();
Dispose();
// Movies
SteamUse.Instance.Init();
HideEverything();
LoadOptions();
// Sets volume for sound elements
AssignMusicVolume();
// Here, the Intro is loaded, and first music track starts
areaMain.Visibility = Visibility.Hidden;
areaVideo.Visibility = Visibility.Visible;
areaVideo.IsEnabled =
true
;
areaVideo.Margin =
new
Thickness(0, 0, 0, 0);
LoadVideo(movIntro);
AssignPathMusic(musTheme3);
}
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:
musTheme3.Stop();
musTheme3.Close();
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.
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:
public
void
AssignPathVoice(MediaElement vcoThis)
{
if
(voice_path.ContainsKey(vcoThis.Name.ToString()))
{
string
path = voice_path[vcoThis.Name.ToString()];
vcoThis.Source =
new
Uri(path, UriKind.Relative);
vcoThis.Play();
}
}
When sound file starts (MediaOpened event):
private
void
AnySoundStart(
object
sender, RoutedEventArgs e)
{
vcoCurrent = sender
as
MediaElement;
}
When sound file play ends (MediaEnded event):
private
void
AnySoundEnd(
object
sender, RoutedEventArgs e)
{
if
((vcoCurrent !=
null
) && (vcoCurrent != musTheme1) && (vcoCurrent != musTheme2) && (vcoCurrent != musTheme3)
&& (vcoCurrent != musTheme_lower_deck_light) && (vcoCurrent != musTheme_lower_deck_dark))
{
vcoCurrent.Stop();
vcoCurrent.Source =
null
;
vcoCurrent.Close();
Dispose();
GC.Collect();
}
lblTextOutput.Content =
""
;
}
What I checked so far, for possible errors:
The source string is assigned correctly, it isn't null and the path to file is correct
The volume of sound files is not zero
The media elements aren't set to "mute", or hidden, or enabled=false.
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.
If I load no sound at start, still other sounds can't be played during the game.
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
Reply
Answers (
0
)
how can I make search textbox with database in wpf
Increase FontSize and FontWeight of DataGridColumnHeader