none
MediaPlayer control not playing video from the Video library

    Question

  • Hi,

     

    I am just playing around with the MediaPlayer and MediaElement controls in Xaml using C#. When my video is local to where the page is executing and I set a relative path to the video file it runs fine in the MediaPlayer control but when I use a File Picker control to pick a file from the hard drive and I set the source like:

     

     MediaTarget.Source = new Uri(file.Path, UriKind.Absolute);
    
     MediaTarget.Play();

     

    So it sets the file path the absolute location of the file on the hard drive which is not in the same location as the page is execuiting from. When The code executes, the MediaPlayer control shows an error on the bottom of the control as 'Error loading from server'. 

     

    Am I missing something, It seems like a very straightforward task and I am sure I am missing something very obvious. 

    I'd really appreciate everyone's help. Thanks in advance.

     

     

    Thursday, January 05, 2012 10:57 PM

Answers

  • Hi Nasir,

    Your app doesn't have permissions to read from the Video library directly via the file system, so the MediaPlayer cannot see the video accessed by file path there. The app can access the file via a StorageFile object because the Storage system can get broader access as granted by capabilities or by the user's selection in the FileOpenPicker. This requires letting the StorageFile object access the file and return a stream to its data rather than reading the path out of the StorageFile and then trying to use it directly. The stream can be set on the MediaPlayer via its SetSource method:

            private async void playButton_Click(object sender, RoutedEventArgs e)
            {
                FileOpenPicker picker = new FileOpenPicker();
                picker.FileTypeFilter.Add(".wmv");
                StorageFile file = await picker.PickSingleFileAsync();
                IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
                mediaPlayer.SetSource(stream,file.ContentType);
            }
    

     


    --Rob


    Thursday, January 05, 2012 11:39 PM

All replies

  • Hi Nasir,

    Your app doesn't have permissions to read from the Video library directly via the file system, so the MediaPlayer cannot see the video accessed by file path there. The app can access the file via a StorageFile object because the Storage system can get broader access as granted by capabilities or by the user's selection in the FileOpenPicker. This requires letting the StorageFile object access the file and return a stream to its data rather than reading the path out of the StorageFile and then trying to use it directly. The stream can be set on the MediaPlayer via its SetSource method:

            private async void playButton_Click(object sender, RoutedEventArgs e)
            {
                FileOpenPicker picker = new FileOpenPicker();
                picker.FileTypeFilter.Add(".wmv");
                StorageFile file = await picker.PickSingleFileAsync();
                IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
                mediaPlayer.SetSource(stream,file.ContentType);
            }
    

     


    --Rob


    Thursday, January 05, 2012 11:39 PM
  • Thanks very much Rob, that really did the trick. I did try the IRandomStream but I wasn't sure what to set in the content type. 

    Thanks for the clear example.

    Friday, January 06, 2012 7:18 PM