Stop on click and restart to 0 - xaml

<StackPanel>
<MediaElement x:Name="UnMediaElement"
Height="10"
Width="10"
Source="/Assets/sounds/unu.wav"
AutoPlay="False" />
<Button x:Name="play1SoundButton"
Height="80"
Width="200"
HorizontalAlignment="Left"
Content="Play Sound"
Click="play1SoundButton_Click" />
</StackPanel>
I have 2 buttons. When I press button1, it will play a sound. I want the sound to stop and restart to 0 when I press button2. Right now, if I press on button1 and then on 2 and then again on button1, it resumes. Also, I don't want to be able to play another sound while one is playing.

1) Your player just doesn't stop the looping audio instead it pauses it. If you are using an Audio Control that has "controlName.pause();" look for "controlname.Stop();". This will stop the whole audio loop and then will start from the beginning when you click your "Play Sound" Again.
2) On your Audio Control, There will be a flag method which will return you if your Control is In Use (Playing Something). If Yes, You can stop it and play next audio.
Few simple things before you go back to your code after reading this.
It would be lot better if you post some code and show where you are having problem. I cannot help but give you theoretical answers only since there is no info as to what your methods are doing exactly.
Edit: Based on the Code that you posted, Change your method to below. Then you should be able to play seamless media.
private void play1SoundButton_Click(object sender, RoutedEventArgs e)
{
if (DoiMediaElement.CurrentState == MediaElementState.Playing)
{
DoiMediaElement.Stop();
}
UnMediaElement.Play();
}
private void play2SoundButton_Click(object sender, RoutedEventArgs e)
{
if (UnMediaElement.CurrentState == MediaElementState.Playing)
{
UnMediaElement.Stop();
}
DoiMediaElement.Play();
}

Related

How to prevent Esc key from light dismissing a popup?

I have a Popup which needs to be light dismissed when user taps an element outside the popup. By default, pressing the Esc key also triggers LightDismiss, which I would like to prevent. Instead, I'd like to handle this KeyDown event explicitly.
Handling the Tapped event in the page's root to check if user's tapped outside the popup is one solution, but since every tap in my app will go through this, I'd prefer not to take this approach.
Some pseudo-code:
<Page>
<Grid>
<Popup
x:Name="FooTip"
Opened="FooTip_Opened"
Closed="FooTip_Closed"
IsLightDismissEnabled="True"
ShouldConstrainToRootBounds="True">
<!-- Popup content here -->
</Popup>
</Grid>
</Page>
TLDR: FooTip should not get light dismissed when Esc key is pressed.
FooTip should not get light dismissed when Esc key is pressed.
You could handle the PreviewKeyDown event for the Page like this:
private void OnPreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape
&& FooTip.IsOpen)
{
e.Handled = true;
}
}

Detecting the end of scrolling in a ListView

