ARTICLE

WPF Media Player in VB.NET

Posted by Dinesh Beniwal Articles | WPF using VB.NET October 21, 2009
This tutorial shows you how to play and control media files in WPF and XAML using WPF Media controls.
 
Reader Level:

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.


WPFMediaPlayer.JPG 

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"

                              Name="McMediaElement"

                 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)
            McMediaElement.Play()
        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)
        McMediaElement.Play()
    End Sub
 
    ' Pause the media.
    Private Sub OnMouseDownPauseMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

        McMediaElement.Pause()
    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
 

Summary

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

Login to add your contents and source code to this article
share this article :
post comment
 

thanks for you and for you amazing idea

Posted by nightmare anas Dec 08, 2009

Dinesh Beniwal! hello!
I am Andrey. I can't hack AxMMControl for recording of sound from microphone.
help me please.
with the best regards,
Andrey

Posted by Andrey Tihonov Oct 24, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor