Delen via


MediaElement.SetSource(IRandomAccessStream, String) Method

Definition

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

public:
 virtual void SetSource(IRandomAccessStream ^ stream, Platform::String ^ mimeType) = SetSource;
void SetSource(IRandomAccessStream const& stream, winrt::hstring const& mimeType);
public void SetSource(IRandomAccessStream stream, string mimeType);
function setSource(stream, mimeType)
Public Sub SetSource (stream As IRandomAccessStream, mimeType As String)

Parameters

stream
IRandomAccessStream

The stream that contains the media to load.

mimeType
String

Platform::String

winrt::hstring

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.

Applies to

See also