locked
Setting the source property in the property window in the media element RRS feed

  • Question

  • Hi Team,

    1) How to set the media element source property in the property window?. Do I need to add the video file to the project or only specifying the path is enough no need to add into the project?

    2) I have a video file in the local hard disk (e:\test.wmv), can I assign the media element's source property like

    <MediaElementx:Name="MainMedia"Source="e:\test.wmv"Volume="0.05"AutoPlay="False"Height="300"Width="500" />

    I tried the above but I got an exception like

    "An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in App1.exe but was not handled in user code

    WinRT information: Failed to assign to property 'Windows.UI.Xaml.Controls.MediaElement.Source'. [Line: 64

    Thanks,

    Prakash.

    Thursday, July 25, 2013 7:24 AM

Answers

  • Add your file to the application and refer to it using a relative path:  "assets\test.wmv"


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Anne Jing Friday, July 26, 2013 7:50 AM
    • Marked as answer by Anne Jing Monday, July 29, 2013 9:23 AM
    Thursday, July 25, 2013 1:41 PM
    Moderator
  • Hi,ps_prakash02

    Just as Matt says,you can add the file to your application under a folder which named Assets,then you can code like this in XAML:

    <MediaElementx:Name="MainMedia"Source="Assets/test.wmv"Volume="0.05"AutoPlay="False"Height="300"Width="500" />

    you can refer to this link to get more imformation about how to set the MediaElement Source when files embedded with the app:

    http://msdn.microsoft.com/en-us/library/windows/apps/dn263185.aspx

     Meanwhile,you can open your local media files in hard disk using the FileOpenPicker control, then you can set MediaElement Source programmatically,code like this:

    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();
    
        var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    
        // mediaControl is a MediaElement defined in XAML
        if (null != file)
        {
            MainMedia.SetSource(stream, file.ContentType);
    
            MainMedia.Play();
        }
    }

    You can refer to this link get more imformation:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh871377.aspx

    Thanks:

    Anne Jing



    • Edited by Anne Jing Friday, July 26, 2013 7:50 AM from
    • Marked as answer by Anne Jing Monday, July 29, 2013 9:23 AM
    Friday, July 26, 2013 7:37 AM

All replies

  • Add your file to the application and refer to it using a relative path:  "assets\test.wmv"


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Anne Jing Friday, July 26, 2013 7:50 AM
    • Marked as answer by Anne Jing Monday, July 29, 2013 9:23 AM
    Thursday, July 25, 2013 1:41 PM
    Moderator
  • Hi,ps_prakash02

    Just as Matt says,you can add the file to your application under a folder which named Assets,then you can code like this in XAML:

    <MediaElementx:Name="MainMedia"Source="Assets/test.wmv"Volume="0.05"AutoPlay="False"Height="300"Width="500" />

    you can refer to this link to get more imformation about how to set the MediaElement Source when files embedded with the app:

    http://msdn.microsoft.com/en-us/library/windows/apps/dn263185.aspx

     Meanwhile,you can open your local media files in hard disk using the FileOpenPicker control, then you can set MediaElement Source programmatically,code like this:

    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();
    
        var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    
        // mediaControl is a MediaElement defined in XAML
        if (null != file)
        {
            MainMedia.SetSource(stream, file.ContentType);
    
            MainMedia.Play();
        }
    }

    You can refer to this link get more imformation:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh871377.aspx

    Thanks:

    Anne Jing



    • Edited by Anne Jing Friday, July 26, 2013 7:50 AM from
    • Marked as answer by Anne Jing Monday, July 29, 2013 9:23 AM
    Friday, July 26, 2013 7:37 AM