How to pause MediaElement when single tap/touch on the screen - UWP - c#

I have to play a video using MediaElement. I want to pause the video when the user taps the screen. I found that there is double tapped event on MediaElement, but couldn't find any single touch event. How can I do this?
<MediaElement Name="videoMediaElement" AreTransportControlsEnabled="True" Stretch="Fill"
MediaOpened="videoMediaElement_MediaOpened" CurrentStateChanged="Media_State_Changed">
<MediaElement.TransportControls>
<MediaTransportControls Background="Red" Foreground="White"
IsStopButtonVisible="True" IsStopEnabled="True" IsTextScaleFactorEnabled="True"
IsPlaybackRateEnabled="True" IsPlaybackRateButtonVisible="True"
IsFastForwardButtonVisible="True" IsFastForwardEnabled="True"
IsFastRewindButtonVisible="True" IsFastRewindEnabled="True"/>
</MediaElement.TransportControls>
</MediaElement>
private async void Media_State_Changed(object sender, RoutedEventArgs args)
{
if (videoMediaElement.CurrentState == MediaElementState.Paused)
{
}
}

There are plenty of events you can use for that, e.g. the MouseLeftButtonDown or TouchDown events. It's as simple as that:
private void element_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
((MediaElement)sender).Pause();
}
private void element_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
{
((MediaElement)sender).Pause();
}
By #JetChopper:
private void element_PointerPressed(object sender, PointerRoutedEventArgs e)
{
((MediaElement)sender).Pause();
}

Related

How can I change the resize mode in the back end in WPF

I have set the initial resize mode of the window to be "NoResize" as shown below.
<Widnow ... WindowStyle="None" ResizeMode="NoResize">
I want to change the resize mode to "CanResizeWithGrip" after this button is clicked.
<Button x:Name="btnResize" Content="Click Me!" Click="BtnResize_Click"/>
And this is my event.
private void BtnResize_Click(object sender, RoutedEventArgs e)
{
}
private void BtnResize_Click(object sender, RoutedEventArgs e)
{
this.ResizeMode = ResizeMode.CanResizeWithGrip;
}

Cannot play media Element programmatically

I developed an app in Windows Phone 8. I have a list with mp3 from web and a mediaelement can't play them.
This is my code
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
}
void note1_MediaOpened(object sender, RoutedEventArgs e)
{
play.Play();
}
And xaml is :
<MediaElement HorizontalAlignment="Left" Height="100" Margin="167,538,0,0" VerticalAlignment="Top" Width="100" Name="play" AutoPlay="True"/>
I have try it in xaml and play it but programmatically not. Can someone help me ?
I do not think you need to Play inside MediaOpened event. Just execute .Play after assigning Source to the MediaPlayer
Just add this to button click event
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.Play();
}

Audio and Video playing in Windows 10 universal app?

I need to play audio and video in windows app where I Get the Url from networkcall and I need to add Url to Source.
I tried in this way, But no video is played or audio is played.
Someone help me in how to achieve this?
Is there any other way to achieve this?
Thanks in Advance.
My xaml code:
<StackPanel HorizontalAlignment="Center" Grid.Row="3">
<MediaElement x:Name="media"
Source="Videos/video1.mp4"
Width="400"
AutoPlay="False"
AreTransportControlsEnabled="True" />
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center">
<Button Content="Play" Click="Play_Click"/>
<Button Content="Pause" Click="Pause_Click"/>
<Button Content="Stop" Click="Stop_Click" />
</StackPanel>
</StackPanel>
My cs code:
async void Play_Click(object sender, RoutedEventArgs e)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
Uri pathUri = new Uri("https://www.youtube.com/watch?v=sOEg_YZQsTI");
media.Source = pathUri;
Util.debugLog("PLaying ...");
media.Play();
media.Volume = 40;
});
}
async void Pause_Click(object sender, RoutedEventArgs e)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
Util.debugLog("Paused .. ");
media.Pause();
});
}
async void Stop_Click(object sender, RoutedEventArgs e)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
Util.debugLog("Stoped ..");
media.Stop();
});
}
void Media_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
// Handle failed media event
}
void Media_MediaOpened(object sender, RoutedEventArgs e)
{
// Handle open media event
}
void Media_MediaEnded(object sender, RoutedEventArgs e)
{
// Handle media ended event
}
If I get it right you are currently assigning a website as the source of the MediaElement. That can't work. If you want to embed youtube content you have two possibilities:
Embed the webpage in a WebView
Set the url to the video itself as the Source

