2

i have a single video (duration: 3 seconds) and i need to create 2 states

1- the video should always reach the second 1.5 and play it from the start.

TimeSpan ts = new TimeSpan(0, 0, 0, 1, 500);
TimeSpan ts_Start = new TimeSpan(0, 0, 0, 0, 0);
if (mediaElement.position == ts)
  mediaElement.position = ts_Start; //doesnt work this block code

2- when i press a button, the video should play the full video (3 seconds). (simple flag, boolean)

so my question is, how do i know when the mediaelement.position = 1.5 seconds ??.... i thought of a method such as playing or something like that.

3 Answers 3

4

If you get the MediaElement's Clock property, you could attach onto the CurrentTimeInvalidated event and watch for the time to hit 1.5 seconds. The event has a lot of precision (i.e. it gets raised VERY often) so you don't want to do too much in response to the event unless you have to.

5
  • how can i do that... i dont understand how
    – makitocode
    Jun 25, 2012 at 20:37
  • which part of it don't you understand?
    – Tim
    Jun 25, 2012 at 20:55
  • MediaElement.Clock. The default value is null and clock associated with the MediaTimeline that controls media playback. That means I create a MediaTimeLine ?, i dont know how to associate the MediaTimeLine with MediaElement, also initialize a clock (which value ?), when fires the method MediaElement.Clock.CurrentTimeInvalidated ?? -- sorry, clock property is new for me.
    – makitocode
    Jun 25, 2012 at 22:00
  • Internet has some examples, however, many are used to implement a seeker using timespan, timeline, DispatcherTimer, with events, not events, and even without the property clock. understand programming, I have handled some MediaElement but this has me very lost, I've searched but each solution leaves me with more questions. I just want to identify the moment when the video reaches the second 1.5 and play it from the start, and when you press the button, play it completely.
    – makitocode
    Jun 25, 2012 at 23:42
  • This application I'm working further with the kinect sdk. I am identifying a gesture (arrangements), to admit, the video should play it fully, but acknowledge the gesture the video should reach the second 1.5 and start again. Everything about the kinect works fine (apart from other components). I tried to implement the event CurrentTimeInvalidated but it generates an error because I have not initialized the clock. I do not know where or how to start not understand when CurrentTimeInalidated event is triggered. I apologize again, I'm new to developing applications.
    – makitocode
    Jun 25, 2012 at 23:55
0

i resolved the problem... :) :) ....

i decide make me own application with many ideas that had taken of other forums.

My solution was easier than i planned, i used 2 videos, 2 mediaElements, a mediaEnded event and boolean variable to chage the video....

and works perfectly! Solution are here ------> (Solution, and coments)

in my app, i didn't have to use properties like clocks, TimeLines, DispatcherTimer, or any event like a CurrentTimeInvalidate, i just used the MediaEnded event and a boolean variable. :) no more. i have 2 videos (1,5 seconds and 3 seconds). when MediaEnded(media 1,5 seconds) mediaElement1,5sec.Position = TimeSpam.Zero; and MediElement3sec.Position = TimeSpam.Zero, and when i clicked the button, i just evaluated the variable (boolean) and play complet video of 3 seconds.

however, the source code are here: MainWindow.xaml

<Window x:Class="wpf_TestVideos.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="371" Width="525" Loaded="Window_Loaded">
<Grid>
    <MediaElement Height="268" HorizontalAlignment="Left" Margin="141,12,0,0" Name="mediaElement15sec" VerticalAlignment="Top" Width="237" MediaEnded="mediaElement15sec_MediaEnded" />
    <MediaElement Height="268" HorizontalAlignment="Left" Margin="142,12,0,0" Name="mediaElement3sec" VerticalAlignment="Top" Width="236" />
    <Button Content="Load" Height="34" HorizontalAlignment="Left" Margin="12,286,0,0" Name="btLoad" VerticalAlignment="Top" Width="73" Click="btLoad_Click" />
    <Button Content="Inicio Juego" Height="23" HorizontalAlignment="Left" Margin="128,286,0,0" Name="btStart" VerticalAlignment="Top" Width="86" Click="btStart_Click" />
    <Button Content="&quot;Reconoce Gesto&quot;" Height="23" HorizontalAlignment="Left" Margin="285,286,0,0" Name="btGesture" VerticalAlignment="Top" Width="108" Click="btGesture_Click" />
</Grid>

MainWindow.xaml.cs:

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Media.Animation;
using System.Threading;

namespace wpf_TestVideos
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    string VideoLocation = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
    string sFileName = "";
    string sFileName2 = "";
    bool bVideoLoop = true;
    TranslateTransform trans = new TranslateTransform();

    private void btLoad_Click(object sender, RoutedEventArgs e)
    {
        mediaElement15sec.LoadedBehavior = MediaState.Manual;
        mediaElement3sec.LoadedBehavior = MediaState.Manual;
        btGesture.IsEnabled = true;
        btStart.IsEnabled = true;
        btLoad.IsEnabled = false;
        DirectoryInfo df = new DirectoryInfo(VideoLocation);
        if (df.Exists)
        {
            sFileName = VideoLocation + @"\Krown_test_loop.mov";
            mediaElement15sec.Source = new Uri(sFileName);
            mediaElement15sec.Stretch = Stretch.Fill;
            sFileName2 = VideoLocation + @"\Krown_test_7.mov";
            mediaElement3sec.Source = new Uri(sFileName2);
            mediaElement3sec.Stretch = Stretch.Fill; 
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("No se puede cargar el video", "TestAll");
        }
    }

    private void btStart_Click(object sender, RoutedEventArgs e)
    {
        mediaElement15sec.Position = TimeSpan.Zero;
        mediaElement3sec.Position = TimeSpan.Zero;
        mediaElement15sec.Play();
        mediaElement3sec.Play();
        bVideoLoop = true;
        //VisualStateManager.GoToState(mediaElement15sec, "Bring1,5ToFront", true);
    }

    private void mediaElement15sec_MediaEnded(object sender, RoutedEventArgs e)
    {
        if (bVideoLoop)
        {
            mediaElement15sec.Position = TimeSpan.Zero;
            mediaElement3sec.Position = TimeSpan.Zero;
        }
    }

    private void btGesture_Click(object sender, RoutedEventArgs e)
    {
        bVideoLoop = false;
        //Animacion_Opacidad(bVideoLoop);
        //VisualStateManager.GoToState(mediaElement3sec, "Bring300ToFront", true);
    }

    private void Animacion_Opacidad(bool bLoop)
    {
        mediaElement15sec.RenderTransform = trans;
        if (!bLoop)
        {
            DoubleAnimation anim1 = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(1));
            trans.BeginAnimation(OpacityProperty, anim1);
        }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        btGesture.IsEnabled = false;
        btStart.IsEnabled = false;
        btLoad.IsEnabled = true;
    }


}

}

0

The accepted solution seems to be more a workaround than a solution.. what in case once you will need to use not 1.5 but another time, eg. 2.5 seconds? will you have to change the videos? The solution could be using a DistpatcherTimer:

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1.5); // your time
timer.Tick += timer_Tick;
timer.Start();
mePlayer.Play(); // run timer and player at same time

When the timer_Tick is reached just set the position to zero and call Play() again:

void timer_Tick(object sender, EventArgs e)
{
    mePlayer.Position = new TimeSpan(0, 0, 0, 0);
    mePlayer.Play();
}

And when clicking the second button, detach the timer (... can be attached later when necessary):

timer.Tick -= timer_Tick;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.