Search code examples
c#mediaelementaudio-playerelementhost

MediaElement in ElementHost Windows Form C#


I'm trying to create a simple MediaPlayer, but I need to mute left/right audio channels, so I have to use MediaElement. I got the following error when I tried to add the player to the ElementHost:

Error CS1503 Argument1: impossible to convert from

'System.Windows.Controls.MediaElement' to

'System.Windows.Forms.Control'.

Here's the code:

ElementHost host = new ElementHost();
System.Windows.Controls.MediaElement player = new System.Windows.Controls.MediaElement();
host.Dock = DockStyle.Fill;
host.Controls.Add(player);

Solution

  • If you just want to play audio, I would use MediaPlayer instead of MediaElement. It doesn't have a UI component.

    If you still want to use MediaElement, set the child of ElementHost:

    ElementHost host = new ElementHost();
    System.Windows.Controls.MediaElement player = new System.Windows.Controls.MediaElement();
    host.Dock = DockStyle.Fill;
    host.Child = player;