MediaElement Will Not Play
-
Good people, Why won't my MediaElement play? When I click play, I hear nothing at all. I created it in C# because I need to change sources - in effect use it as an mp3 player. What am I doing wrong? Here is the code:
private void OnButtonPlaySongClick(object sender, RoutedEventArgs e)
{
MediaElement mp3Player = new MediaElement();
mp3Player.LoadedBehavior = MediaState.Manual;
mp3Player.UnloadedBehavior = MediaState.Stop;
mp3Player.Source = new Uri(@"Media\singleladies.mp3", UriKind.Relative);
mp3Player.Volume = .8;
mp3Player.Play();
}[EDIT: So I checked it out line-by-line in the debugger and apparently everything is loaded into the player. It has the source, it's MediaElement.HasAudio = true, plus several other properties are set. However, it just won't play. I have no idea what's going on.] Now, I am wondering - if I want an mp3 Player - is this the best route to go, creating a new MediaElement each time? Probably not, huh? At any rate, any help would be appreciated. Thanks, Blitz
-
Good people, Why won't my MediaElement play? When I click play, I hear nothing at all. I created it in C# because I need to change sources - in effect use it as an mp3 player. What am I doing wrong? Here is the code:
private void OnButtonPlaySongClick(object sender, RoutedEventArgs e)
{
MediaElement mp3Player = new MediaElement();
mp3Player.LoadedBehavior = MediaState.Manual;
mp3Player.UnloadedBehavior = MediaState.Stop;
mp3Player.Source = new Uri(@"Media\singleladies.mp3", UriKind.Relative);
mp3Player.Volume = .8;
mp3Player.Play();
}[EDIT: So I checked it out line-by-line in the debugger and apparently everything is loaded into the player. It has the source, it's MediaElement.HasAudio = true, plus several other properties are set. However, it just won't play. I have no idea what's going on.] Now, I am wondering - if I want an mp3 Player - is this the best route to go, creating a new MediaElement each time? Probably not, huh? At any rate, any help would be appreciated. Thanks, Blitz
Ok, so I think the solution is to create it in the XAML, make sure that the LoadedBehavior is set to Manual in the XAML, and make sure the file you want to play has it's Copy To Output Directory set to "Copy always". Comments, and thoughts are welcome.
-
Good people, Why won't my MediaElement play? When I click play, I hear nothing at all. I created it in C# because I need to change sources - in effect use it as an mp3 player. What am I doing wrong? Here is the code:
private void OnButtonPlaySongClick(object sender, RoutedEventArgs e)
{
MediaElement mp3Player = new MediaElement();
mp3Player.LoadedBehavior = MediaState.Manual;
mp3Player.UnloadedBehavior = MediaState.Stop;
mp3Player.Source = new Uri(@"Media\singleladies.mp3", UriKind.Relative);
mp3Player.Volume = .8;
mp3Player.Play();
}[EDIT: So I checked it out line-by-line in the debugger and apparently everything is loaded into the player. It has the source, it's MediaElement.HasAudio = true, plus several other properties are set. However, it just won't play. I have no idea what's going on.] Now, I am wondering - if I want an mp3 Player - is this the best route to go, creating a new MediaElement each time? Probably not, huh? At any rate, any help would be appreciated. Thanks, Blitz
Hi, try this
class myclass
{
MediaElement me;public myclass() { MediaElement me = new MediaElement(); LoadedBehavior = MediaState.Manual; me.Source = new Uri(@"Path"); me.Loaded += new RoutedEventHandler(me\_Loaded); } void me\_Loaded(object sender, RoutedEventArgs e) { me.Play(); }
}