Silverlight Work MediaElement control in VB.NET

In this article you will learn how to Work with MediaElement control in Silverlight.
  • 1940

MediaElement control: Silverlight is able to do so using an embedded media player so that the client system does not need to have a player installed. This is a different approach from other media technologies. Adding media to a page is as simple as adding a MediaElement to your markup and providing a Uniform Resource Identifier (URI) to the media to play. You can interactively control media playback by using the Play, Pause, Stop and Volume controling methods of a MediaElement object.

Example of an MediaElement control

<UserControl x:Class="SilverlightApplication17.MainPage"
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
    <Grid x:Name="LayoutRoot" Background="AliceBlue" Width="551"> 
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="5*"/>
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions>
            <RowDefinition Height="94*" />
            <RowDefinition Height="84*" />
            <RowDefinition Height="91*"/>
            <RowDefinition Height="211*" />
        </Grid.RowDefinitions>
        <TextBlock Text="Volume"
                   Grid.Row="0"
                   Grid.Column="0" 
                   TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
        <Slider x:Name="VolumeSlider" Grid.Column="1"
                Orientation="Vertical" 
                Minimum="0"
                Maximum="1" SmallChange="0.1"
                LargeChange="0.2" 
                ValueChanged="VolumeSlider_ValueChanged" Margin="0,0,410,0" /> 
        <TextBlock Text="Balance"
                   Grid.Row="1"
                   Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <Slider x:Name="BalanceSlider"
                Grid.Row="1"
                Grid.Column="1"
                Orientation="Vertical" 
                Minimum="-1"
                Maximum="1" SmallChange="0.1"
                LargeChange="0.2" 
                ValueChanged="BalanceSlider_ValueChanged"
                Value="0" Margin="0,0,411,0" />
        <MediaElement x:Name="MyMedia"
                      Canvas.Left="100"
                      Canvas.Top="100"
                      Source="/Assets/Butterfly.wmv"
                      AutoPlay="True"
                      Grid.Row="3"
                      IsMuted="False"
                      Volume="0"
                      Grid.ColumnSpan="2"
                      Balance="0" />
        <TextBlock HorizontalAlignment="Center" Margin="25,45,34,25" Text="Bass"VerticalAlignment="Center" Grid.Row="2"
 Height="21" Width="33" />
        <Slider LargeChange="0.2" Margin="0,0,411,9" Maximum="1" Minimum="-1" Name="Slider1"Orientation="Vertical" 
SmallChange
="0.1" Value="0" Grid.Row="2"
        Grid.Column="1" />
    </Grid>
</UserControl>

//File: MainPage.xaml.vb

Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Partial Public Class MainPage
    Inherits UserControl
    Public Sub New()
        InitializeComponent()
    End Sub
    Private Sub VolumeSlider_ValueChanged(ByVal sender As ObjectByVal e AsRoutedPropertyChangedEventArgs(Of Double))
        MyMedia.Volume = e.NewValue
    End Sub
    Private Sub BalanceSlider_ValueChanged(ByVal sender As ObjectByVal e AsRoutedPropertyChangedEventArgs(Of Double))
        MyMedia.Balance = e.NewValue
    End Sub
    Private Sub BassSlider_ValueChanged(ByVal sender As ObjectByVal e AsRoutedPropertyChangedEventArgs(Of Double))
        MyMedia.Balance = e.NewValue
    End Sub
    Private Sub TraceSlider_ValueChanged(ByVal sender As ObjectByVal e AsRoutedPropertyChangedEventArgs(Of Double))
        MyMedia.Balance = e.NewValue
    End Sub
End
 Class

Output Window

cont.gif
 

Conclusion

Hope this article would help you understand how to Work with MediaElement control in Silverlight.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.