SetSource (MediaElement)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sets the source value of a MediaElement object with downloaded content.

object.SetSource(downloader, part)

Arguments

downloader

Downloader

The Downloader object that contains the downloaded content.

part

string

The name of the specific part of the downloaded content package. When the downloaded content package is a .zip file, part refers to the contents of a file name in downloader. If the downloaded content does not represent packaged content, set part to an empty string.

Managed Equivalent

MediaElement.SetSource - use a stream obtained from WebClient as input.

Remarks

The SetSource method sets the downloaded media content to the Source property of a MediaElement object. Equivalent SetSource methods exist on ImageBrush (for ImageSource) and Image (for Source).

Using the SetSource method differs notably from setting the Source property, because Source allows cross-domain Uniform Resource Identifiers (URIs), whereas the Downloader object that you create to support SetSource does not. Therefore, if you are sourcing media element content by using cross-domain URIs or referencing media by using a specific moniker such as mms:, you should set Source directly instead of using SetSource.

The first parameter, downloader, identifies the Downloader object that represents the downloaded content. The second parameter, part, identifies the specific part to retrieve from the downloaded content. If the downloaded content does not represent packaged content, such as a .zip file, part must be set to an empty string.

NoteNote:

In Silverlight 1.0, if an initial download attempt through SetSource fails (the underlying Downloader raises the DownloadFailed event), subsequent attempts to call SetSource will also fail, even if the URI is expected to result in a successful download on subsequent attempts. To avoid this issue, you should provide a DownloadFailed handler on the underlying downloader. If DownloadFailed is detected, you should set any references to the the existing MediaElement to null, remove it from the tree, create a new one (by using the CreateFromXaml method), and add it again to the object tree at the same position. Then reset any existing references with the new reference.

Example

The following JavaScript example shows how to use the SetSource method to set the Source property of a MediaElement object to the downloaded content.

// Event handler for the Completed event.
function onCompletedVideo(sender, eventArgs)
{
    // Retrieve the MediaElement object.
    var myMediaElement = sender.findName("myMediaElement");

    // Set the Source property to the "xbox.wmv" part of the downloaded zip
    myMediaElement.setSource(sender, "xbox.wmv");
}

Applies To

MediaElement