question

Tendy-3851 avatar image
0 Votes"
Tendy-3851 asked JackJJun-MSFT answered

WPF mediaelement acting strange

Hello, I have a project that requires a user to see a demonstration of what they are supposed to do. To do this we are using videos. My project is a VB .net windows form in .net 3.5 (x86). To do this I made a WPF user control that contains a windows.control.mediaelement. This media element is then hosted on my user control by an elementhost. When I load in this test form the video Uri gets set on a click of a button(a mp4), however when the video is loaded in, it only recognized that is had audio and not a video. So, I made a test form in my project which opened a form which has an element host on it and the same user control that will not load the video. I launched that from a button on my main form and it would play the video. However, when I closed and relaunch the test form, with that button sometimes it would play the video while other times it would just think the file only had audio. The best way I can replicate this issue is in a separate project. I have a main form that has a button to open another form that has a wpf usercontrol and a button to load and play the video. If I have a bunch of instances open eventually the newest form will not think that the mp4 only contains audio.

windows-wpfwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

If you want to play videos in windows form app, I recommend that you don't use WPF mediaelement.
You can try the following steps to use the Media Player in windows form app.

First, please create a winform app and right click Toolbox->Choose Items.

Second, click COM components->choose Windows Media Player->Click Ok.

Third, you can see Windows Media Player in your Toolbox.

Finally, you can use the following code to play the video successfully.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         AxWindowsMediaPlayer1.URL = "E:\test.mp4"
    
         AxWindowsMediaPlayer1.settings.volume = 100
         AxWindowsMediaPlayer1.Ctlcontrols.play()
     End Sub

By the way, I suggest that you use the latest .net framework version such as .net 4.7.2 and .net 4.8.

Tested Result:

53055-1.gif



1.gif (1.3 MiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.