Home > database >  Problems playing video with Windows Media Player in Winforms App
Problems playing video with Windows Media Player in Winforms App

Time:10-10

Goal. I created a 9-Form application in Visual Studio 2019, and I am trying to play a video file, in one of my forms, stored in the same folder with the executable file (.exe) of my winforms application.

Steps. I installed via the 'COM Components' the Windows Media Player feature, and showed in my Toolbox as it should be (Although the icon doesn't appear next to its title inside the Toolbox). After that, I drag 'n' dropped it in my form in order to write the following code based on Microsoft Documentation.

private void button1_Click(object sender, System.EventArgs e)
{
      axWindowsMediaPlayer1.URL = "video delphi.mp4";
}

Actual Result. Although when I am dragging the player in my form, it shows up as it should, but when I am running the application, it becomes blank as it was never being placed there or being dragged / used in my forms. I've checked from the properties that the player is visible and enabled. At some point, by pressing the play button I was able to hear only audio and no video.

Errors / Notifications Received. 1. When I first tried to see what was going on, I received a notification during the runtime of my app that the file isn't being supported, although insted of showing me the (.mp4) extension, it only showed (,) symbol. That notification was not coming from VS 2019 but from the player itself. 2. Currently I am receiving an C00D1179 error while the aforementioned notification doesn't appear anymore.

CodePudding user response:

In order to play a .mp4 file using Windows Media Player, try the following which has been tested.

Pre-requisites: If not already installed, a "mp4" codec must be installed - such as enter image description here

  • Click Windows Forms App (.NET Framework)

    enter image description here

  • Click Next

  • Enter desired project name (ex: MediaPlayerTest)

  • Click Create

  • Add Reference

    • In VS menu, click Project
    • Select Add Reference...
    • Click COM
    • Select Windows Media Player
    • Click OK

    Open Solution Explorer:

    • In VS menu, click View
    • Select Solution Explorer

    Open Form1 in Designer

    • In Solution Explorer, double-click Form1.cs

    Open Toolbox:

    • In VS menu, click View
    • Select Toolbox
    • Search: Windows Media Player
    • If Windows Media Player isn't found, add it to the Toolbox.

    Add Windows Media Player to Toolbox (if it doesn't already exist in the Toolbox)

    • Right-click All Windows Forms
    • Select Choose Items....
    • After it's finished loading, click COM Components.
    • Check "Windows Media Player**. enter image description here
    • Click OK
    • Windows Media Player should now exist under "All Windows Forms" in the Toolbox.

    Add Windows Media Player to Form1

    • Drag Windows Media Player from the Toolbox onto Form1.

    Add Buttons to Form1

    • Drag two buttons from the Toolbox onto Form1
    • Rename button1 to btnPlay
    • Rename button2 to btnStop
    • Double-click btnPlay to create the click event handler
    • Double-click btnStop to create the click event handler

    Modify/add code to Form1.cs

    • In Solution Explorer, right-click Form1.cs
    • Select View Code

    Copy desired .mp4 file (ex: ocean.mp4) to the folder that your .exe file exists in.

    Form1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace MediaPlayerTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                axWindowsMediaPlayer1.uiMode = "none";
            }
    
            private void btnPlay_Click(object sender, EventArgs e)
            {
                string filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().FullName), "ocean.mp4");
                axWindowsMediaPlayer1.URL = filename;
            }
    
            private void btnStop_Click(object sender, EventArgs e)
            {
                //stop
                axWindowsMediaPlayer1.Ctlcontrols.stop();
            }
        }
    }
    

    Test Media Player:

    • Run your program
    • Click Play button

    Resources:

    • Related