How to make Trackbar works while media is playing - c#

I am working on a simple mediaplayer application. It works great but I want to add some extra features. I have added a trackbar control.How can i set trackbar length the same as the music's length ?
Like if the song is halfways the trackbars halfways.This is what I have so far
string[] files, indexed_files;
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK) {
files = ofd.SafeFileNames;
indexed_files = ofd.FileNames;
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(files[i]);
}
}
button4.Enabled = true;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = indexed_files[listBox1.SelectedIndex];
progressBar1.Maximum =(int) axWindowsMediaPlayer1.currentMedia.duration;
axWindowsMediaPlayer1.PlayStateChange += axWindowsMediaPlayer1_PlayStateChange;
}
void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
trackBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count != 0) {
axWindowsMediaPlayer1.URL = indexed_files[index];
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
index++;
index = (index % listBox1.Items.Count);
}
}

This will bring you the desired outcome.In my example i just placed the url in the form load for demonstration purposes.The openstatechanged event its to set the trackbar maximum since you need to wait for the player to load the file,after that the code its pretty self-explanatory:
public partial class Form1 : Form
{
Timer t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "YourUrlHere";
t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler(t_Tick);
}
void t_Tick(object sender, EventArgs e)
{
trackBar1.Value = (int)this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
private void axWindowsMediaPlayer1_OpenStateChange(object sender, AxWMPLib._WMPOCXEvents_OpenStateChangeEvent e)
{
if (axWindowsMediaPlayer1.openState == WMPLib.WMPOpenState.wmposMediaOpen)
{
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
t.Start();
}
}
}
Yes its a timer:),and probably it is best to set it bellow 1000 for reasons of delay.

So you should now add a timer and insert the following code in timer Tick event handler:
trackbar.Value = this.axWindowsMediaPlayer1.ctlControls.CurrentPosition;

Related

How can I change the current file to another in Windows Media Player?

I am using Visual Studio .Net Framework to create a slideshow that operates on its own after clicking the "play" button. My current issue is that I do not know how to go about making the application go to the next file in the document after the video has ended. At the moment I have it setup for the user to select a time period for how long they want each image displayed, but I would rather have this feature automated.
This is my current code :
{
public partial class TVDisplay : Form
{
// Time f3 = new Time();
public TVDisplay()
{
//f3.Show();
InitializeComponent();
//both listview and listbox clear at initilize//
listBox1.Items.Clear();
listView1.Items.Clear();
//xml file from first application is read for department information, and that path and department are placed in textbox3 to be read by the listbox//
var xd = new XmlDocument();
string pathz = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filepath = pathz + #"\Assigned_Television.xml";
xd.Load(filepath);
// var list = xd.GetElementsByTagName("Television");
textBox3.Text = xd.GetElementsByTagName("Path")[0].InnerText;
}
protected override bool ProcessDialogKey(Keys keyData)
{
//when esacpe is pressed, the display screen is windowed instead of fullscreen//
if (Form.ModifierKeys == Keys.None && keyData == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
return true;
}
return base.ProcessDialogKey(keyData);
}
//Page load//
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.stretchToFit = true;
axWindowsMediaPlayer1.uiMode = "none"; //disables the play, pause, etc. buttons at the bottom of the windows media player//
listView1.Items.Clear(); //clears listview items//
Repopulate(); //calls repopulate function//
refreshtimer.Interval = 5 * 60 * 1000;
//timer for when images need to be displayed//
Timer tmr = new Timer();
int result = int.Parse(textBox2.Text);
tmr.Interval = result * 60 * 1001;
tmr.Tick += new EventHandler(timer1_Tick);
tmr.Start();
}
private void Repopulate()
{
foreach (var d in System.IO.Directory.GetFiles(textBox3.Text))
{
var dirName = new DirectoryInfo(d).Name;
listView1.Items.Add(dirName);
}
foreach (ListViewItem item in listView1.Items)
{
item.Selected = true;
listBox1.Items.Add(item.Text);
}
}
//hidden button that acts as a "play" button, taking url from textbox 1, playing the video, and looping it until it is time for the next image//
private void button1_Click(object sender, EventArgs e)
{
// MessageBox.Show(textBox4.Text);
axWindowsMediaPlayer1.URL = textBox4.Text;
axWindowsMediaPlayer1.Ctlcontrols.play();
axWindowsMediaPlayer1.settings.setMode("loop", true);
}
// timer that clicks button one//
private void timer1_Tick(object sender, EventArgs e)
{
button1.PerformClick();
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
}
//when the tvdisplay is shown, start button is clicked and button 1 (play button) is clicked//
private void TVDisplay_Shown(object sender, EventArgs e)
{
startbutton.PerformClick();
button1.PerformClick();
}
//hidden behind windows media player//
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Timer tmr = new Timer();
int result = int.Parse(textBox2.Text);
tmr.Interval = result * 60 * 1000;
//int result = int.Parse(textBox3.Text);
// tmr.Interval = 1 * 60 * 1000;
//tmr.Interval = 180000;
tmr.Tick += new EventHandler(timer2_Tick);
tmr.Start();
//textBox2.Clear();
var path = textBox3.Text;
textBox4.Text = path + listBox1.SelectedItem.ToString();
}
private void refreshtimer_Tick(object sender, EventArgs e)
{
listBox1.BeginUpdate();
int x = listBox1.SelectedIndex;
listBox1.Items.Clear();
listView1.Items.Clear();
Repopulate();
//selects the first index once files are repopulated//
listBox1.SelectedIndex = x;
listBox1.EndUpdate();
}
//hidden behind windows media display//
private void startbutton_Click(object sender, EventArgs e)
{
startbutton.Enabled = false;
timer3.Interval = 10000; //time in milliseconds
timer3.Tick += timer3_Tick;
timer3.Start();
//startbutton.Enabled = false;
if(listBox1.Items != null)
{
MessageBox.Show("There is no content entered for display.", "Error");
Application.Exit();
}
else {
listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % listBox1.Items.Count;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
startbutton.PerformClick();
}
private void timer3_Tick(object sender, EventArgs e)
{
startbutton.Enabled = true;
timer3.Stop();
}
}
}
Image/Video is displayed by Windows Media Player.

