1

I am making application based on Windows Media Player in Visual Studio C#, I need media time in milliseconds, the media player I am trying to copy is:

enter image description here

But in Visual Studio C#, the default Media Player Version 1.0 is available, which has the old look, and media time is in seconds only:

enter image description here

How can I achieve Media Player as shown in first image, which has a better look and media time in milliseconds?

I have tried options in Windows Media Player & search in web but all shows default media player.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Adil Ahmed
  • 37
  • 4
  • That Media Player isn't a generic component. It's a wrapper over the *old* Media Player application, that's why it looks so weird. It was used in Windows Forms apps simply because there was no other alternative. WPF added proper audio and video elements – Panagiotis Kanavos Mar 03 '23 at 10:15
  • What kind of application are you building? Windows Forms? WPF? MAUI? UWP? Only Windows Forms had to use the Media Player component. All other stacks have native support for audio and video, usually through controls named `MediaElement` or something similar. For example, [How to: Control a MediaElement (Play, Pause, Stop, Volume, and Speed)](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-control-a-mediaelement-play-pause-stop-volume-and-speed?view=netframeworkdesktop-4.8) shows how to use MediaElement in WPF – Panagiotis Kanavos Mar 03 '23 at 10:16
  • WinUI has [MediaPlayerElement](https://learn.microsoft.com/en-us/windows/apps/design/controls/media-playback) and MAUI has a [MediaElement](https://devblogs.microsoft.com/dotnet/announcing-dotnet-maui-communitytoolkit-mediaelement/) that maps to different players, depending on the platfrom. On Windows it uses WinUI's MediaPlayerElement – Panagiotis Kanavos Mar 03 '23 at 10:20
  • I am making Windows Form Application on Visual Studio Community 2022, but I don't how media player shows time in milli seconds. – Adil Ahmed Mar 03 '23 at 17:19
  • How can I get the wrapper over old Media Player application? – Adil Ahmed Mar 03 '23 at 17:22

2 Answers2

0

Updated 2:

enter image description here

Directly create a WindowsFormsControlLibrary (.Net Framework 4.8)

Use a label to directly cover the original time, and set the anchor of the label to right, bottom.

Use a timer control, which is enabled by default, and the interval is 100 milliseconds.

Use a button control to open the file to be played, and set the anchor to left and bottom.

Set the media dock property to fill. Right click on it and click send to back so it doesn't block other controls.

The anchor is set to prevent the control from being misaligned when it is stretched.

using System;
using System.Windows.Forms;

namespace WindowsFormsControlLibrary1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = TimeSpan.FromMilliseconds(axWindowsMediaPlayer2.Ctlcontrols.currentPosition * 1000).ToString(@"mm\:ss\.f");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Video Files|*.mp4;*.avi;*.wmv;*.*";
            openFileDialog1.Title = "Select a Video File";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                axWindowsMediaPlayer2.URL = openFileDialog1.FileName;
            }
        }
    }
}

Just import it into the winform program as a dll and use it.

Updated:

Use the ElementHost control to place a WPF UIElement on your Windows Forms control or form.

using System;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        ElementHost elementHost = new ElementHost();
        MediaElement mediaelement = new MediaElement();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mediaelement.Source = new Uri(@"C:\Users\Administrator\Downloads\demo1.mp4");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Text = TimeSpan.FromMilliseconds(mediaelement.Position.TotalMilliseconds).ToString(@"mm\:ss\.f");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            elementHost.Dock = DockStyle.Fill;
            elementHost.Child = mediaelement;
            this.Controls.Add(elementHost);
        }
    }
}

I used mediaelement.Position.TotalMilliseconds to get the milliseconds of the video, then format it to look like you want. I used the timer control to refresh the text value of the textbox.

Although it is possible to use wpf controls in winforms, I still hope you create WPF programs directly.

enter image description here


You can create your own Windows Media Player, but I don't know if you are using a winforms program or a Wpf program.

This is an official example from Microsoft, it uses WPF show.

You can obtain the video time and modify it according to your needs.

Here's an example of how I get the time and format it:

var ti = this.mediaelement.Position.TotalMilliseconds;
TimeSpan timeSpan = TimeSpan.FromMilliseconds(ti);
string formattedTime = timeSpan.ToString(@"hh\:mm\:ss\.fff");
TextBox.Text = formattedTime;

enter image description here

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
  • I am using WinForm application. What is Media Element? is it available in Winform application? – Adil Ahmed Mar 04 '23 at 16:26
  • @AdilAhmed I have edited my answer. Although it is possible to use wpf controls in winforms, I still hope you create WPF programs directly. If it solves your issue, please mark it as the answer. – Jiale Xue - MSFT Mar 07 '23 at 05:44
  • Xue, Thank you for detail description, I got the concept of milli sec, but how can i make UI like in the first image in my question? – Adil Ahmed Mar 07 '23 at 13:38
  • @AdilAhmed Currently you can only use version 1 in it as shown in Figure 2. As shown in my edit, you can use usercontrol to achieve the effect in my picture. If you want to use MediaElement, then you need to design UI and implement it yourself. I'm not quite sure about the version in your picture 1. From my research, it's hard for me to give you further results without new information. – Jiale Xue - MSFT Mar 08 '23 at 07:48
  • Thank you Xue, I check the installation folder of app (picture 1 in my question) and found the same libraries files as my application (Picture 2 in my question) AxInterop.WMPLib.dll & Interop.WMPLib.dll, but I dont know how they made new look like new media player (picture1 in my question). Do I need to use Windows Media Player SDK? – Adil Ahmed Mar 08 '23 at 12:33
0

Uno Platform released Media Player Element, which works across the full reach of .NET - Windows/Linux/Mac/iOS/Android/Web.

check https://platform.uno/blog/4-9-release-media-player-element-on-mobile-web-linux-webview2-support/

mtmattei
  • 1
  • 1