Reputation: 1642
I'm making a media player with basic functions, but it is behaving strangely, when pause play or stop button is clicked following exception occurs 'System.NotSupportedException' Additional information: Cannot control media unless LoadedBehavior or UnloadedBehavior is set to Manual.
Required code and xaml is as follows:
XAML:
<MediaElement x:Name="VideoPreview" LoadedBehavior="Manual" UnloadedBehavior="Stop" Width="auto" Height="250" MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded" Source="{Binding SelectedItem.SourceUri, ElementName=VideoList}" Margin="5,5,5,5" Stretch="Fill"/>
Code behind:
void OnMouseDownPlayMedia(object sender, MouseButtonEventArgs args)
{
VideoPreview.Play();
}
// Pause the media.
void OnMouseDownPauseMedia(object sender, MouseButtonEventArgs args)
{
VideoPreview.Pause();
}
// Stop the media.
void OnMouseDownStopMedia(object sender, MouseButtonEventArgs args)
{
VideoPreview.Stop();
}
When either of the mouse events is clicked I get the exception.
Upvotes: 1
Views: 5680
Reputation: 69959
The answer really is in the error text... Cannot control media unless LoadedBehavior
or UnloadedBehavior
is set to Manual
. Therefore, your solution is to set either the MediaElement.LoadedBehavior
or MediaElement.UnloadedBehavior
property to a value of MediaState.Manual
.
From the MediaElement.LoadedBehavior
Property page on MSDN:
LoadedBehavior must be set to Manual in order to interactively control media with the Play, Pause, and Stop methods.
It's amazing what you can find out from MSDN.
Upvotes: 3