sound not playing at the right moment - c#

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));
}

Related

Issue with MediaElement and thread.sleep

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();
}

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 ?

C# brief delay after clicking image to display new source

I've got an Windows.Controls.Image that when clicked needs to change its source and have that displayed for x milliseconds before then navigating to a new Page. I tried using Thread.Sleep(200) after changing the source but no matter how I tried to force a redraw the changed source was not displayed and then when the sleep was over it instantly changed page. Does anyone know how I can do this? Cheers!
private void ButtonClicked(object sender, MouseButtonEventArgs e)
{
Image button = (Image)sender;
buttons[button.Name].Source = ImageResizer.ResizeImage(System.Drawing.Image.FromFile(#"D:\buttons\" + button.Name + ".png"), buttonSize);
NavigationService.Navigate(new UpgradeQuiz());
}
You can asynchronously wait:
await Task.Delay(2000);
But you should make your handler async:
private async void ButtonClicked(object sender, MouseButtonEventArgs e) // async after private
{
Image button = (Image)sender;
buttons[button.Name].Source = ImageResizer.ResizeImage(System.Drawing.Image.FromFile(#"D:\buttons\" + button.Name + ".png"), buttonSize);
await Task.Delay(2000);
NavigationService.Navigate(new UpgradeQuiz());
}

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();
}

How to use WebBrowser in windows phone?

I'm using the following code to make my app to load a url,
WebBrowser wb = new WebBrowser();
wb.Navigate(new Uri(uri,UriKind.Absolute));
But still it is not loading that page? What could be the problem??
WebBrowser is a control. Like a button or a textblock, you won't see anything unless you put it somewhere in your page.
To launch an external browser, use WebBrowserTask:
var webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri(uri, UriKind.Absolute);
webBrowserTask.Show();
Yo can't have any callbacks with the default WebBrowserTask in wp, if you need more control, use the WebBrowser as you have been doing,
Xaml
<phone:WebBrowser IsScriptEnabled="True" LoadCompleted="UriContentLoaded" x:Name="browserControl" />
Code Behind
public MainPage() //Your page constructor
{
InitializeComponent();
this.browserControl.Loaded += SetBrowserUri;
}
private void SetBrowserUri(object sender, RoutedEventArgs e)
{
browserControl.Navigate(new Uri("http://www.bing.com"));
}
private void UriContentLoaded(object sender, NavigationEventArgs e)
{
if (MessageBox.Show("Do you want to load a second uri?", "Load again", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
browserControl.LoadCompleted -= this.UriContentLoaded; //Remove previous handler
browserControl.LoadCompleted += this.SecondUriContentLoaded; //Add new handler
browserControl.Navigate(new Uri("http://www.google.com"));
}
}
private void SecondUriContentLoaded(object sender, NavigationEventArgs e)
{
MessageBox.Show("Finished loading");
}

Categories