Playing two video with axWindowsMediaPlayer - c#

I try to play jpg (in loop), after click mp4 should be played after end, that jpg should play again. I dont know why but after I play in axWindowsMediaPlayer1_PlayStateChange vido play and then stop. Help.
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;
namespace Video
{
public partial class Form1 : Form
{
bool clicked = false;
public Form1()
{
InitializeComponent();
axWindowsMediaPlayer1.URL = "wait2.JPG";
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded & clicked== true)
{
clicked = false;
axWindowsMediaPlayer1.settings.setMode("Loop", true);
axWindowsMediaPlayer1.URL = "wait2.JPG";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
}
private void axWindowsMediaPlayer1_ClickEvent(object sender, AxWMPLib._WMPOCXEvents_ClickEvent e)
{
axWindowsMediaPlayer1.settings.setMode("Loop", false);
axWindowsMediaPlayer1.URL = "video.MP4";
axWindowsMediaPlayer1.Ctlcontrols.play();
clicked = true;
}
}
}

I wish someone had replied to this question the time it was posted. It took me a lot of time to figure out why I was not able to start a new video by setting the URL property. I finally found the answer to this issue here:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd562470%28v=vs.85%29.aspx
The problem is with setting the URL property from within the axWindowsMediaPlayer1_PlayStateChange() event handler. According to the above msdn document:
"Do not call this method from event handler code. Calling URL from an event handler may yield unexpected results."
So the URL property has to be set outside of the even handler. I also tried Dispatcher.Invoke() and even starting a new thread from within the event handler to set the URL property; but that too did not help. It really has to come from outside of the event handler!

Related

C# OpenWebKitSharp .NET 4 - How to call javascript

I am trying to call javascript using OpenWebKitSharp from WinForms with .NET 4
Here is the code I am trying to use.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WebKit;
using WebKit.Interop;
using WebKit.JSCore;
using webkitForm.Properties;
namespace webkitForm
{
public partial class Form1 : Form
{
WebKitBrowser webKitSharpBrowser = new WebKitBrowser();
public Form1()
{
InitializeComponent();
this.Controls.Add(webKitSharpBrowser);
webKitSharpBrowser.Width = 600;
webKitSharpBrowser.Height = 400;
}
private void button1_Click(object sender, EventArgs e)
{
webKitSharpBrowser.Preferences.AllowPlugins = true;
webKitSharpBrowser.UseJavaScript = true;
webKitSharpBrowser.Navigate("http://sandbox.icontact.com");
webKitSharpBrowser.GetScriptManager.EvaluateScript("alert('An alert from C#!');"); //Call javascript?
}
}
}
I can't get javascript to fire for anything... there must be something that I am missing.
Thanks in advance.
Well, it seems like it can't be done the way you want to:
if you are using .NET 4, calling a function is possible by using:
<webkitbrowser>.GetScriptManager.CallFunction("name", new Object[] { arg1, arg2, ...});
If you want to use .NET 2 you can use:
<webkitbrowser>.StringByEvaluatingJavaScriptFromString("name(arguments)")
- Open Webkit Sharp Issues
I tested your code, it IS working, but calling the alert function only triggers an event (WebKitBrowser.ShowJavaScriptAlertPanel), you are responsible for handling that event and showing a message or updating a label, or anything else.
For example:
Browser.ShowJavaScriptAlertPanel += Browser_ShowJavaScriptAlertPanel;
and then handle the event:
private void Browser_ShowJavaScriptAlertPanel(object sender, WebKit.ShowJavaScriptAlertPanelEventArgs e)
{
MessageBox.Show(e.Message);
}

Im trying to use backgroundworker i want to see the upload progress but it dosent work why?

