Issue with MediaElement and thread.sleep - c#

I am making a selection change from a combobox. When that event occurs, I want to play one media file, pause 5 seconds and then play another media file. What is actually happening is that there is a 5 second pause. Then only the second media file plays (vb.mp4). What am I doing wrong here?
private void cmb_adGroupZoneOne_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.mediaElement.Source = new Uri("C:/fb.mp4");
this.mediaElement.LoadedBehavior = MediaState.Manual;
this.mediaElement.Play();
System.Threading.Thread.Sleep(5000);
this.mediaElement.Source = new Uri("C:/vb.mp4");
this.mediaElement.LoadedBehavior = MediaState.Manual;
this.mediaElement.Play();
}

<MediaElement Name="mediaElement" MediaEnded="mediaElement_MediaEnded" />
private void cmb_adGroupZoneOne_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.mediaElement.Source = new Uri("C:/fb.mp4");
this.mediaElement.LoadedBehavior = MediaState.Manual;
this.mediaElement.Play();
}
private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
System.Threading.Thread.Sleep(5000);
this.mediaElement.Source = new Uri("C:/vb.mp4");
this.mediaElement.LoadedBehavior = MediaState.Manual;
this.mediaElement.Play();
}

Related

WebBrowser.DocumentCompleted and Navigate - C#

I'm trying to modify a simple application which use the WebBrowser item:
private void button1_Click(object sender, EventArgs e)
{
var menu = webBrowser1.Document.GetElementById("SomeItem").InvokeMember("click");
webBrowser1.Visible = true;
button1.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Visible = false;
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("domain");
}
This works fine, the page load and then after clicking the button the user is redirected to the desired location.
Now I'm trying to make this without the help of the button.
Simply put the button code inside the load function didn't help, so I've introduced in my code the DocumentCompleted event.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("SomeDomain");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentComplete);
}
private void wb_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
wb.Document.GetElementById("SomeItem").InvokeMember("click");
}
By debugging it, I saw that the wb object is correctly instantiated with the "domain" but I got System.NullReferenceException when I try to retrieve SomeItem.
The document is not fully loaded because I'm still in the load function ?

take a snapshot from wecam by using DShowNET library

how can i make a snapshot in my cod:
using DirectX.Capture;
using DShowNET;
private void Form1_Shown(object sender, EventArgs e)
{
DirectX.Capture.Capture capture = null;
Filters filters = null;
capture = new DirectX.Capture.Capture(filters.VideoInputDevices[deviceNo], filters.AudioInputDevices[0]);
capture.PreviewWindow = pictureBox1;
}
private void _Pause_Click(object sender, EventArgs e)
{
//??
}
i want to convert pictureBox1's video stream to a image by clicking on "Pause" button.
and then save pictureBox1's pic(*.jpg) to data base.

Continuous Playing of Different Video Files in a VLC Playlist

