WPF Media Player in VB.NET

This tutorial shows you how to play and control media files in WPF and XAML using WPF Media controls.
  • 8487

WPF does not have a built-in media controls but it provides a wrapper around current Windows Media Player 10 ActiveX (OCX) control. A computer where this functionality will be used must have Media Player 10 or later versions installed.

WPF has two classes to work with audio, video and video with audio - MediaElement and MediaPlayer.  The MediaElement is a part of XAML UIElement and is supported by both XAML and WPF code behind but MediaPlayer is available in WPF code behind only.  


The project attached with this article let you browse a media file and plays in the TV below.


The following XAML code snippet creates a MediaElement and sets the default media file.

<MediaElement Margin="10,10,10,0 " Source="C:\Projects\WPF\MediaSamples\MediaSamples\Media\Lake.wmv"


                 Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill"

                 MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>

The Browse button click event handler code listed below uses OpenFileDialog and let you browse media files and sets MediaElement.Source to new URI that takes media file name as a parameter.

    Private Sub BrowseButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim dlg As New OpenFileDialog()
        dlg.InitialDirectory = "c:\"
        dlg.Filter = "Media files (*.wmv)|*.wmv|All Files (*.*)|*.*"
        dlg.RestoreDirectory = True

        If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Dim selectedFileName As String = dlg.FileName
            FileNameLabel.Content = selectedFileName
            McMediaElement.Source = New Uri(selectedFileName)
        End If
    End Sub

MediaElement has methods - Play, Stop, and Pause, which are used to play, stop, and pause the current media. The code for Play, Pause, and Stop buttons looks like following:

    ' Play the media.
    Private Sub OnMouseDownPlayMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)
    End Sub
    ' Pause the media.
    Private Sub OnMouseDownPauseMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

    End Sub

    ' Stop the media.
    Private Sub OnMouseDownStopMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)        McMediaElement.[Stop]()
    End Sub

MediaElement has Volume and SpeedRatio properties to set volume and the speed of the media.  

    ' Change the volume of the media.
    Private Sub ChangeMediaVolume(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
        McMediaElement.Volume = CDbl(volumeSlider.Value)
    End Sub

    ' Change the speed of the media.
    Private Sub ChangeMediaSpeedRatio(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
        McMediaElement.SpeedRatio = CDbl(speedRatioSlider.Value)
    End Sub


In this article and attached source code, I discussed how to create and use a MediaElement in WPF to play media files.


More Articles

© 2020 DotNetHeaven. All rights reserved.