In the designer i put backgroundworker and i have two events: Do Work and Progress Changed.
I used breakpoint and its getting inside the Do Work event but it never get into the Progress Changed event. Its never stop there like the event isnt working. Why the progrss changed event isnt working ?
This is the code:
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 Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.YouTube;
using System.Threading;
namespace YoutubeTesting
{
public partial class Form1 : Form
{
YouTubeRequestSettings settings;
YouTubeRequest request;
string devkey = "AI39si6xhSQXx95FTYIACWPfq-lLIphblgaReuz9z6VEjR1Q6YjrV6FRN2U6FN6P6-lGF2OYaUZhCVOKJ_MCk4o6kPeUszvf5A";
string username = "chocolade13091972#gmail.com";
string password = "password";
public Form1()
{
InitializeComponent();
worker.RunWorkerAsync();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void upload()
{
try
{
settings = new YouTubeRequestSettings("You Manager", devkey, username, password);
settings.Timeout = -1;
request = new YouTubeRequest(settings);
Video video = new Video();
video.Title = "test";
video.Tags.Add(new MediaCategory("Comedy", YouTubeNameTable.CategorySchema));
video.Keywords = "Comedy";
video.Private = false;
video.MediaSource = new MediaFileSource("d:\\VIDEO0037.3gp", "video/3gp");
request.Upload(video);
MessageBox.Show("Successfully Uploaded");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
upload();
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
textBox1.Text = e.ProgressPercentage.ToString();
}
}
}
You need to report the progress using worker.ReportProgress()
From MSDN:
If you need the background operation to report on its progress, you
can call the ReportProgress method to raise the ProgressChanged event.
The WorkerReportsProgress property value must be true, or
ReportProgress will throw an InvalidOperationException.
It is up to you to implement a meaningful way of measuring your
background operation's progress as a percentage of the total task
completed.
The call to the ReportProgress method is asynchronous and returns
immediately. The ProgressChanged event handler executes on the thread
that created the BackgroundWorker.
You have to set this.
backgroundWorker.WorkerReportsProgress = true;
Gets or sets a value indicating whether the BackgroundWorker can
report progress updates.
EDIT
If still not working checks whether you have bind the event properly in the designer code. Or just add something like below in your class.
backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.worker_ProgressChanged);
In your Upload method you have to report progress. Otherwise above event won't fire. Keep in mind that, it's not easy to report actual progress always.
Below is an example code for a DoWork method. Look at here if you want to see a complete example.
static void bw_DoWork (object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i += 20)
{
if (_bw.CancellationPending) { e.Cancel = true; return; }
_bw.ReportProgress (i);
Thread.Sleep (1000); // Just for the demo... don't go sleeping
} // for real in pooled threads!
e.Result = 123; // This gets passed to RunWorkerCompleted
}

Windows Forms\Console - Make Your KeyBord Type Words. Or Your Mouse to be clicked

Well,
I am not sure if someone already asked this qusiton before. I was trying to look arround but noting came up. (if there is, please show me and close this. I am very sorry!)
For a few days now I am looking for a way that when I click on a button in my windows form in C# it will copy paste something to somewhere else.
The best way to expline this:
Lets say I got a Ms Word open, and I want that when I will click on a button in my windows form, after 5 seconds, it will write something in my word office. Of course I will open the Ms Word by my self.
Another thing: is how to make your mouse click on hes key?
edit:
When i use this code --
int forhow = int.Parse(textBox1.Text);
for(int i = 0;i <forhow; i++)
{
i++;
SendKeys.Send("ספאמר על ידי פריזו - ספאמר על גירסא ראשונה");
//ספאמר על ידי פThread.Sleep(1200);
//Thread.Sleep(5000);
SendKeys.Send("{ENTER}");
}
well, its should do it only 1 time. is i write 1 in the text box. but, it is doing it about 50 times. and the stop. any one knows why? + . if you lick on the button, the program stops to work until she compltite all the "Send:".
I couldn't find a way to force a mouse click, but you cam mimic the keyboard using the SendKeys class. All code that is not in between "//{" and "//}" was generated by visual studio.
Hope this helps!
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.Diagnostics;
//}
namespace ClickToWord
{
public partial class Form1 : Form
{
//{
Process imsWord = new Process();
Timer tempTime = new Timer();
int counter = 0;
//}
public Form1()
{
InitializeComponent();
//{
imsWord.StartInfo.FileName = #"";
//Inside the "" put the path to the file/application. No need to escape it, because of the "#"
tempTime.Interval = 1000;
//The interval in miliseconds
tempTime.Tick += new EventHandler(tempTime_Tick);
//}
}
void tempTime_Tick(object sender, EventArgs e)
{
//{
char send = 'a';
send += (char)(counter % 26);
SendKeys.Send(send.ToString());
counter++;
//An example of looping through the alphabet. Send any string via SendKeys, and it will act as if the keyboard ent it.
//This mimics keyboard strokes, and requires the document to have focus. That is why it is not the ideal way to do this.
//To programmatically communicate with Word, use the Microsoft Word Object Model library.
//tempTime.Enabled = false;
//}
}
private void button1_Click(object sender, EventArgs e)
{
//{
imsWord.Start();
//Starts the proccess
tempTime.Enabled = true;
//Starts the timer
//}
}
}
}

