Embedding Media Content into the web page
If you only want to use media on your Silverlight page (i.e., movie and audio data), you can even avoid creating the necessary XAML yourself. The Silverlight SDK contains a special web control called <asp:MediaPlayer> that takes care of both setting up the required XAML and embedding it into the web page (like <asp:Silverlight> does). The web control supports several properties, and the following is a list of the most interesting ones, apart from the mandatory Width and Height:
- MediaSource
-
The URL of the video or audio file to embed
- AutoPlay
-
Whether the media will be played automatically once the page has been loaded (true) or not (false)
- Muted
-
Whether the audio will be off (probably not a bad idea for commercial web sites)
- PlaceholderImageUrl
-
The URL of an image that is displayed as a placeholder while the media content is loaded
- ScaleMode
-
How to scale the content; choose from ScaleMode.None (no scaling), ScaleMode.Stretch (stretch without keeping the aspect ratio), and ScaleMode.Zoom (stretch and keep the aspect ratio)
- Volume
-
The volume of the audio portion of the media
As with <asp:Silverlight>, <asp:MediaPlayer> also requires a ScriptManager control to be present. Example 1 shows a page with an embedded WMV video. No XAML or JavaScript is to be seen.
Example 1. Embedding media content (Media.aspx)
The client output of the preceding code is very similar to the one <asp:Silverlight> was creating in the previous section. However, there are two obvious changes: the <object> tag is missing (but is created by JavaScript code), and the initialization method sets up the MediaPlayer control:
Code View:
<div>
<span id="Media1_parent"></span>
<script type="text/javascript">
//<![CDATA[
Sys.UI.Silverlight.Plugin.create("Media1_parent", "1.0", 1, "\u003cobject
type=\"application/x-silverlight\" data=\"data:,\" id=\"Media1\"
style=\"height:240px;width:320px;\"\u003e\r\n\r\n\u003c/object\u003e");
//]]>
</script>
</div>
<script type="text/javascript">
//<![CDATA[
Sys.Application.initialize();
Sys.Application.add_init(function() {
$create(Sys.UI.Silverlight.MediaPlayer, {"autoPlay":true,"mediaSource":
"video.wmv","scaleMode":1,"source":"/Silverlight_Web/WebResource.axd?d=UWvRBn2D
vlAG1qPj1sEy8HZXD30sZ0KcGEYDUyVCC-Hn51Pnb5xTrEvCUJJOmiUwlEsRMyw82Qw4YzS6q3RmGBy
9jFQYPNgFIpKmO8HkyTk1&t=633378050096020000"}, null, null, $get("Media1"));
});
//]]>
</script>
So, indeed, the JavaScript code embeds the media using the <object> HTML element. This is no surprise, since <asp:MediaPlayer> is creating XAML markup to embed the video or audio content. However, the initialization of this <object> element, in the <script> section, is different. The source property is set to this URL (the exact name will vary on your system, because the current time is embedded in the URL):
Code View:
/Silverlight_Web/WebResource.axd?d=UWvRBn2DvlAG1qPj1sEy8HZXD30sZ0KcGEYDUyVCC-Hn
51Pnb5xTrEvCUJJOmiUwlEsRMyw82Qw4YzS6q3RmGBy9jFQYPNgFIpKmO8HkyTk1&t=633378050096
020000
So, no static XAML file is sent down to the browser; instead, the
ASP.NET Futures creates a handler that will return dynamically generated
XAML content. In case you are curious, use a tool such as Firebug (http://www.getfirebug.com/) to sniff incoming traffic and
have a look at the dynamic XAML.
The Media control also supports subelements. One is
- Position
-
The start time of the chapter
- Title
-
The title of the chapter
- ThumbnailImageSource
-
The thumbnail image that marks the chapter
Example 2 shows what this can look like, when you hover over the video, all markers are shown. Clicking on one of them jumps to the appropriate position in the video.