MULTIMEDIA

Silverlight : Build a Download and Playback Progress Bar

10/9/2010 4:41:09 PM
One thing every media content downloader needs is a bar that shows both download progress and playback position. Thankfully, Silverlight fully supports the concept of custom controls. Listing 1 shows the XAML, and Listing 2 shows the code-behind for this control.
Listing 1. PlayDownloadProgressControl.xaml
<UserControl x:Class="VideoPlayer.PlayDownloadProgressControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas x:Name="LayoutRoot" Background="White">
<Line x:Name="DownloadProgress" X1="0" Y1="0"
X2="0" Y2="0" Stroke="DarkGray"
Canvas.Left="0" Canvas.Top="2" StrokeThickness="3"/>
<Line x:Name="PlaybackProgress" X1="0" Y1="0"
X2="0" Y2="0" Stroke="DarkGreen"
Canvas.Left="0" Canvas.Top="2" StrokeThickness="3"/>
</Canvas>
</UserControl>

Listing 2. PlayDownloadProgressControl.xaml.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;

namespace VideoPlayer
{
public partial class PlayDownloadProgressControl : UserControl
{
public PlayDownloadProgressControl()
{
InitializeComponent();
}
public void UpdatePlaybackProgress(double playbackPercent)
{

PlaybackProgress.X2 = PlaybackProgress.X1 +
(playbackPercent * LayoutRoot.ActualWidth / 100.0);
}

public void UpdateDownloadProgress(double downloadPercent)
{
DownloadProgress.X2 = DownloadProgress.X1 +
(downloadPercent * LayoutRoot.ActualWidth / 100.0);
}
}
}


To incorporate this into video player app, add the following bolded lines to the MainPage.xaml file from Listing 1:

...
<MediaElement Grid.Row="0"
x:Name="videoPlayer"
Stretch="Fill"
DownloadProgressChanged="videoPlayer_DownloadProgressChanged"
>
</MediaElement>
<local:PlayDownloadProgressControl x:Name="progressBar"
Height="4" Grid.Row="0"
VerticalAlignment="Bottom"/>
<StackPanel Grid.Row="1">
...

The DownloadProgressChanged event notifies you when a new chunk has been downloaded so you can update the progress:

private void videoPlayer_DownloadProgressChanged(object sender,
RoutedEventArgs e)
{
progressBar.UpdateDownloadProgress(
100.0 * videoPlayer.DownloadProgress);
}

Other  
 
Top 10
-  Programming Role-Based Security
-  Deploying the Client for Microsoft Exchange Server 2010 : Preparing the Deployment
-  The Android Army invades! (Part 3) : Disaster recovery
-  Windows 7 : Indexing Your Computer for Faster Searches (part 1) - Adding or Removing Indexed Locations
-  Windows Server 2008 R2 : Manage Remote Desktop Services (part 1) - Administer Remote Desktop Session Host
-  Modifying Display Appearance and Video Settings
-  HP X7000 Wi-Fi Touch Mouse - The Wi-Fi Touch
-  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 1) - 2D Texture Mapping Demo
-  Windows Server 2008 : Designing Organizational Unit and Group Structure - Group Policies and OU Design
-  Other Snippet Of Photography News From Across The Globe
Most View
-  All in One - The iDevice To Rule Them All
-  Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
-  Windows 7 : General Maintenance Tools (part 1) - Updating Your Computer
-  SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 4) - Reviewing Backup and Restore Settings in SPCA
-  Understanding SharePoint 2010 Extranet Security
-  Synchronizing Mobile Data - Using Merge Replication (part 2) - Programming for Merge Replication
-  Birds Of Prey (Part 3) - Behind the scenes
-  Need To Know A Snapshot Of What Is Exciting Us (Part 2) - Olympus OM- D brings a new style to CSCs
-  ASP.NET AJAX Extensions : Selective Page Updates with Partial Rendering
-  SQL Server 2005 : Beyond OWC: Full-On OLAP Development (part 3) - XMLA at Your Service
-  Programming .NET Security : Hashing Algorithms Explained
-  Programming Symmetrical Encryption (part 2) - Configuring the Algorithm
-  HomePlug Buyer’s Guide (Part 3) - ZyXEL PLA4205 Gigabit Adaptor & Netgear Powerline AV+ 500 Adapter XAV5501
-  Collaborating via Web-Based Communication Tools : Evaluating Web Conferencing Tools
-  IIS 7.0 : Managing Web Sites - Administrative Tasks - Adding a New Web Site
-  Using MySQL Enterprise (part 3) - Query Analyzer
-  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 4) - Windows Mobile & BlackBerry
-  Create Your Own E-Books (Part 5) - Format Wars, Format Conversion & Apple Steps Forward
-  Building LOB Applications : Navigating RIA LOB Data
-  Network Attached Storage Round-Up (Part 2) - Limitations Of NAS, NAS Noise And Power