Setting the LoadedBehaviour and UnloadedBehaviour of MediaElement WPF Application(C#) - c#

I am playing a video in a MediaElement in a WPF application. However when I am programatically invoking the Play() , Pause() or Stop() I get an error message saying:
Cannot control media unless LoadedBehavior or UnloadedBehavior is set to Manual.
I have no idea about it.
Can someone tell me how can these be set to MANUAL?

In XAML you can set the value to the properties like this:
<MediaElement x:Name="player"
LoadedBehavior="Manual"
UnloadedBehavior="Stop" ... />
Or using code behind:
player.LoadedBehavior = MediaState.Manual;
player.UnloadedBehavior = MediaState.Stop;

Related

MediaElement XAML -> AutoPlay not a property?

I have created a MediaElement in XAML and assigned a .mpg as the source.
Looking at the XAML controls gallery from Microsoft, there are some examples such as:
<MediaPlayerElement Source="Assets/SampleMedia/fishes.wmv"
MaxWidth="400"
AutoPlay="True" />
However, I am faced with invalid markup that is returning:
The property AutoPlay was not found in type MediaElement
I have tried removing the AutoPlay and using a MediaOpened event to start the video from code behind,but there is never any trigger even?
I am not having too much success.
DO I need to add any additional resources to access certain properties?
<MediaElement x:Name="backdropMotion" Source="Images/inMotion_video.mpg"
Stretch="Fill" Margin="198,126,-326,229" Grid.Column="2"
LoadedBehavior="Play" IsEnabled="True" IsMuted="True"
Grid.Row="1" Grid.ColumnSpan="2" MediaOpened="MediaOpenedEvent"
AutoPlay="True"
>
Ultimately, I am trying to run a media element to a panel that effectively acts as an animated background, looping almost like a .gif. First step is to get the element running.

How to use Acrylic Accent in Windows 10 Creators Update?

I can't find any detailed document to use Acrylic Accent (CreateBackdropBrush). I found a post in StackOverflow which is somewhat useful but it doesn't help to get started. So please create a detailed answer to this post so that everyone can learn.
Update:
Microsoft has released an official Acrylic material document
Note:
If anyone doesn't know about Acrylic Accent. Acrylic Accent is the new feature in Windows 10 Creators Update that allows the app background to be Blurred and Transparent.
CREATOR UPDATE
XAML
You need to use a component that you put on the background of your app, let's say a RelativePanel
<RelativePanel Grid.Column="0" Grid.ColumnSpan="2" MinWidth="40" x:Name="MainGrid" SizeChanged="Page_SizeChanged"/>
<RelativePanel Grid.Column="0" Width="{Binding ElementName=MainGrid,Path=Width}" Background="#28000000"/>
<Grid>
<!--Having content here, for example textblock and so on-->
</Grid>
The second RelativePanel is used to set the shadow color above the Blur.
.CS
And then you can use the following code :
private void applyAcrylicAccent(Panel panel)
{
_compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
_hostSprite = _compositor.CreateSpriteVisual();
_hostSprite.Size = new Vector2((float) panel.ActualWidth, (float) panel.ActualHeight);
ElementCompositionPreview.SetElementChildVisual(panel, _hostSprite);
_hostSprite.Brush = _compositor.CreateHostBackdropBrush();
}
Compositor _compositor;
SpriteVisual _hostSprite;
and calling it with applyAcrylicAccent(MainGrid);
You also will need to handle the SizeChanged event :
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (_hostSprite != null)
_hostSprite.Size = e.NewSize.ToVector2();
}
Of course you will need to be on the Creator Update to run this, the CreateHostBackdropBrush() won't work on a mobile device, or in tablet mode.
Also, consider that the panel or grid that you set with a acrylic color won't be able to display any control (as far I've tested yet). So you need to use your relative panel without any control in it.
Transparent Title bar
The transparency of the title bar could be set using the following code
ApplicationViewTitleBar formattableTitleBar = ApplicationView.GetForCurrentView().TitleBar;
formattableTitleBar.ButtonBackgroundColor = Colors.Transparent;
CoreApplicationViewTitleBar coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = true;
Here a example of what the above code generate (with some other things added too.)
Fall Update 10.0.16190 and above
As Justin XL mention in an answer below, starting from the Build 16190 and above, developers have access to different Acrylic Brushes located at Windows.UI.Xaml.Media (Acrylic API) and the guidelines from Microsoft : Acrylic material guidelines
In the Creators Update Insider Preview 16193 (along with Windows 10 SDK 16190), there's a new AcrylicBrush that you can apply directly onto your element just like a normal SolidColorBrush.
<Page xmlns:media="using:Windows.UI.Xaml.Media" ...>
<Page.Resources>
<media:AcrylicBrush x:Key="HostBackdropBrush"
BackgroundSource="HostBackdrop"
TintColor="LightBlue"
TintOpacity="0.6"
FallbackColor="LightSkyBlue"
FallbackForced="False" />
</Page.Resources>
<Grid Background="{StaticResource HostBackdropBrush}" />
</Page>
Note you can change the BackgroundSource to Backdrop to sample from the app content instead of the content behind the app window. Also make sure you define an appropriate FallbackColor because you will lose the acrylic effect when the app window has lost focus or the device is in battery saver mode.