Universal Windows 8.1 Store project here.
I want to know, when a ListView stops scrolling after user interaction. I found plenty of information on the net, but not one example reliably working on WP 8.1 (WPF/WP8 examples do not help much, and there are loads of them).
Here's what I do now.
1. The ListView
<ListView
x:Name="MessageList"
ItemsSource="{Binding Messages}"
VerticalAlignment="Bottom"
ItemContainerStyle="{StaticResource ChatListViewItemStyle}"
PointerEntered="MessageList_OnPointerEntered"
>
<ListView.ItemTemplate>
<DataTemplate>
<messages:MessageContainer />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
2. The ScrollViewer
I get a ScrollViewer reference from the ListView in code behind.
// GetChildElement<T>(this DependencyObject root) is a simple extension method of mine
Scroll = MessageList.GetChildElement<ScrollViewer>();
3. ListViewer.PointerEntered and ScrollViewer.ViewChanged
PointerEntered handler is used to detect the start of user interaction. When an interaction is detected, I subscribe to Scroll.ViewChanged and use IsIntermediate flag of the event to detect when the list stops scrolling (including inertia).
void MessageList_OnPointerEntered(object sender, PointerRoutedEventArgs e)
{
Debug.WriteLine("START MONITORING INTERACTION");
Scroll.ViewChanged += OnViewChangedByUser;
}
void OnViewChangedByUser(object sender, ScrollViewerViewChangedEventArgs e)
{
Debug.WriteLine("WAITING FOR INTERACTION TO END");
if (!e.IsIntermediate) {
Debug.WriteLine("INTERACTION ENDED");
Scroll.ViewChanged -= OnViewChangedByUser;
}
}
This does work to some extent.
The problem
The problem is, ViewChanged is not fired when the list is scrolled to the end/start and the user pulls it out of bounds and releases it, causing it to return back with inertia. So, the interaction start is detected, but the end is not. ViewChanged is not fired at all -- neither with IsIntermediate=True, nor with False.
What is a better way of doing what I want?
Sadly there's no good way to do this on Windows 8.1 aside from repeated polling and checking the ScrollOffset.
I'd just get an array of 10 doubles, and like 10 times a second I'd shift-in the current scroll offset. Than in that same handler check if the last 5 equals to the end of your list than raise an event.
As Tamás Deme puts it, there's no nice way of doing what is required. However, I've found a workaround that works in my case (nothing nice about it though).
In fact, I'm detecting, whether the list is scrolled to the bottom, when the scrolling stops. It's detecting the end of scrolling is what is causing so much trouble.
There are two parts of the problem: 1 - detecting the end of user interaction, 2 - detecting the end of inertia. Suprisingly, there's no good way of solving either of them. Thankfully, what I actually need is just knowing the value of VerticalOffset when scrolling (user-driven or inertia-animated) ceases. I don't actually have to know whether the user is still holding the list or not.
void MessageList_OnPointerEntered(object sender, PointerRoutedEventArgs e)
{
IsScrolledToLastLine = false; // this is to signal, that the user is
// holding the list, and there must be no
// automatic scrolling, when content is
// added to it.
Debug.WriteLine("[*]START MONITORING INTERACTION");
Scroll.ViewChanged += OnViewChangedByUser;
Scroll.LayoutUpdated += OnScrollLayoutUpdated;
}
void OnScrollLayoutUpdated(object sender, object e)
{
// will trigger multiple times during scrolling
// AND
// will trigger when inertia finally stops
// (regardless of the changes of VerticalOffset)
IsScrolledToLastLine = Scroll.ScrollableHeight == Scroll.VerticalOffset;
Debug.WriteLine("Interaction progress: {0}", IsScrolledToLastLine);
}
void OnViewChangedByUser(object sender, ScrollViewerViewChangedEventArgs e)
{
if (!e.IsIntermediate) {
IsScrolledToLastLine = Scroll.ScrollableHeight == Scroll.VerticalOffset;
Debug.WriteLine("Interaction end: {0}", IsScrolledToLastLine);
Scroll.LayoutUpdated -= OnScrollLayoutUpdated;
Scroll.ViewChanged -= OnViewChangedByUser;
}
}
Scroll.LayoutUpdated
LayoutUpdated is fired multiple times during scrolling. Unlike ViewChanged this event is also fired when inertia stops in the situation shown in the picture of the post. Unfortunatelly, there is no way to determine in LayoutUpdated, whether the list stopped scrolling completely or not.
ViewChanged works fine when you actually change VerticalOffset by scrolling; LayoutUpdated covers the over-scrolling situation.
There is another problem though: OnScrollLayoutUpdated may remain subscribed when scrolling over the edges of the list, as ViewChanged will not trigger. Fortunately, I can just ignore that, this doesn't break anything.

Play sound clip in Windows Phone 8