I am trying to play different video files continuously on VLC player on my Winform application.
The problem I face is between different playlist videos there is a 1-2 seconds of black screen.
How can I play all videos in my playlist smoothly without any waiting?
private void buttonLoad_Click(object sender, EventArgs e)
{
var uri = new Uri(#"C:\Users\Val\Downloads\000013.ts");
var converted = uri.AbsoluteUri;
var uri2 = new Uri(#"C:\Users\Val\Downloads\000210.ts");
var converted2 = uri2.AbsoluteUri;
axVLCPlugin21.playlist.add(converted);
axVLCPlugin21.playlist.add(converted2);
}
private void buttonStart_Click(object sender, EventArgs e)
{
axVLCPlugin21.MediaPlayerEndReached += new EventHandler(OnTimedEvent);
axVLCPlugin21.playlist.playItem(0);
}
private void OnTimedEvent(object sender, EventArgs e)
{
axVLCPlugin21.playlist.playItem(1);
}
this is a simplified version of what I am trying to do.
When the player reaches end of the first video file it starts the second on by eventhandler function.
The best way I know to play videos seamlessly is to avoid the timed event in your example:
private void buttonLoad_Click(object sender, EventArgs e)
{
var uri = new Uri(#"C:\Users\Val\Downloads\000013.ts");
var converted = uri.AbsoluteUri;
var uri2 = new Uri(#"C:\Users\Val\Downloads\000210.ts");
var converted2 = uri2.AbsoluteUri;
axVLCPlugin21.playlist.add(converted);
axVLCPlugin21.playlist.add(converted2);
}
private void buttonStart_Click(object sender, EventArgs e)
{
axVLCPlugin21.playlist.play();
}

sound not playing at the right moment

I created a MediaElement for a Background Music in my Windows Phone App and it plays perfectly
now the problem when I tried to add sound for a button when pressed
When I debug , the Background music plays , but when I press the button it navigates me to the next page without playing the sound I created, and when I press BackButton from the Navigated Page, it plays it, why?
code:
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.Play();
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.Play();
}
you should handle this outside button click,
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
Add an event handler to the click and call separately,
Click.MediaEnded += Click_Completed;
void Click_Completed(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
or you can make MediaEnded event of the mediaElement and there in you can call the navigation
Like this:
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.MediaEnded += Click_MediaEnded;
Click.Play();
}
void Click_MediaEnded(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}

C# AxWindowsMediaPlayer loop

I've got this annoying problem which I can't track down where it goes wrong.
I'm creating a Windows Media Player in code and I'm trying to loop a video... It loops, but only once...
So it plays the video, and once more. And then it just stop and shows the end of the video. So it seems as if it loops only once.
This is the code I have:
try {
wmPlayer = new AxWMPLib.AxWindowsMediaPlayer();
wmPlayer.enableContextMenu = false;
((System.ComponentModel.ISupportInitialize)(wmPlayer)).BeginInit();
wmPlayer.Name = "wmPlayer";
wmPlayer.Enabled = true;
wmPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
mainForm.Controls.Add(wmPlayer);
((System.ComponentModel.ISupportInitialize)(wmPlayer)).EndInit();
wmPlayer.uiMode = "none";
if(kind == "idle") {
IdleVideo(name);
}
}
catch { }
}
private static void IdleVideo(string name) {
System.Diagnostics.Debug.WriteLine("Video called once");
wmPlayer.URL = #"C:\ProjectSilver\assets\RadarDetectie\idle\" + name + "_idlescreen_movie.ogv";
Debug.WriteLine(wmPlayer.URL);
wmPlayer.settings.setMode("loop", true);
wmPlayer.Ctlcontrols.play();
}
So I hope you guys can help, why doesn't it keep playing?
just use
private void Form1_Load(object sender, EventArgs e)
{
// give the path of your video here
axWindowsMediaPlayer1.URL = "Path of your video";
// this line will automatically start your video
axWindowsMediaPlayer1.settings.autoStart = true;
//here the system will automatially create a thread and will keep on
running your video...
axWindowsMediaPlayer1.settings.setMode("loop", true);
}
Add an event handler for the PlayStateChange event:
wmPlayer.PlayStateChange += wmPlayer_PlayStateChange;
Then in the event handler check if e.newState==8 which means media ended:
AxWMPLib.AxWindowsMediaPlayer wmPlayer;
private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if(e.newState==8) // MediaEnded
// call function to play the video again
}
For play states, check this:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd562460%28v=vs.85%29.aspx
Edit:
I don't know what you do with kind, or where the first part of your code is defined, but this worked for me:
AxWMPLib.AxWindowsMediaPlayer wmPlayer;
private void button2_Click(object sender, EventArgs e)
{
wmPlayer = new AxWMPLib.AxWindowsMediaPlayer();
wmPlayer.CreateControl();
wmPlayer.enableContextMenu = false;
((System.ComponentModel.ISupportInitialize)(wmPlayer)).BeginInit();
wmPlayer.Name = "wmPlayer";
wmPlayer.Enabled = true;
wmPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(wmPlayer);
((System.ComponentModel.ISupportInitialize)(wmPlayer)).EndInit();
wmPlayer.uiMode = "none";
wmPlayer.URL = #"C:\...";
wmPlayer.settings.setMode("loop", true);
wmPlayer.Ctlcontrols.play();
}

Categories