How to detect DoubleClick in WPF ToggleButton

In my WPF application I have Toggle Button, I want to detect when user double click on it (in both cases if it checked or unchecked).
How can I do that?
Thanks in advance
You can use the OnPreviewMouseDoubleClick event
xaml:
<ToggleButton Height="75" Width="100" PreviewMouseDoubleClick="Control_OnPreviewMouseDoubleClick"/>
code-behind:
private void Control_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var toggleButtton = sender as ToggleButton;
if (toggleButtton != null)
{
if (toggleButtton.IsChecked.HasValue)
{
if (toggleButtton.IsChecked.Value)
{
// Checked
}
else
{
// Unchecked
// this will re-check the button if double-click unchecks it
//
toggleButtton.IsChecked = true;
toggleButtton.Focus();
}
}
}
}
Use PreviewMouseDoubleClick event (msdn):
XAML:
<ToggleButton x:Name="tButton" Height="30" Content="MyButton"
PreviewMouseDoubleClick="tButton_PreviewMouseDoubleClick"
/>
Code-behind:
private void tButton_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
tButton.IsChecked = !tButton.IsChecked.Value;
e.Handled = true;
//...
}
This event is pretty easy with togglebuttons
Xaml you write the following to get an EventHandler:
<ToggleButton Name="button1" MouseDoubleClick="button1_DoubleClick" />
In c# you write the following to get an EventHandler:
button1.MouseDoubleClick += new MouseButtonEventHandler(button1_DoubleClick);
And in both cases you need:
void button1_DoubleClick(object sender, MouseButtonEventArgs e)
{
}

simultaneous storyboards in windows phone 7

