Search code examples
vb.netmedia-player

how to get media element to work in universal app?


i try get media element to work with the is universal application on windows, but not working, i cant not select source, its not working like other version WPF, thanks for time.

mediaElement.Source = New Uri("C:\Users\User\My Videos\Movie.mp4", UriKind.RelativeOrAbsolute)

this work ok, but not in universal. its not playing or evening getting the source.


Solution

  • For Windows Universal, it doesn't work the same way as it would in WPF, I had the same problem a few weeks ago, with that being said, you need to import the picker from Windows.Storge.Pickers, its kind like openfiledialog and use the following example i have quickly done for you. Change it around to suit your needs.

    Imports Windows.Storage.Pickers
    
    
    Private Async Sub button1_Click(sender As Object, e As RoutedEventArgs) Handles button1.Click
    
        Dim OpenFileDialog1 = New FileOpenPicker
        OpenFileDialog1.CommitButtonText = "Play"
        OpenFileDialog1.FileTypeFilter.Add(".mp4")
        OpenFileDialog1.SuggestedStartLocation = PickerLocationId.HomeGroup
    
        Dim Movie = Await OpenFileDialog1.PickSingleFileAsync
        If Movie IsNot Nothing Then
            Dim SelectedMovie = Await Movie.OpenAsync(Windows.Storage.FileAccessMode.Read)
            mediaElement.SetSource(SelectedMovie, Movie.ContentType)
            mediaElement.Play()
        End If
    End Sub
    

    If you have any problems with it, let me know and i will be happy to help you out, also dont forget to have the button & mediaelement component dragged to your windows form/designer, Happy Coding!