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:
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:
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.
Updated 2:
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;
}
}
}
}
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.
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;