I have looked everywhere I could think of, but I can't find the answer to this...
I want to make a sort of template Storyboard for windows phone 7 (xaml and c#), and then to use that storyboard to animate multiple objects the same way. (i.e. rotating all objects 90 degrees at the same time) Any ideas?
It has to be dynamically done. By that I mean that I want to assign different objects to several different storyboards during the program, so it can't be something static.
The main problem that I have faced up to this point is that a storyboard must be stopped before it can be used again, therefore they can't be simultaneously running. I was thinking however that if I could make temporary copies of the Storyboard, that I could run them all at the same time? Please give me any ideas that you have. Thanks in advance!
I initialize my objects (rectangles) in a grid as such:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Rectangle Name="rec1" Height="80" Width="10" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,30,140,0">
<Rectangle.RenderTransform>
<RotateTransform x:Name="rec1Transform" Angle="0" CenterX="5" CenterY="-10" />
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Name="rec2" Height="10" Width="80" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,55,0">
.
.
.
</Rectangle>
</Grid>
At this moment, I have 4 general storyboards that I want to use many times over, and I am using a series of buttons to imitate all of the animations. For example:
My Storyboards
private void rotate1(object parameter, object secondParameter, String Target)
{
oneTwo.Stop();
oneTwo.Children[0].SetValue(Storyboard.TargetNameProperty, Target);
oneTwo.Begin();
}
private void rotate2(object parameter, object secondParameter, String Target)
{
twoThree.Stop();
twoThree.Children[0].SetValue(Storyboard.TargetNameProperty, Target);
twoThree.Begin();
}
private void rotate1Inverse(object parameter, object secondParameter, string Target)
{
twoOne.Stop();
twoOne.Children[0].SetValue(Storyboard.TargetNameProperty, Target);
twoOne.Begin();
}
private void rotate2Inverse(object parameter, object secondParameter, string Target)
{
threeTwo.Stop();
threeTwo.Children[0].SetValue(Storyboard.TargetNameProperty, Target.ToString());
threeTwo.Begin();
}
My Simulation Buttons
//Rotate Buttons
private void button_Click1(object sender, RoutedEventArgs e)
{
rec1Transform.CenterY = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec1Transform");
}
private void button_Click2(object sender, RoutedEventArgs e)
{
rec2Transform.CenterX = -10;
rotate1(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec2Transform");
}
private void button_Click3(object sender, RoutedEventArgs e)
{
rec3Transform.CenterY = -10;
rotate1(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec3Transform");
}
private void button_Click4(object sender, RoutedEventArgs e)
{
rec4Transform.CenterX = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec4Transform");
}
private void button_Click5(object sender, RoutedEventArgs e)
{
rec5Transform.CenterY = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec5Transform");
}
private void button_Click6(object sender, RoutedEventArgs e)
{
rec6Transform.CenterX = -10;
rotate2(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec6Transform");
}
private void button_Click7(object sender, RoutedEventArgs e)
{
rec7Transform.CenterY = -10;
rotate1(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec7Transform");
}
//Inverse Rotate Buttons
private void button_Click1_Inverse(object sender, RoutedEventArgs e)
{
rec1Transform.CenterY = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec1Transform");
}
private void button_Click2_Inverse(object sender, RoutedEventArgs e)
{
rec2Transform.CenterX = 90;
rotate2Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec2Transform");
}
private void button_Click3_Inverse(object sender, RoutedEventArgs e)
{
rec3Transform.CenterY = 90;
rotate2Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec3Transform");
}
private void button_Click4_Inverse(object sender, RoutedEventArgs e)
{
rec4Transform.CenterX = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec4Transform");
}
private void button_Click5_Inverse(object sender, RoutedEventArgs e)
{
rec5Transform.CenterY = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec5Transform");
}
private void button_Click6_Inverse(object sender, RoutedEventArgs e)
{
rec6Transform.CenterX = 90;
rotate1Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec6Transform");
}
private void button_Click7_Inverse(object sender, RoutedEventArgs e)
{
rec7Transform.CenterY = 90;
rotate2Inverse(RotateTransform.AngleProperty, PlaneProjection.RotationZProperty, "rec7Transform");
}
The problem is that I want to use these 4 basic storyboards SIMULTANEOUSLY on as many as 20 rectangles at a time, thereby animating all 20 rectangles in unison. But I don't want a static storyboard assigned to each rectangle, because the storyboard I want to apply to each rectangle changes every second or so. I will also be using all 4 storyboards at the same time. (i.e. I will use "oneTwo" for 3 of the rectangles, "twoThree" for 5 of the rectangles, "twoOne" for 8 of the rectangles, and "threeTwo" for 2 of the rectangles)
I hope that all makes sense. The program is based solely on animating many rectangles at the same time, and any help or suggestions would be greatly appreciated.
Try to bind the rectangles in a stackpanel
<Listbox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
<Storyboard x:Name="FirstStoryBoard">
<DoubleAnimation Storyboard.TargetName="rectangle"
Storyboard.TargetProperty="Width"
From="0"
To="1"
//AutoReverse="True"
Duration="00:00:01" />
</Storyboard>
</StackPanel.Resources>
<Rectangle Name="rectangle" Fill={Binding colour}>
</Rectangle>
</StackPanel>
</ListBox.ItemTemplate>
</DataTemplate>
</Listbox>
Then Have a Binding in listbox with colours..refer binding in listbox items in web.
then with a button click start the story board. the storyboard children rectangles increases its width
FirstStoryboard.begin();

Categories