WPF MediaElement doens't show video when LoadedBehavior="Manual" - c#

When LoadedBehavior attribute of MediaElement, is set to "Manual",
I can see the video in the previewer, but when I run the application, the video is not displayed at all.
When LoadedBehavior is set to "Play", it works fine.
The 'Build Action' property of the video file is set to "Content".
The 'Copy to output' property of the video file is set to "Copy Always".
This is the code which doesn't display the Video:
<MediaElement Source="Videos\abc.mp4" Height="300" Width="300" LoadedBehavior="Manual"/>

So I expect the media player to be displayed with the right buttons, and let the user to chose when to play, pause, stop
Then your expectations are simply plain wrong because the MediaElement control doesn't come with any such buttons.
You'll need to add the buttons yourself and then hook up event handlers or commands to them where you call the Play, Pause and Stop methods.
The other option is to call the method(s) programmatically without the use of any buttons in the UI, but when you set the LoadedBehavior to Manual, you have to call Play() explicitly one way or the other for the media to play.

You have set the LoadedBehavior to "Manual" this means you need to start the MediaElement 'manual' (ha funny ;) ) Just call Play() in your code
Here is the doc

Related

How can I easily hide all transport controls in UWP MediaPlayerElement except a few?

I have a MediaPlayerElement that plays a video automatically and I want the user to be only able to seek in the video and press pause/stop/play.
It looks like I have to set AreTransportControlsEnabled to true and then hide all the controls I don't want one by one as per default all controls are visible.
So I did this:
<MediaPlayerElement x:Name="mediaPlayer" AreTransportControlsEnabled="True">
<MediaPlayerElement.TransportControls>
<MediaTransportControls
ShowAndHideAutomatically="True"
IsFullWindowButtonVisible="False"
IsNextTrackButtonVisible="False"
IsPreviousTrackButtonVisible="False"
IsVolumeButtonVisible="False"
IsZoomButtonVisible="False"
IsFastForwardButtonVisible="False"
IsFastRewindButtonVisible="False"
IsPlaybackRateButtonVisible="False"
IsRepeatButtonVisible="False"
IsSkipBackwardButtonVisible="False"
IsSkipForwardButtonVisible="False"
Windows10version1803:IsCompactOverlayButtonVisible="False"
IsSeekBarVisible="True"
IsSeekEnabled="True"
IsStopButtonVisible="True"
/>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
For my taste this looks really cumbersome. Isn't there are setting like "hideall=true" and then I could only enable those I want. And for example, there seems to be no way to also hide the "cast to device" button, so with the current approach the user would always see this button, what I don't really like:
Any ideas?
Removing "CastButton" from generic.xaml didn't work out for me. I found a solution for removing Cast to Device button in another forum :
https://social.msdn.microsoft.com/Forums/windows/en-US/e3307864-f194-4197-9f0d-bb2b8cd7228c/uwp-custom-media-transport-controls-hide-custom-buttons
Here is the working code for removing "CastButton" AppBarButton from Mediaplayer at runtime.
public class CustomMediaTransportControls: MediaTransportControls
{
protected override void OnApplyTemplate()
{
AppBarButton CastButton = GetTemplateChild("CastButton") as AppBarButton;
var MediaControlsCommandBar = GetTemplateChild("MediaControlsCommandBar") as CommandBar;
MediaControlsCommandBar.PrimaryCommands.Remove(CastButton);
base.OnApplyTemplate();
}
}
You can create your own media transport controls by setting AreTransportControlsEnabled to false, and using the Play and Pause methods on MediaPlayer.
For more info and examples, see Create custom transport controls.
The official Media transport controls sample will be a good start.
For example, in the official sample, if you do not want to show the 'cast to device' button, you could directly remove the AppBarButton named as CastButton in the generic.xaml.

Why a video won't load if Source is set from another Window in WPF?

I had 2 mediaelements in one window, my program worked correctly asigning video to each mediaelement.
I created a second wpf window, a window I have access with no problem, I can set a Image Source and it works, I can change the selectedIndex of a TabControl and it works.
The problem is when I want to assign the source of a mediaelement. The Source property is assigned, but it never loads, loaded and mediafailed events never trigger, not matter if I assign the events in the xaml, in the cs of the first or the seccond Window.
When I use a button with a breakpoint on the first window to watch variables this is the result:
mediaelement.Source is set correctly with the URI object.
mediaelement.IsLoaded is false;
Test flags I set in the mediafailed and loaded events never set.
This is what we know:
The URI is not the problem because it works when the mediaelement is on the first window.
I'm creating the second window from the first window.
I can set Image.Source in the second window whit no problem, and image.isLoaded changes to true.
I'm using Dispatcher.Invoke to set the values.
If i use a throw new NotImplementedException(); in the events it never throws.
What I'm doing wrong?

