Playing a different segments in a video - c#

i want to develop a video player which has to play different parts in a video (specified by their start n end positions). I am using directx.audiovideoplayback dll for this purpose.
the starting and ending positions are stored in an array.
eg- {2,6,8,19} tells that segment between 2nd to 6th second has to be played and then 8th to 19th second should be played.
my problem is that despite me giving condition that
if(video_obj.CurrentPosition==endtime) video_obj.Stop();
the video isnt stopping..
the video is playing from 2nd position to end of file.
code is
public static int[] seconds = { 3,8,12,16};
public static int start, end;
private void btnPlay_Click(object sender, EventArgs e)
{
vdo.Owner = panel1;
panel1.Width = 300;
panel1.Height = 150;
vdoTrackBar.Minimum = 0;
vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);
if (vdo != null)
{
vdo.Play();
timer1.Start();
}
}
private void vdoTrackBar_Scroll(object sender, EventArgs e)
{
if (vdo != null)
{
vdo.CurrentPosition = vdoTrackBar.Value;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int i;
for (i = 0; i < seconds.Length; i = i + 2)
{
start = seconds[i];
vdo.CurrentPosition = start;
end = seconds[i + 1];
vdoTrackBar.Value = (int)vdo.CurrentPosition;
MessageBox.Show("Starts at " + vdo.CurrentPosition + " and Ends at " + end);
if (vdo.Paused)
vdo.Play();
if (vdo.Playing)
{
if (vdo.CurrentPosition == vdo.Duration)
{
timer1.Stop();
vdo.Pause();
vdoTrackBar.Value = 0;
}
if (vdo.CurrentPosition == end)
{
timer1.Stop();
vdo.Pause();
}
vdoTrackBar.Value += 1;
}
}
Help! Somewhere something is wrong and i have no clue about it
How do i correct it?
Video starts playing when i

So there still isn't enough info in the post to really definitively say whats going wrong. I'm guessing that your timer isn't ticking with enough resolution to happen to tick exactly when the for loop in the timer tick would coincide with the video's current position.
How often does the timer tick? What's the precision of the video's current position?

Related

Simulation keyboard events in minecraft with an auto-keyboard

So I'm playing minecraft and I want to make a program that switches the tool on a set time frame, meaning it switches to the second item slot in the toolbar and then the third and so on. If you're curious I'm playing skyblock and have a cobblestone generator. It works perfectly in notepad and other "textboxes" like chrome with the timing and everything but it seems that minecraft does not allow simulated keyboard presses because it does not respond to the input given by the program. Even if i have the game in full screen (f11) it does not seem to work. Do you think the program itself is faulty or do I need to adapt it for minecraft specifically? Any tips at all would be very appreciated.
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
timer1.Start();
if (textBox1.Text == "")
{
int interval = 1; // set to 1 second for testing purposes. This is 200 in practise.
timer1.Interval = interval * 1000;
}
else
{
int interval = int.Parse(textBox1.Text);
timer1.Interval = 1000 * interval;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (i > 9)
{
this.Close();
}
i++;
keyboardPress(i);
}
private void keyboardPress(int i)
{
SendKeys.Send(Convert.ToString(i));
}

Stop and repeat process by using a timer in C# Windows Form

I am trying to use a timer to achieve a sort of old animation used in the past to show that a process is running.
The way I would like to do that is by adding dots to a sentence (in a label control), for example:
"Process is running." to "Process is running.." and "Process is running..." with a limit of 3 dots and then revert back to a single dot.
I am not sure as to the fact using a timer here would be the best choice, but I thought it should work fine for such a simple example.
The code I used is as follows:
public string InitialProcessText;
private void StartBtn_Click(object sender, EventArgs e)
{
if(fileName != "No file selected")
{
ValidationLbl.Text = null;
ProcessLbl.Text = "Application is now running.";
//InitialProcessText = ProcessLbl.Text;
ProcessTimer.Start();
}
else
{
ValidationLbl.Text = "No file was added";
}
}
private void StopBtn_Click(object sender, EventArgs e)
{
ProcessTimer.Stop();
}
private void ProcessTimer_Tick(object sender, EventArgs e)
{
_ticks++;
//For every two ticks, ProcessLbl.Text = InitialProcessText
ProcessLbl.Text += ".";
}
What could I add to set a limit of adding 2 dots and then remove the dots and add dots again (I would assume to do this in the ProcessTimer_Tick method)?
You can just use your _ticks variable:
private readonly int _ticksPerUpdate = 2;
private readonly int _maxNumberOfDots = 3;
private void ProcessTimer_Tick(object sender, EventArgs e)
{
_ticks++;
if(_ticks == (_ticksPerUpdate * (_maxNumberOfDots + 1)))
{
_ticks = 0;
ProcessLbl.Text = InitialProcessText;
}
else if(_ticks % _ticksPerUpdate == 0)
{
ProcessLbl.Text += ".";
}
}
Remember to reset the ticks counter every time you start the timer:
private void StartBtn_Click(object sender, EventArgs e)
{
if(fileName != "No file selected")
{
ValidationLbl.Text = null;
ProcessLbl.Text = "Application is now running.";
InitialProcessText = ProcessLbl.Text;
// reset the variable
_ticks = 0
ProcessTimer.Start();
}
else
{
ValidationLbl.Text = "No file was added";
}
}
I assume that _ticks counts the number of ticks. You could then go :
if(ticks%3 == 0)
{
ProcessLbl.Text = "Application is now running."
}
else
{
ProcessLbl.Text+=".";
}
Then, at 1st tick, 1%3=1 so it adds a dot, at 2nd tick, 2%3=2 so it adds a dot and 3rd tick, 3%3=0, so it gets back to original.
Just because...here's another approach:
private void ProcessTimer_Tick(object sender, EventArgs e)
{
ProcessLbl.Text = ProcessLbl.Text.EndsWith("...") ? ProcessLbl.Text.TrimEnd(".".ToCharArray()) + "." : ProcessLbl.Text + ".";
}

C# Image rotation using timer

I am beginner C# user using WFA.Below is my code to get images to rotate every 4 ticks. please could I have some assistance to what I might have done incorrect.
private void timer1_Tick(object sender, EventArgs e)
{
int time = 0;
int pic = 0;
{
if (time >= 4)
pic++;
this.picBox1.Image = ImageList.Images[pic];
pic++;
if (time == 4 || time == 8 || time == 12 || time == 16)
this.lblCompany.Visible = true;
this.lsbHistory.ValueMember = ("User Clicks on at ___+ DateTime.Now.ToshortDateString");
every time your tick event gets called you set time to 0, that means it is lower then 4. you should take both time and pic values out of the event and reset time to 0 after.
I'd reccomend reading this https://msdn.microsoft.com/en-us/library/5557y8b4.aspx
on how to use breakpoints to have a beter look at what's happening in your code
private int time = 0;
private int pic = 0;
private void timer1_Tick(object sender, EventArgs e)
{
time++;
if (time >= 4)
{
pic++;
time = 0;
this.picBox1.Image = ImageList.Images[pic];
//other logic you might have

How to reset a picture boxes position back to the left side of the form?

I'm creating a basic racing game in C# for an assignment where two picture boxes race from the left side of the form to the right side. What I am struggling with is resetting the position back to the 1st pixel on the left side of the form once the picture box has reached the end of the form on the right side. At the moment the picture boxes just keep going right and then disappear from the form and never come back.
That is what the layout of the game looks like:
I've tried searching google for snippets of code or even examples on how I might achieve this and have yet to find anything.
Any help would be much appreciated.
public partial class frmRacing : Form
{
public frmRacing()
{
InitializeComponent();
}
//This is the segment of intergers and the randomizer.
Random r = new Random();
int dir = 1;
int min, sec, ms = 0;
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
//This is the timer for "Player One". It moves the players picture box across the form at a random speed between 1-10 and times how long it takes to complete the total laps.
private void tmrOne_Tick(object sender, EventArgs e)
{
dir = r.Next(1, 10);
picStark.Left += dir;
lblTimer1.Text = min + ":" + sec + ":" + ms.ToString();
ms++;
if (ms > 100)
{
sec++;
ms = 0;
}
else
{
ms++;
}
if (sec > 60)
{
min++;
sec = 0;
}
}
//This is the timer for "Player Two". It moves the players picture box across the form at a random speed between 1-10 and times how long it takes to complete the total laps.
private void tmrTwo_Tick(object sender, EventArgs e)
{
dir = r.Next(1, 10);
picLannister.Left += dir;
lblTimer2.Text = min + ":" + sec + ":" + ms.ToString();
ms++;
if (ms > 100)
{
sec++;
ms = 0;
}
else
{
ms++;
}
if (sec > 60)
{
min++;
sec = 0;
}
}
//This is the start button. It enables all the timers and starts the race.
private void btnStart_Click(object sender, EventArgs e)
{
tmrOne.Enabled = true;
tmrTwo.Enabled = true;
tmrThree.Enabled = true;
}
private void hsbLaps_Scroll(object sender, ScrollEventArgs e)
{
lblLaps3.Text = lblLaps3.Text + 1;
}
//This is the overall timer for the race.
private void tmrThree_Tick(object sender, EventArgs e)
{
lblTimer3.Text = min + ":" + sec + ":" + ms.ToString();
ms++;
if (ms > 100)
{
sec++;
ms = 0;
}
else
{
ms++;
}
if (sec > 60)
{
min++;
sec = 0;
}
}
}
You could create a method which you call at the end of your tick events which checks if the Left property is greater than the width of your form.
private void ResetPicture(PictureBox pb)
{
// check if picture box left property is greater than the width
// of your form - the width of your picturebox
if (pb.Left >= this.Width - pb.Width)
{
// the picture has won the game, reset it
pb.Left = 1;
}
}

C# timers for moving objects

I have 4 dogs who're racing, I need to move them across the form but they don't move gradually, they start out at the starting line and immediately teleport to the finish line, without moving in between. With every timer tick, their location.X is incremented.
Do I need one timer or 4? I currently have one, and its interval is set to 400.
This is the relevant code:
private void btnRace_Click(object sender, EventArgs e)
{
btnBet.Enabled = false;
timer1.Stop();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{ while (!isWon)
{
for (i = 0; i < Dogs.Length; i++) // there are four dogs
{
if (Dogs[i].Run()) // Run() returns true if full racetrack is covered by this dog
{
Winner = i + 1;
isWon = true;
MessageBox.Show("We have a winner! Dog #" + Winner);
break;
}
}
}
And in the Dog class:
public bool Run()
{
Distance = 10 + Randomizer.Next(1, 4);
p = this.myPictureBox.Location;
p.X += Distance ;
this.myPictureBox.Location = p;
//return true if I won the game
if (p.X >= raceTrackLength)
{
return true ;
}
else
{
return false ;
}
}
The dogs only appear to move one step and then immediately show up on the finish line. What am I doing wrong?
Remove While loop from timer1_Tick method.
This method runs every 400 ms, but in your case at first launch it waits until one dog wins.
Also you should stop the timer after one of dogs win.
private void timer1_Tick(object sender, EventArgs e)
{
for (i = 0; i < Dogs.Length; i++) // there are four dogs
{
if (Dogs[i].Run()) // Run() returns true if full racetrack is covered by this dog
{
Winner = i + 1;
isWon = true;
timer1.Stop();
MessageBox.Show("We have a winner! Dog #" + Winner);
break;
}
}
}
Your timer goes off just once and is stuck in this loop;
while (!isWon)
{
}
Remove the loop and let the Timer do it's work
Add at the end
if (isWon) timer1.Stop();

Categories