Search code examples
c#formsuser-controlsaxwindowsmediaplayer

creating media player button from user control in main form c#


I want to play media player in main form, from user control that media player is in there. How can C call media player with buttons that I put them in main form?

    private void player1_Load(object sender, EventArgs e)
    {

    }

    private void bunifuImageButton7_Click(object sender, EventArgs e)
    {

    }

    private void bunifuImageButton1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            this.bunifuMaterialTextbox1.Text = ofd.FileName;
        }
    }
}

Solution

  • Presumably, you have added a media player object to your form, and it's the COM object, axWindowsMediaPlayer (from your tags). Let's assume it has the default name of axWindowsMediaPlayer1.

    I am also assuming that your bunifuImageButton7 is the Play button.

    You need to load the path of your media file into the mediaplayer's URL property, and then activate its Play control, like this:

    private void bunifuImageButton7_Click(object sender, EventArgs e)
    {
    axWindowsMediaPlayer1.URL = bunifuMaterialTextbox1.Text;
    
    axWindowsMediaPlayer1.Ctlcontrols.play();
    }
    

    This was easily found on the Microsoft site. You might want to visit their site and bookmark it, as it contains everything you need to know about the media player:

    https://learn.microsoft.com/en-us/windows/desktop/wmp/axwindowsmediaplayer-object--vb-and-c

    Documentation for all the other player controls is here: https://learn.microsoft.com/en-us/windows/desktop/wmp/iwmpcontrols--vb-and-c