After a video file ends, how would I get the next video to play without setting a timer?

I have put together a list of files that I want played from a specific directory. The videos/pictures are displayed on Windows Media Player. After the end of one video, I want the play button to be pressed to play the next video. Currently, my code is set up to have a button pressed to go to the next file in the list, as well as a play button being pressed to display the image (buttons are pressed using a timer, but this is inconvenient because some videos do not get played for the full time). Video URLS are pulled from textboxes that are filled with xml or file path information.
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.stretchToFit = true;
axWindowsMediaPlayer1.uiMode = "none"; //disables the play, pause, etc. buttons at the bottom of the windows media player//
listView1.Items.Clear(); //clears listview items//
Repopulate(); //calls repopulate function//
refreshtimer.Interval = 5 * 60 * 1000;
//timer for when images need to be displayed//
Timer tmr = new Timer();
int result = int.Parse(textBox2.Text);
tmr.Interval = result * 60 * 1001;
tmr.Tick += new EventHandler(timer1_Tick);
tmr.Start();
}
private void Repopulate()
{
foreach (var d in System.IO.Directory.GetFiles(textBox3.Text))
{
var dirName = new DirectoryInfo(d).Name;
listView1.Items.Add(dirName);
}
foreach (ListViewItem item in listView1.Items)
{
item.Selected = true;
listBox1.Items.Add(item.Text);
}
}
//hidden button that acts as a "play" button, taking url from textbox 1, playing the video, and looping it until it is time for the next image//
private void button1_Click(object sender, EventArgs e)
{
// MessageBox.Show(textBox4.Text);
axWindowsMediaPlayer1.URL = textBox4.Text;
axWindowsMediaPlayer1.Ctlcontrols.play();
axWindowsMediaPlayer1.settings.setMode("loop", true);
}
// timer that clicks button one//
private void timer1_Tick(object sender, EventArgs e)
{
button1.PerformClick();
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
}
//when the tvdisplay is shown, start button is clicked and button 1 (play button) is clicked//
private void TVDisplay_Shown(object sender, EventArgs e)
{
startbutton.PerformClick();
button1.PerformClick();
}
//hidden behind windows media player//
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Timer tmr = new Timer();
int result = int.Parse(textBox2.Text);
tmr.Interval = result * 60 * 1000;
//int result = int.Parse(textBox3.Text);
// tmr.Interval = 1 * 60 * 1000;
//tmr.Interval = 180000;
tmr.Tick += new EventHandler(timer2_Tick);
tmr.Start();
//textBox2.Clear();
var path = textBox3.Text;
textBox4.Text = path + listBox1.SelectedItem.ToString();
}
private void refreshtimer_Tick(object sender, EventArgs e)
{
listBox1.BeginUpdate();
int x = listBox1.SelectedIndex;
listBox1.Items.Clear();
listView1.Items.Clear();
Repopulate();
//selects the first index once files are repopulated//
listBox1.SelectedIndex = x;
listBox1.EndUpdate();
}
//hidden behind windows media display//
private void startbutton_Click(object sender, EventArgs e)
{
startbutton.Enabled = false;
timer3.Interval = 10000; //time in milliseconds
timer3.Tick += timer3_Tick;
timer3.Start();
//startbutton.Enabled = false;
if(listBox1.Items != null)
{
MessageBox.Show("There is no content entered for display.", "Error");
Application.Exit();
}
else {
listBox1.SelectedIndex = (listBox1.SelectedIndex + 1) % listBox1.Items.Count;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
startbutton.PerformClick();
}
private void timer3_Tick(object sender, EventArgs e)
{
startbutton.Enabled = true;
timer3.Stop();
}
}
If a timer is the most practical element to use, what would be a better way to implement it?