Playing a video without opening another window or having the buttons

I have a C# WinForms application, when you press a button at run-time, the application has to play a video but not in another new window, I want it to be in the form itself, also I don't have to see the Play/Pause/Stop etc. buttons of the Windows Media Player, just to have the video playing and when it ends return to the beginning where the button is situated. I couldn't find a question similar to this one, anyone has any ideas ? If any more information is needed just let me know.
Thanks alot!
Using Windows Media Player Control:
You can hide control buttons by setting uiMode property to none.
You can enable loop mode using settings.setMode method and setting loop to true
You can set a file path to play, by assign a path to URL property.
Since the settings.autoStart property is true by default, when you set the url, the player plays the media. Also you can use commands like Ctlcontrols.play or other control commands.
Sample Code
this.axWindowsMediaPlayer1.uiMode = "none";
this.axWindowsMediaPlayer1.settings.setMode("loop", true);
this.axWindowsMediaPlayer1.URL = #"d:\video1.wmv";
Resources
How to: Embed Windows Media Player on a Form
AxWindowsMediaPlayer Object Properties, Methods and Events

C# PresentViewController to a viewcontroller in storyboard

I am creating my first Xamarin-application (C#) in iOS and I am having trouble with navigating in storyboard. All of my tabbar-,navigation- and viewcontrollers are in the storyboard. In almost every viewcontroller, I added a menu (Facebook- and YouTube-style) which slides from the side. From there it has to be possible to return to the first viewcontroller (home) in storyboard. Because the menu is active in almost every controller of my application, I don't use segues (It would be a mess in my storyboard).
So I would want to use PresentViewController(), but when I do this, the page turnes black and doesn't show anything.
homeViewController home = new homeViewController();
PresentViewController(home, true, null);
Does it turn black because PresentViewController expects a XIB-file from the homeViewController? I want the user to stay in the storyboard, so he can continue by segueing through my application.
Does someone has an idea?
Thanks in advance.
Did you create the empty constructor yourself? If so, then its not loading the information from the storyboard, which is why it's black.
To create the controller call this:
var controller = Storyboard.InstantiateViewController("HomeViewController") as UIViewController;
You will also have to open the controller in XCode and set its "Storyboard ID" to "HomeViewController". This is the second tab from the left (I think) in the details pane. The same tab where you change the class.

How I will get dropping media on mediaelement in wpf?

I am working with mediaelement in wpf, but the problem is I can't drop media on medaiaelement.
can anyone tell me the solution.the following code is .cs file code. I set allow drop property= true
private void mediaElement1_Drop(object sender, System.Windows.DragEventArgs e)
{
String[] FileName = (String[])e.Data.GetData(System.Windows.Forms.DataFormats.FileDrop, true);
if (FileName.Length > 0)
{
String VideoPath = FileName[0].ToString();
mediaElement1.Source = new Uri(VideoPath);
mediaElement1.Play();
}
e.Handled = true;
}
Tried it myself. Actually it will work after you play something there.
Here's the point: assume we have a Grid:
<Grid AllowDrop="True"></Grid>
It won't allow drop.
Now the following
<Grid Background="Transparent" AllowDrop="True"></Grid>
Will allow drop.
The first Grid doesn't have background at all, so actually there's no way to drop anything on it - there's no grid. And in second case there is grid's background even though we can't see it.
The same thing applies to MediaElement. Unfortunately it doesn't have any Background or Content property, so it won't allow drop until you start playing something there.
Solution is to handle drop on MediaElement parent container.
By the way, don't forget to set LoadedBehavior="Manual" for MediaElement so that it will play dropped file.
EDIT.
Here is explanation why MediaElement doesn't allow drop till any content was loaded in it.
Every WPF component is in fact composed of some other basic elements: Borders, Grids, ContentPresenters etc. So something inside the MediaElement handles drop. I cannot tell you what element it is because MediaElement's Template is not accessible. But it really doesn't matter what exactly is the element that handles drag and drop there. What does matter is that there's is nothing material in MediaElement's area until you load content on it - just like in case of my example with Grid at the beginning of this post. I mean that when you move mouse cursor over it's area there is nothing between cursor and MediaElement's container. Try to handle MouseDown event: result will be the same - it won't fire until you load any video. Why? Because there is nothing to raise event. Nothing cannot raise anything.
As I mentioned before there is great difference between Background="{x:Null}" and Background="Transparent": in first case there's no background brush, no background, but in second case there is one. Feel the difference.

Categories