Media player framework WinRT, label showing info how to remove?

I have a Windows 8.1 and i am using the playerFramework:MediaPlayer from codeplex (https://playerframework.codeplex.com/) and when I put my cursor on the seek bar or the timer i have a label that appears on the player. I have tried to play around with the style but have not found a way to remove this, has anyone been able to remove this label?
Here is my XAML Player code:
<playerFramework:MediaPlayer
x:Name="Player"
Height="281"
Width="498"
IsCaptionSelectionVisible="False"
IsFullScreenEnabled="True"
IsFullScreenVisible="True"
IsPlayPauseVisible="True"
IsResolutionIndicatorVisible="False"
IsSignalStrengthVisible="False"
IsSkipAheadVisible="False"
IsSkipBackVisible="False"
IsTimeElapsedVisible="True"
IsTimeRemainingVisible="True"
IsTrickPlayEnabled="False"
IsVolumeVisible="True"
SeekWhileScrubbing="True"
ThumbnailImageSource="{Binding
VideoDetails.Thumbnail480Uri,
Converter={StaticResource UriConverter}}" />
I have found the solution, after looking at the playerFramework:MediaPlayer style I found the element that shows the tips. This element is called Info, so i need to hide it. So basically you need to added IsInfoEnabled="False" to your MediaElement.
thus:
<playerFramework:MediaPlayer
x:Name="Player"
MinHeight="281"
MinWidth="498"
IsInfoEnabled="False"
ThumbnailImageSource="{Binding
Thumbnail480Uri,
Converter={StaticResource UriConverter}}"/>

Binding MediaElement

We are trying to switch our application from code behind coding to MVVM pattern.
We have a MediaElement in our XAML file :
<MediaElement Name="MyMedia" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="MediaEnd" MediaOpened="MediaBegin" Margin="10,24,13.6,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Visibility="Visible" />
For the moment, with the code behind coding approach, we can access the MediaElement directly in the .xaml.cs file to call methods like MediaElement.play() or MediaElement.pause().
The problem is that we are currently blocked when we try to convert our code to MVVM, because we can't access the MediaElement in the .cs file.
We are trying to bind the MediaElement itself so that we can call play or pause methods, but we haven't succeeded yet.
Any ideas ?
Thank you in advance.
This link describes how to control it http://msdn.microsoft.com/en-us/library/ms748248.aspx
it uses an event but still calls the MediaElement.play() in the event handler.

Getting a dynamic Image through event in WPF

I'm trying to add a dynamic Image in my WPF form. I've added the Image like this:
<Image Height="212" HorizontalAlignment="Left" Margin="12,167,0,0"
Name="picture_scan" Stretch="Fill" VerticalAlignment="Top" Width="227"
Source="{Binding FingerprintSource}" />
The source leads to the following code in my service class:
public BitmapSource FingerprintSource
{
get { return fingerprintScan.WpfImageSource; }
}
The WpfImageSource is a BitmapSource. As I said, the Image is dynamic. Through an event from my Fingerprint Reader, the following code is called:
private void HandleFingerprintObtainedEvent(Fingerprint fingerprint, FingerprintImage fingerprintImage)
{
Debug.WriteLine("New fingerprint found!");
fingerprintScan = fingerprintImage;
}
When I run the program and press my finger on the reader, a new fingerprint image is found. The value fingerprintScan is being changed. But the problem is, before and after putting my finger on the scanner, the bitmap is empty (white?). What am I doing wrong? Do I need to do more besides databinding, like checking for events or something? Is it a problem when the source of the databinding is a BitmapSource instead of a BitmapImage?
You are not notifying that the property has changed.
The class that has de FingerprintSource property has to implement the interface INotifyPropertyChanged. Then you can use the property setter to raise the PropertyChanged event. Otherwise the WPF binding does not know that something has changed.
Here you have a good start point: WPF/MVVM Quick Start Tutorial

Categories

Resources