When beginning a media-based application for Windows Phone, perhaps the easiest thing to do is to use the Silverlight MediaElement control. With just a few lines of code, you can easily play or stream video in your Windows Phone Application. This article is going to explain how to use MediaElement control in Windows 7 Phone development.
Getting started:
1. Open Visual Studio
2. Create new Silverlight Windows 7 Phone Development Project
3. Enter your project Name
4. Click on button ‘Ok’
Now drag and drop MediaElement control form toolbox into MainPage.xaml design page; after doing this set media path by selecting Source property of MediaElement control.
Now write down the following code to build this layout;
Code of ‘MainPage.xaml’:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="My Application" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="MediaElement Demo" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<MediaElement Height="433" HorizontalAlignment="Left" Margin="12,0,0,140" Name="mdElementDemo" VerticalAlignment="Bottom" Width="438" />
</Grid>
</Grid>
Code of ‘MainPage.xaml.cs’:
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace MediaElementDemo
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded+=new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(objectsender, RoutedEventArgse)
{
MediaPlayerLauncher launcher=new MediaPlayerLauncher();
launcher.Media=new Uri("http://www.robtowns.com/music/blind_willie.mp3", UriKind.RelativeOrAbsolute);
launcher.Controls= MediaPlaybackControls.All;
launcher.Show();
}
}
}
Now execute your program then following output window will be appearing.
This is the complete description about Media Control in Windows Phone 7. I hope you will be enjoying it. Thanks for reading this article.
Leave Comment