Pages

Thursday, March 18, 2010

Creating a Media Player in the Silverlight

Hi All,
This is very interesting, the silver light is giving us so much power to create user friendly application. In this reason i have found that, we can create a media player using the silverlight and this is very easy. We do not need to bother about the code there is not that much code is involved, there is just a single component which we can use to create the Media player. That control is " MEDIA ELEMENT. "

Please check the following code to create the media Player. I have taken the Media element inside the Canvas and there is only 3 Buttons for PLAY, PAUSE , LOAD and STOP. Just pass the file name in the Source Property of that media element..

x:Class="VideoPlayer.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:DesignHeight="640" d:DesignWidth="550">

x:Name="LayoutRoot" Background="White">
>
x:Name="medPlayer" Width="640" Height="480" AutoPlay="True" />
x:Name="btnPlay" Background="Green" Width="100" Height="45" Canvas.Left="8" Canvas.Top="497" Content="Play" Click="bPlay_Click" />
x:Name="btnPause" Background="Yellow" Width="100" Height="45" Canvas.Left="112" Canvas.Top="497" Content="Pause" Click="bPause_Click" />
x:Name="btnStop" Background="Red" Width="100" Height="45" Canvas.Left="216" Canvas.Top="497" Content="Stop" Click="bStop_Click" />
x:Name="btnOpen" Background="Green" Width="100" Height="45" Canvas.Left="320" Canvas.Top="497" Content="Load" Click="bOpen_Click" />
>
>
>


C# code for media Player


private void btnPlay_Click(object sender, RoutedEventArgs e)
{
medPlayer
.Play();
}

private void btnPause_Click(object sender, RoutedEventArgs e)
{
medPlayer
.Pause();
}

private void btnStop_Click(object sender, RoutedEventArgs e)
{
medPlayer
.Stop();
}

private void btnOpen_Click(object sender, RoutedEventArgs e)
{ string myUrl = "http://localhost:4065/VideoPlayer.Web/Files/Grass_loop.wmv";
Uri myUri
= new Uri(myUrl);
medPlayer
.Source = myUri;
}


Enjoy!!!!!!!!!!

Thanks
Anil Kumar Pandey
Micorsoft MVP (C#)
Navi Mumbai, Maharshtra

No comments:

Post a Comment

Kontera