Note: This application will be designed for a touch device (MS Surface Hub)
My Windows Form contains axWindowsMediaPlayer component. I have created a playlist and I am able to loop the media files in the playlist. However, I want my axWindowsMediaPlayer playlist to pause after 5 sec (time limit just for testing/debugging purpose) of inactivity (No input from the user to be more precise) and display a dialog-box asking me whether I wish to continue.
Following is my code to set a timer_Tick event:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TimerDemo
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern Boolean GetLastInputInfo(ref tagLASTINPUTINFO plii);
public struct tagLASTINPUTINFO
{
public uint cbSize;
public Int32 dwTime;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlenabled = true;
var pl = axWindowsMediaPlayer1.playlistCollection.newPlaylist("MyPlaylist");
pl.appendItem(axWindowsMediaPlayer1.newMedia(#"C:\ABC\abc1.mp4"));
pl.appendItem(axWindowsMediaPlayer1.newMedia(#"C:\ABC\abc2.mp4"));
axWindowsMediaPlayer1.currentPlaylist = pl;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8) //Media Ended
{
}
}
private void timer1_Tick(object sender, EventArgs e)
{
tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
Int32 IdleTime;
LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
LastInput.dwTime = 0;
if (GetLastInputInfo(ref LastInput))
{
IdleTime = System.Environment.TickCount - LastInput.dwTime;
if (IdleTime > 5000)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
timer1.Stop();
MessageBox.Show("Do you wish to continue?");
}
else
{
}
timer1.Start();
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
}
}
With this code, the application is not entering the timer1_Tick Event.
Queries:
Does e.newState == 3 (Playing State) in axWindowsMediaPlayer considered as input?
How do I ensure that the application enters into timer1_Tick event?
If I remove the axWindowsMediaPlayer part of the code then the timer1_Tick event is responding.
For your application to get into the timer_Tick event, you first need to start the timer.
Replace the following code:
public Form1()
{
InitializeComponent();
}
With the Following:
public Form1()
{
InitializeComponent();
timer1.Start();
}
This should work fine for you.
Related
I am trying to write a code for taking fps at label while taking view from the camera.
So, I used task for that and I used timer but I got an error. I fixed the problem with using CheckForIllegalCrossThreadCalls = false; but as far as I know it is not an effective way.
I want to fix this with using invoke. How should I write the code properly?
Here is the code:
using System;
using System.Drawing;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace projectimg
{
public partial class Form1 : Form
{
Stopwatch watch = new Stopwatch();
System.Timers.Timer timerFPS;
public Form1()
{
InitializeComponent();
}
CameraConnection connnection;
private bool btn_GetviewWasClicked = false;
private void Form1_Load(object sender, EventArgs e)
{
btn_Getview.Click += Btn_Getview_Click;
timerFPS = new System.Timers.Timer(1000);
timerFPS.Elapsed += TimerFPS_Elapsed;
timerFPS.Start();
}
private void Btn_Getview_Click(object sender, EventArgs e)
{
btn_GetviewWasClicked = true;
Task.Run(() =>
{
connnection = new CameraConnection();
connnection.Connect();
});
CameraConnection.DataReceived += CameraConnection_DataReceived;
}
private void TimerFPS_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
txt_fps.Text = Convert.ToString(CameraConnection.count);
CameraConnection.count = 0;
}
}
}
I have used the following code to create and loop my videos in my playlist:
private void Form1_Load(object sender, EventArgs e)
{
var pl = axWindowsMediaPlayer1.playlistCollection.newPlaylist("plList");
pl.appendItem(axWindowsMediaPlayer1.newMedia(#"C:\ABC\abc.mp4"));
pl.appendItem(axWindowsMediaPlayer1.newMedia(#"C:\XYZ\xyz.mp4"));
axWindowsMediaPlayer1.currentPlaylist = pl;
//axWindowsMediaPlayer1.settings.setMode("loop", true);
axWindowsMediaPlayer1.Ctlcontrols.play();
}
However, I found out that the next item in the playlist plays for 2 seconds before the end of current item in the playlist. Then, the new item plays all over again (i.e. the first 2 seconds of the item plays twice, once before the end of current item and once at the beginning of the next item).
How do I ensure that the next item is loaded only after the current item is complete playing?
Requirement 2: How do I detect the end of Playlist (i.e. After playing of all videos)
I tried to capture it in PlayStateChange Event, but I am unable to capture it in either in Media Ended or Stopped or Last states
To perfectly loop your videos, use the following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern Boolean GetLastInputInfo(ref tagLASTINPUTINFO plii);
public struct tagLASTINPUTINFO
{
public uint cbSize;
public Int32 dwTime;
}
public Form1()
{
InitializeComponent();
timer1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlenabled = true;
var pl = axWindowsMediaPlayer1.playlistCollection.newPlaylist("XYZ");
p1.appendItem(axWindowsMediaPlayer1.newMedia(#"C:\Folder\file1.mp4"));
pl.appendItem(axWindowsMediaPlayer1.newMedia(#"C:\Folder\file2.mp4"));
axWindowsMediaPlayer1.currentPlaylist = pl;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void timer1_Tick(object sender, EventArgs e)
{
tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
Int32 IdleTime;
LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
LastInput.dwTime = 0;
if (GetLastInputInfo(ref LastInput))
{
IdleTime = System.Environment.TickCount - LastInput.dwTime;
if (IdleTime > 10000)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
timer1.Stop();
MessageBox.Show("Do you wish to continue?");
}
timer1.Start();
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
}
}
I use fallowing library to detect webcam for simple c# application. When it debug works fine and when I try again It give fallowing error.
Used reference
If somebody has any idea about it please help me to solve it.
First get this box and when click OK get fallowing error message.
an error occurred while capturing the video image. the video capture
will now be terminated. Object reference not set to an instance of an
object.
Form1.cs
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using WinFormCharpWebCam;
namespace camTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WebCam webcam;
private void button1_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void button2_Click(object sender, EventArgs e)
{
webcam.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
webcam.Continue();
}
private void Form1_Load(object sender, EventArgs e)
{
webcam = new WebCam();
webcam.InitializeWebCam(ref imgVideo);
}
}
}
Webcam.cs
using System;
using System.IO;
using System.Linq;
using System.Text;
using WebCam_Capture;
using System.Collections.Generic;
namespace WinFormCharpWebCam
{
//Design by Pongsakorn Poosankam
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Forms.PictureBox _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}
public void Stop()
{
webcam.Stop();
}
public void Continue()
{
// change the capture time frame
webcam.TimeToCapture_milliseconds = FrameNumber;
// resume the video capture from the stop
webcam.Start(this.webcam.FrameNumber);
}
public void ResolutionSetting()
{
webcam.Config();
}
public void AdvanceSetting()
{
webcam.Config2();
}
}
}
I've recently made this small Windows Forms Application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Spammer
{
public partial class Form1 : Form
{
Thread t1;
int delay, y = 1;
public Form1()
{
t1 = new Thread(send);
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
t1.Start();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
y = 0;
}
private void send()
{
while (y == 1)
{
String textt = textBox1.Text;
Thread.Sleep(delay);
SendKeys.SendWait(textt);
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
Now, it looks like this:
It has a Delay textbox, a Text to Send textbox, and 2 buttons: Start and Stop.
Now, I've tried running it and setting the delay to 1000ms.
When I press the "Stop" button, it perfectly stops and no more messages are sent.
But when I input in the delay very small delays like 100ms, for example, pressing "Stop" doesn't do anything.
It's even kind of hard to click it, and even when I click it doesn't stop sending the messages.
Why is this? And can I solve it somehow?
OK, Thank you, everyone, for helping!
I'm now using the GUI timer and it works flawlessly.
This one is giving me a hard time.
The thing is that I have a code that plays some notes in MIDI, and I wanted to be able to pause it, so I made a simple Form like this:
namespace Music
{
public partial class Form1 : Form
{
static BackgroundWorker _bw = new BackgroundWorker
{
WorkerSupportsCancellation = true
};
private void button1_Click(object sender, EventArgs e)
{
if (!Playing)
{
Playing = true;
_bw.DoWork += Start_Playing;
_bw.RunWorkerAsync("Hello to worker");
}
else
{
Playing = false;
_bw.CancelAsync();
}
}
static void Start_Playing(object sender, DoWorkEventArgs e)
{
//Plays some music
}
}
}
And when I click it starts playing, but no matter what I do, it can't stop. But the thing is that if I do the same thing in the console it works perfect.
Did I miss something?
How can I control a separate thread from the form?
This seems to work...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private BackgroundWorker _bw = new BackgroundWorker { WorkerSupportsCancellation = true,
WorkerReportsProgress = true};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_bw.IsBusy)
{
_bw.CancelAsync();
}
else
{
_bw.ProgressChanged += new ProgressChangedEventHandler(_bw_ProgressChanged);
_bw.DoWork += new DoWorkEventHandler(_bw_DoWork);
_bw.RunWorkerAsync();
}
}
void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
textBox1.Text += (string)e.UserState;
}
void _bw_DoWork(object sender, DoWorkEventArgs e)
{
int count = 0;
while (!_bw.CancellationPending)
{
_bw.ReportProgress(0, string.Format("worker working {0}", count));
++count;
Thread.Sleep(2000);
}
}
}
}