locked
mediaElement source URI binding not working RRS feed

  • Question

  • var graphicFile = (StorageFile)parameter; if (graphicFile != null) { //if video if (regexVideo.IsMatch(graphicFile.ContentType)) {

    //why is this not working VideoSouce = new Uri(graphicFile.Path, UriKind.RelativeOrAbsolute); } }

    VideoSource is binded to the source property of media element which is set to autoplay by default

    but that uri wont make play the video

    Friday, October 16, 2015 7:29 AM

Answers

  • Hi LazyCodingGuy,

    Yes, I can reproduce your issue in my side and get the invalid path error, it is because that when we use the FileOpenPicker to pick a file, the graphicFile's path will be something like c:\path1\file.mp4. But our app doesn't have access to the file system outside of its own app directory, so it cannot read c:\path1\file.mp4. What we need to do is to copy the graphicFile to the LocalFolder, after that it should work:

    StorageFolder LocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    StorageFile MyNewFile =await graphicFile.CopyAsync(LocalFolder,"mytestfile",NameCollisionOption.ReplaceExisting);
    VideoSouce = new Uri(MyNewFile.Path, UriKind.Absolute);
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, October 21, 2015 6:30 AM

All replies

  • Hi LazyCodingGuy,

    Welcome to the Developing Universal Windows apps forum!

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    Since I do not have all your code, so I have created the following example based on your code:
    In the MainPage.xaml:

    <MediaElement Source="{Binding VideoSouce}" AutoPlay="True" AreTransportControlsEnabled="True"></MediaElement>
    <Button Click="Button_Click" Content="Button"></Button>

    In the MainPage.xaml.cs:

     public class play
        {
            public Uri VideoSouce { get; set; }
        }
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                
            }
    
            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                var parameter =await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("test.mp3");
                var graphicFile = (StorageFile)parameter;
                play play = new play();
                if (graphicFile != null)
                {
                    play.VideoSouce = new Uri(graphicFile.Path, UriKind.RelativeOrAbsolute);
                }
                this.DataContext = play;
            }
        }

    It works fine in my side, I will recommend you put a breakpoint in this line:VideoSouce = new Uri(graphicFile.Path, UriKind.RelativeOrAbsolute) to check if you can get the correct Uri.
    Or please try to post a simple reproduced project in here to help analyze this issue.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, October 16, 2015 9:06 AM
  • MainPageView.xaml

      <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
             <MediaElement Source="{x:Bind viewModel.VideoSouce, Mode=OneWay}" AutoPlay="True" 
                           AreTransportControlsEnabled="True" x:Name="test"/>
        </Grid>

    MainPageView.xaml.cs

    private MainPageViewModel viewModel;
    
            public LoadAndPlayView()
            {
                this.InitializeComponent();
                this.DataContext = viewModel = new MainPageViewModel();
            }

    MainPageViewModel.cs

    private Uri _videoSouce; public Uri VideoSouce { get { return _videoSouce; } set { Set(ref _videoSouce, value); }

    public override void OnNavigatedTo(object parameter, NavigationMode mode, IDictionary<string, object> state) { var graphicFile = (StorageFile)parameter;

    if (graphicFile != null) { //if video if (regexVideo.IsMatch(graphicFile.ContentType)) {

    // the uri is set here but its not doing anything to the media elemetn

    //the media element says invalid path or invalid type but its just

    //mp4 I tried other types too I think the path is wrong but why

    VideoSouce = new Uri(graphicFile.Path, UriKind.Absolute); } } } }

    otherpageViewModel
       public async void buttonClick()
            {
                //Create and open the file picker for vid or photo
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.ViewMode = PickerViewMode.Thumbnail;
                openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                openPicker.FileTypeFilter.Add(".mp4");
                openPicker.FileTypeFilter.Add(".mkv");
                openPicker.FileTypeFilter.Add(".avi");
                openPicker.FileTypeFilter.Add(".jpg");
                openPicker.FileTypeFilter.Add(".jpeg");
                openPicker.FileTypeFilter.Add(".png");
    
                StorageFile graphicFile = await openPicker.PickSingleFileAsync();
    
                NavigationService.Navigate(typeof(MainPageView), graphicFile);
            }



    Friday, October 16, 2015 5:19 PM
  • Hi LazyCodingGuy,

    Yes, I can reproduce your issue in my side and get the invalid path error, it is because that when we use the FileOpenPicker to pick a file, the graphicFile's path will be something like c:\path1\file.mp4. But our app doesn't have access to the file system outside of its own app directory, so it cannot read c:\path1\file.mp4. What we need to do is to copy the graphicFile to the LocalFolder, after that it should work:

    StorageFolder LocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    StorageFile MyNewFile =await graphicFile.CopyAsync(LocalFolder,"mytestfile",NameCollisionOption.ReplaceExisting);
    VideoSouce = new Uri(MyNewFile.Path, UriKind.Absolute);
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, October 21, 2015 6:30 AM