MediaElement.SetSource(IRandomAccessStream, String) MediaElement.SetSource(IRandomAccessStream, String) MediaElement.SetSource(IRandomAccessStream, String) MediaElement.SetSource(IRandomAccessStream, String) Method

Definition

Sets the Source property using the specified stream and MIME type.

public : void SetSource(IRandomAccessStream stream, Platform::String mimeType)
void SetSource(IRandomAccessStream stream, winrt::hstring mimeType) const;
public void SetSource(IRandomAccessStream stream, String mimeType)
Public Sub SetSource(stream As IRandomAccessStream, mimeType As String)

Parameters

stream
IRandomAccessStream IRandomAccessStream

The stream that contains the media to load.

mimeType
String String

The MIME type of the media resource, expressed as the string form typically seen in HTTP headers and requests. The empty string "" can be passed in as the mimeType value if the MIME type is unknown.

Examples

Here is some code that creates MediaElement object and sets the media source using a FileOpenPicker control.

<MediaElement x:Name="mediaControl" Height="400" />
async private void SetLocalMedia()
{
    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();

    openPicker.FileTypeFilter.Add(".wmv");
    openPicker.FileTypeFilter.Add(".mp4");
    openPicker.FileTypeFilter.Add(".wma");
    openPicker.FileTypeFilter.Add(".mp3");

    var file = await openPicker.PickSingleFileAsync();

    // mediaControl is a MediaElement defined in XAML
    if (null != file)
    {
        var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        mediaControl.SetSource(stream, file.ContentType);
        mediaControl.Play();
    }
}

Remarks

You can use the FileOpenPicker control to get the file stream for a media file on the local system.

See also