Stop YouTube Video on Back Button of Windows Metro App 8.1 - windows-8

I have a page on which i am showing multiple youtube videos as webviews. When i play any video, then correspoding video starts playing. But When I click on the back button of the App i should be stop then but its running in background(can hear voice of video).
here is the code of back button:
protected void GoBack(object sender, RoutedEventArgs e)
{
((Frame)Window.Current.Content).GoBack();
}
Tell me how i can dispose of this webview or can stop video.
Thanks

First make sure that the page is finalized; most probably by detaching all the event handlers you manually attached before. Then (sooner or later) the page will be finalized which in turns disposes of the webview.
A similar effect would be achieved by forward navigating the webview to an empty html page.

Here is the solution:
Invoke the UnLoaded event of WebView
- webView.Unloaded += webView_UnLoaded;
Then change the source of the webview in webView_UnLoaded event handler:
Here is my code:
private void webView_UnLoaded(object sender, RoutedEventArgs e)
{
WebView webView = (WebView)sender;
webView.Source = new Uri(" ");
}

Related

How to implement correct Back behaviour when using Frame.Navigate?

I have a Windows 10 Universal Windows Platform app with multiple pages, a main page, a list page and a details page and use the following to navigate to List page:
this.Frame.Navigate(typeof(ListPage), parameter);
When you are on the list page you can select an item which will launch a details page like so:
this.Frame.Navigate(typeof(DetailsPage), parameter);
Which works fine, the parameter is a selected Id or information then when using the Back button which on a Desktop app or Phone uses:
this.Frame.GoBack();
This always returns to the MainPage, that is when go from Main, to List to Details hitting back goes to Main, how do I get the GoBack to Go back to the previous page, it always seems to go home rather than the user expected behaviour, an ideas how to resolve this?
I’ve seen this before when you subscribe to the HardwareButtons.BackPressed event (or whatever the equivalent is in a Win10 UWP app) on a page, but then don’t unsubscribe from it. This means two event handlers get called when pressing Back, and both event handlers call Frame.GoBack().
I always subscribe to the event in the page’s NavigatedTo event, and unsubscribe in the NavigatedFrom event.
Could this be happening with your app?
If every page in your app should have the same behaviour, i.e. go back to the previous page, then subscribe to the back button event in the app class as suggested by #RoguePlanetoid in the comments:
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
The OnLaunched method would be a good place to do this. Don't forget to tell the OS to display the back button when the app is running on a desktop or tablet:
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
Then, add an event handler in the app class like this:
private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
If you want different behaviour on different pages when back is pressed, i.e. ask the user to confirm losing their changes or something, then subscribe to the back button event in a pages OnNavigatedTo method (the code will be same as above), but make sure you unsubscribe in the page's OnNavigatedFrom event:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
SystemNavigationManager.GetForCurrentView().BackRequested -= this.OnBackPressed;
base.OnNavigatedFrom(e);
}

Is it possible to control the phone's vibrator?

I need to control the phone's vibration functionality when the user taps certain elements of my app's UI. I can't figure out how to get access the vibrator though. Is it possible? If so, how do I do this?
Yes it is possible - see Vibration Device class. This code should vibrate your phone:
private void Btn_Click(object sender, RoutedEventArgs e)
{
VibrationDevice device = VibrationDevice.GetDefault();
if (device != null) device.Vibrate(TimeSpan.FromSeconds(1));
}
The above code should work for WP8.1 WinRT, for WP8.1 Silverlight take a look here at MSDN and use VibrateController class.

How to implement a 'AddToFavorites' button on an win8 app

I am developing an app on win8 and would like to add an 'AddToFavorites' button on the application bar, so that the user can select his favorites/bookmarks and place them to this. I have added this button to the application bar but the button does not perform any action when clicked.
Currently what I have done is
private async void Favorite_Button_Click(object sender, RoutedEventArgs e)
{
if (SampleDataSource.GetGroup("Favorite").Items.Count != 0)
{
if (this.pageTitle.Text != "Favorite")
this.Frame.Navigate(typeof(ItemDetailPage), SampleDataSource.GetGroup("Favorite").Items[0].UniqueId);
}
else
{
await new Windows.UI.Popups.MessageDialog("You haven't marked any favorites.").ShowAsync();
}
}
But I dont see this functionality working. Any of your suggestions would be of great help to me. It is a Windows8 Metro style C#/XAML App.
Thanks in Advance!

pause mediaelement when user switches to another app in windows 8 C# XAML

I had created mediaplayer using mediaelement in windows 8 C#,XAML.
Everything works fine.But there are critical questions I have as follows.
User Navigates to the PlayerPage.
Player starts playing the media.
Now user moves his mouse to left corner and switches to the another app.
My Questions are as follows.
How do I pause my mediaelement if user is switches to the another app?
If user presses windowsbutton + L the device goes in lock state How do I pause the mediaelement?
User presses only windowsbutton How do i pause the mediaelement?
Please give any suggestions/guidance so that I can complete my player.
In constructor assign this event. It will be called when your app's visibility gets collapsed.
Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged += MyPlayer_VisibilityChanged;
Here's event body.
async void MyPlayer_VisibilityChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.VisibilityChangedEventArgs args)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
//TODO : First get args.Visible property. If it's false & media is playing, just pause it.
});
}

Force a view to refresh?

I'm trying to do a loading icon where once you tap on the icon, it will call the following handler:
private void refresh_btn_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
refresh_btn.Visibility = System.Windows.Visibility.Collapsed;
loading_icon.Visibility = System.Windows.Visibility.Visible;
refreshMix();
}
private void refreshMix()
{
...
refresh_btn.Visibility = System.Windows.Visibility.Collapsed;
loading_icon.Visibility = System.Windows.Visibility.Visible;
}
However, the view doesn't seem to auto-reload after I change the icon visibilities before calling refreshMix(). Is there a way to force the page to reload itself?
You are probably doing some lengthy work in refreshMix() in UI thread, right? Do this work in background thread and UI thread will be free to update page.
You need set Collapsed before Visible :
control.Visibility = System.Windows.Visibility.Collapsed;
control.Visibility = System.Windows.Visibility.Visible;
Within Silverlight there is no concept of page or view re-loading. When you change the properties of any visual element, this will be reflected on the screen the next time it is rendered.