Show the new form after Progress Bar percentage completed c# project

I am working on a project using Visual Studio(c#). I want to create a startup form when i install my application with a progress bar. And after progress bar completed this form should be hide and a new form should be open. can u help me about this problem?
Edit:
I've just made a sample application trying to use exactly the code that you've specified. It worked fine besides just one tweak:
Form1().Show(); should be new Form1().Show();
The only way this code does not execute is if you forgot to set timer1 to enabled state in design view which causes the code to never fire up.
Are you sure the code is firing up? have you done a break-point on this piece of code?
On a sidenote: timer1 is not on a separate thread so you don't need to use Invoke (you can see if you actually need it by looking InvokeRequired property of a control)
Suggested improvement: if you are not going to use Form2 again and judging from your code, it is likely you won't; perhaps you should call Close() on Form2 instead of Hide() and release the resources. I've had times when my application kept running in background because I hid the form but never closed it and application was on "exit when last window closes" which never happened.
So to be sure, here is the final code that does work on my machine:
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;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//enable timer1 here or in designer
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
//disable timer1 first thing, otherwise it can end up ticking
//multiple times before you've had a chance to disable it
//if the timespan is really short
timer1.Enabled = false;
int d;
for (d = 0; d <= 100; d++)
progressBar1.Value = d;
Hide();
//create a new Form1 and then show it
new Form1().Show();
}
}
}
Create your form and add your progress bar
Set up event handlers on the parts of the form that should effect the progress bar
Update the progree bar to reflect the amount of work that is done
When the form is complete close it
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;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
int d;
for (d = 0; d <= 100; d++)
progressBar1.Value = d;
this.Hide();
Form1().Show();
timer1.Enabled = false;
}
}
}

Windows Forms Opacity After Shown- C#

I am tryig to fade-in a windows form using c# but it doesnt seem to work after I have shown the form. Is it possible to change the forms opacity after Ive shown it?
Code:
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.Timers;
namespace ToolStrip
{
public partial class Form1 : Form
{
Form ToolForm = new ToolForm();
Form PropForm = new PropertyGrid();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ToolForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
ToolForm.Owner = this;
ToolForm.Show();
ToolForm.Location = new Point(50, 50);
}
private void button2_Click(object sender, EventArgs e)
{
PropForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
PropForm.Owner = this;
PropForm.Show();
PropForm.Location = new Point(50, 50);
System.Timers.Timer aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
PropForm.Opacity = PropForm.Opacity - 0.25;
Console.WriteLine(PropForm.Opacity);
}
}
}
because you r using System.Timers.Timer which is a multithread timer, in it's OnTimedEvent() it calls control created by another thread, which cause exception.
If you use System.Windows.Forms.Timer, it will work. i tested.
Using your code (and creating the other necessary Form classes), I get a cross-threading exception the first time the timer fires and the event handler is called, as Benny suggests.
Making changes to your code to check InvokeRequired in the timer event handler, and use Invoke if necessary to change PropForm.Opacity, results in the opacity changing after the form is shown, as required.
Note that you probably want to start with an Opacity of 0, and increase it gradually - otherwise your form will start off completely solid and gradually fade out
I will mention in passing that Opacity will have no effect on some versions of Windows, though you say you have Opacity effects working elsewhere, so it shouldn't be that in this case.
Ive gotten it to work without timers:
int Loop = 0;
for (Loop = 100; Loop >= 5; Loop -= 10)
{
this.PropForm.Opacity = Loop / 95.0;
this.PropForm .Refresh();
System.Threading.Thread.Sleep(100);
}
but i cant seem to change this example to fade-in instead of out.

Categories