3

I have a .NET 5.0 WinForms app in which I am trying to add the Windows Media Player. I am trying to add it to the Toolbox by doing Choose Toolbox Items -> COM Compononents which tells me the following controls were added but are not enabled.

I wonder if I have some version compatibility issues and what should I do in this case?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Veljanovski
  • 43
  • 1
  • 2
  • 8
  • Related: [Following controls were added but are not enabled](https://stackoverflow.com/q/27878153/150605), [C#.NET Unable to add windows media player control to Toolbox](https://stackoverflow.com/q/42706385/150605) – Lance U. Matthews Nov 23 '21 at 22:58
  • I've seen that, apparently, they are using .NET Framework whereas I am using .NET 5.0. – Veljanovski Nov 23 '21 at 23:05

2 Answers2

3

An easy solution is to use the WPF MediaPlayer control in code,

https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=windowsdesktop-6.0

Step 1: Open your project file and enable WPF,

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

Step 2: Initialize the control in code and play some file,

using System.Windows.Media;

namespace testwinforms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var player = new MediaPlayer();
            player.Open(new Uri(@"C:\Users\someuser\Downloads\somefile.mp3"));
            player.Play();
        }
    }
}

This works for .NET Core 3.0/3.1 and .NET 5/6.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 1
    I can't get that to work. Are you sure that's all I need? Surely I'd have to add the player to my forms' controls collection? I also just tried using VideoDrawing, no luck. – Veljanovski Jan 09 '22 at 20:54
  • @Veljanovski It works on my machine, an empty WinForms project created from `dotnet new winforms` with the changes listed above. If you cannot get that to work, you will have to show what exactly you tried (the detailed steps). – Lex Li May 29 '23 at 20:18
-2

This page describes several ways to use the Windows Media Player control.

https://learn.microsoft.com/en-us/windows/win32/wmp/player-control-guide

If you want to use Windows Media Player control, .NET Framework will be a better choice

Jingmiao Xu-MSFT
  • 2,076
  • 1
  • 3
  • 10
  • 2
    What you linked is a WPF control, not the one this question is asking about (a COM control). Also the way you interpreted "Applies to" section is wrong, and the WPF `MediaPlayer` control is available on .NET 5, though irrelevant to this question. – Lex Li Nov 24 '21 at 06:38
  • ".NET Framework will be a better choice". But is it the only choice? Should I try and migrate from .NET 5.0 to .NET Framework? – Veljanovski Nov 24 '21 at 10:54
  • Through reading the documentation, we can see that windows media player control is not applied to .NET 5.0 right now. Currently It will be a good choice to migrate to .NET Framework – Jingmiao Xu-MSFT Nov 25 '21 at 01:07