We explore the MediaElement in Xamarin.Forms — a new way to easily include audio and video in your Xamarin.Forms apps.
Looking for a straightforward way to include audio and video media in your Xamarin.Forms app? Take a look at the new, and currently "experimental" control, the MediaElement. This control, available in v.4.5, renders a media player on your page, along with default OS controls. Alternatively you can provide your own UI and get the exact look and feel that you want.
As this control is still in preview, you'll need to opt-in to use it. Just set the following flag in your app.xaml.cs
before the call to InitializeComponent()
:
Device.SetFlags(new string[]{ "MediaElement_Experimental" });
The MediaElement display properties include VideoHeight, VideoWidth and an Aspect property that works pretty much the same as an Image element. So AspectFill
will keep the source aspect, filling the entire height and width but may bleed outside, while AspectFit
will also maintain the source aspect, but fit within the height and width of the control. Fill
will fill the height and width regardless of the aspect.
The Source
property can be set to either a URI or a local path if you want to embed the media into your application package:
<MediaElement Source="https://sec.ch9.ms/ecn/ch9/wp7.mp4"
ShowsPlaybackControls="True"
VerticalOptions="FillAndExpand" />
or
<MediaElement Source="ms-appx:///wp7.mp4"
ShowsPlaybackControls="True"
VerticalOptions="FillAndExpand" />
If you are following along with your own app, be aware that the video in the sample code is 250 MB, so you might want to make sure you are on WiFi.
(The VerticalOptions
is required at the time of writing due to a layout issue — you may find it unnecessary, depending on your specific version.)
Note the use of the "ms-appx" scheme for the local source and the following locations if using local media:
By default, the media will begin playing as soon as it's loaded, so if that's all you need then you're done!
By default, the platform's native media controls will be used, but it's pretty straightforward to prevent that if you want to provide your own.
Set ShowPlaybackControls="False"
and use the Play()
, Pause()
and Stop()
methods to control playback with your own buttons.
You can further tweak the experience using some extra properties:
AutoPlay
specifies whether the media will automatically begin playing upon loadingVolume
controls the volumeCanSeek
specifies whether seeking is enabledIsLooping
controls automatical repeat when playback endsYou can also hook into a selection of events that will help you give a little polish to your app. You could use them to drive loading indicators or custom playback controls:
Opening
, when the control is attempting to open and validate the sourceBuffering
, the media is loadingPlaying
, the media is currently playingPaused
, playback is paused and the control is displaying the current frameStopped
, the control contains media but is not playing or paused; in this state the control will display the first frameClosed
, a transparent frame is displayed as the control has no mediaThere is one more property that you may find useful — KeepScreenOn
. Set this to True and the device should keep the screen on during playback.
The MediaElement
makes it really easy to get your media playing in your cross platform apps. Check out the official docs for more information.
Nigel Ferrissey is a software developer based in Brisbane, Australia. Nigel has been writing software for as long as he can remember and specializes in .NET, frontend and Xamarin. When he's not writing code, you can find him writing about Xamarin at www.xamarininsider.com or pursuing his other passion, Fender guitars.