so I was trying to make a code that changes the writings font(arial,times new roman etc.) in the textbox every 750 miliseconds but I couldn't do it

private void OnTimer(object sender, EventArgs e)
{
int i = 0;
i += 1;
}
I tried to make the timer
private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
FontFamily[] fontlar = FontFamily.Families;
fontlar.GetUpperBound(i);
Timer timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
I put the code in form1 but I tried it in textbox as well it didn't work
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
button
I guess you are new to this.
Your Timer does nothing
Your Timer is local, it should be public
You didn't create your Textbox
I think this would be your answer:
Timer timer1;
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
private void OnTimer(object sender, EventArgs e)
{
i += 1;
FontFamily[] fontlar = FontFamily.Families;
//fontlar.GetUpperBound(i);
textBox1.Font = new Font(fontlar[i], 16.0f);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
Don't forget to create a textbox on your form.

Why WinForm Gets Stuck with BackgroundWorker?

hi guys i tried to copy some files with this Code everything is good and the app will copy files but in copy progress i cant move my app or do anything
i tried to use thread but its not works i also use backgroundWorker but still nothing the only control that doesnt get stuck is progressBar its works fine here is my code :
public Form1()
{
InitializeComponent();
backgroundWorker1.Dispose();
backgroundWorker1.DoWork += BackgroundWorker_DoWork;
backgroundWorker1.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
backgroundWorker1.ProgressChanged += BackgroundWorker_ProgressChanged;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker2.DoWork += BackgroundWorker2_DoWork;
backgroundWorker2.WorkerReportsProgress = true;
}
private void BackgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
File.Copy(sourcePath, targetPath);
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < fileSize; i++)
{
int p = (i + 1) * 100 / Convert.ToInt32(fileSize);
backgroundWorker1.ReportProgress(p);
}
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lbProgress.Text = e.ProgressPercentage.ToString();
progressBar1.Value = e.ProgressPercentage;
}
private void btnTarget_Click(object sender, EventArgs e)
{
folderBrowser = new FolderBrowserDialog();
if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
targetPath += folderBrowser.SelectedPath + #"\" + fileName;
lbTarget.Text = targetPath;
}
}
private void btnSource_Click(object sender, EventArgs e)
{
op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
{
sourcePath += op.FileName;
lbSource.Text = sourcePath;
fileInfo = new FileInfo(sourcePath);
fileSize = fileInfo.Length / 1024;
fileName = fileInfo.Name;
MessageBox.Show(string.Format("File size is: {0} KB", fileSize));
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
backgroundWorker2.RunWorkerAsync();
}
You're updating the progress bar faster than the UI can update, for every single byte of the file being copied in a tight loop. You're flooding the UI thread with pointless work.
Remove backgroundWorker1, it's not doing anything useful anyway. If you don't have a way to track the progress (which you don't with File.Copy), just use a progress bar without progress (set Style to Marquee).
For testing I created a simple winform application with a button, a label and a background worker and added the following corresponding events:
private void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
for (int i = 0; i < 10; i++)
{
Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
private void OnBackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
labelProgress.Text = e.ProgressPercentage.ToString();
}
private void OnBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
labelProgress.Text = "Done";
}
private void OnButtonProgressClick(object sender, System.EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
Works as expected.
Could You try to update Your DoWork to this:
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int mainProgress = 0;
for (int i = 0; i < fileSize; i++)
{
int calculatedProgress = (i + 1) * 100 / Convert.ToInt32(fileSize);
if(calculatedProgress > mainProgress)
{
mainProgress = calculatedProgress;
backgroundWorker1.ReportProgress(mainProgress);
}
}
}
maybe You are doing so many updates that simply Window Thread is all the time updating only progress and don't have a time to make anything else?

How to run a progress bar on a timer - c#

I want to run a progress bar on a form through the use of a timer.
I have tried multiple ways and have not been able to get it to work.
I hope someone here can help me with this.
private void SplashScreen_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 1000;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
you are assigning event_handler like
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
and you are changing the progressBar value in
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
change event handler to
splashScreenTimer.Tick += new EventHandler(timer_Tick);
or move codes to the other event handler timer1_Tick which should be in your form
For running the progressBar full in 4 seconds you can do like this
private void Form1_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 30;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer_Tick);
}
int waitingTime = 0;
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value < 100)
{
progressBar.Value++;
}
else
{
if (waitingTime++ > 35)
this.Close();
}
}

Categories