I'm trying to do something that I thought would be pretty simple, but its not proving that way. I want to play a sound clip from a URI that I'm obtaining from an API. The URI provides an absolute URI to the audio clip.
I've tried using the MediaElement component and that works, except it hangs the UI while the clip is downloading/playing. This means a poor user experience and probably wouldn't get past store certification either.
I've also tried the SoundEffect class from the XNA framework, but that complains about an absolute URI – it seems this only works with relative links and thus wont suffice.
I'm wondering what other options I have for playing a sound clip in a windows phone 8 app that wont hang the UI
Any suggestions welcomed.
Thanks
Using media files on a network or the Internet is going to add latency to the app. You can't start playing the media until the phone has loaded the file. Use the MediaElement.MediaOpened to determine when the media is ready, then call .Play();
Of course, you need to let the users know that the media is downloading. My example uses the SystemTray ProgressIndicator to show the user a message.
XAML
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<StackPanel>
<Button x:Name='PlayButton'
Click='PlayButton_Click'
Content='Play Media' />
<MediaElement x:Name='media1'
MediaOpened='Media1_MediaOpened'
AutoPlay='False' />
</StackPanel>
</Grid>
CODE
private void Media1_MediaOpened(object sender, RoutedEventArgs e) {
// MediaOpened event occurs when the media stream has been
// validated and opened, and the file headers have been read.
ShowProgressIndicator(false);
media1.Play();
}
private void PlayButton_Click(object sender, RoutedEventArgs e) {
// the SystemTray has a ProgressIndicator
// that you can use to display progress during async operations.
SystemTray.ProgressIndicator = new ProgressIndicator();
SystemTray.ProgressIndicator.Text = "Acquiring media - OverTheTop.mp3 ";
ShowProgressIndicator(true);
// Get the media
media1.Source =
new Uri(#"http://freesologuitar.com/mps/DonAlder_OverTheTop.mp3",
UriKind.Absolute);
}
private static void ShowProgressIndicator(bool isVisible) {
SystemTray.ProgressIndicator.IsIndeterminate = isVisible;
SystemTray.ProgressIndicator.IsVisible = isVisible;
}

manipulationdelta for popup window - XAML Windows 8

I am trying to put floating popup windows on screen. I want user to be able to move that popup window anywhere on the screen.
And so I am using:
<Popup x:Name="myPopup" Grid.Row="0" Grid.RowSpan="2" Margin="0, 0, 0, 0" ManipulationMode="All" ManipulationDelta="PopupManipulationDelta" IsLightDismissEnabled="False" Visibility="Collapsed" IsOpen="false">
code behind:
private void PopupManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var ct = (CompositeTransform)addShapesPopup.RenderTransform;
ct.TranslateX += e.Delta.Translation.X;
ct.TranslateY += e.Delta.Translation.Y;
UpdateLayout();
}
But this is not working. The function PopupManipulationDelta is not even getting called.
I tried using same logic on shapes like rectangle, ellipse and it works fine there.
Can you please help me understand why it's not working with popup?
Thank you.
I believe a Popup does not have any visual representation, so it can't respond to hit testing and thus to manipulation events. Simply put some control inside of it and have that respond to the input events. It can be a Grid with Background="Transparent" if you don't want it to be visible.

WinRt WebView "Back Button"

I'm using the WebView control in a Metro app. I'd like to implement a "Back" button function, but can't seem to locate an inbuilt one. Can anyone tell me if there is one, or do I need to roll my own (admittedly not the most complex coding task, but I don't want to reinvent the wheel)?
This worked for me:
webView.InvokeScript("eval", new[] { "history.go(-1)" });
Unfortunately you will have to roll your own. You should be able to use something like:
webView.InvokeScript("history.back();");
or possibly
webView.InvokeScript("history.go(-1);");
The suggested code works. Here it is more complete:
in the xaml
<Button Content="Back" HorizontalAlignment="Left" Margin="580,98,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
in the xaml.cs
private void Button_Click_1(object sender, RoutedEventArgs e)
{
webView.InvokeScript("eval", new[] { "history.go(-1)" });
}