I have this app with C# Code but the MP3 just not playing.
music = new MediaElement();
music.AutoPlay = false;
music.Source = new Uri("/music/musicNormal.mp3", UriKind.Absolute);
music.CurrentStateChanged += new RoutedEventHandler(music_CurrentStateChanged);
music.MediaOpened += new RoutedEventHandler(music_MediaOpened);
ForcurrentStateChanged event I give a MessageBox, but it never triggered.
For MediaOpened event I give a line music.Play(), but it also never triggered
I have these functions
private void changeMusic(bool normal)
{
music.Stop();
if (normal)
music.Source = new Uri("/music/musicNormal.mp3", UriKind.Absolute);
else
music.Source = new Uri("/music/musicFast.mp3", UriKind.Absolute);
}
private void playMusic()
{
if (timeBar.Value <= 10 && music1)
{
timeNumber.Foreground = new SolidColorBrush(Colors.Red);
changeMusic(false);
music2 = true;
music1 = false;
}
else if (timeBar.Value > 10 && music2)
{
timeNumber.Foreground = new SolidColorBrush(Colors.White);
changeMusic(true);
music1 = true;
music2 = false;
}
if (musicEnabled) music.Play();
}
I have tried :
- Tracking through MediaFailed event, but it is never failed
- Changing the UriKind to Relative and RelativeOrAbsolute
- Give/take away the slash on the beginning of the URI
- Tried to separate those two into 2 mediaElement
- Setting the mp3 to resource, content, embedded resource
What I know :
- The state always “Closed”
- If I specified the URI in XAML, it works. But I don’t wanna do that.
- All Function are working properly based on the condition. Just the music won’t start.
Any thoughts?
Answer Found! My bad for not searching in SOF deeper : stackoverflow.com/questions/7018335/… Before we could modify MediaElement in Code, we need to visualize it in the XAML first.