2

我有一个视频(持续时间:3 秒),我需要创建 2 个状态

1-视频应始终达到第二个 1.5 并从头开始播放。

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-当我按下一个按钮时,视频应该播放完整的视频(3 秒)。(简单标志,布尔值)

所以我的问题是,我怎么知道什么时候mediaelement.position = 1.5 seconds??....我想到了一种方法,比如玩或类似的东西。

4

3 回答 3

3

如果您获得MediaElement'Clock属性,您可以附加到CurrentTimeInvalidated事件上并观察时间达到 1.5 秒。该事件具有很高的精度(即它经常被提出),因此除非您必须这样做,否则您不想对事件做太多的响应。

于 2012-06-25T18:28:04.477 回答
0

我解决了这个问题... :) :) ....

我决定用其他论坛的许多想法制作自己的应用程序。

我的解决方案比我计划的要容易,我使用了 2 个视频、2 个 mediaElements、一个 mediaEnded 事件和布尔变量来更改视频......

并且完美运行!解决方案在这里------> (解决方案和评论)

在我的应用程序中,我不必使用诸如时钟、TimeLines、DispatcherTimer 之类的属性或诸如 CurrentTimeInvalidate 之类的任何事件,我只使用了 MediaEnded 事件和一个布尔变量。:) 不再。我有 2 个视频(1.5 秒和 3 秒)。当 MediaEnded(media 1,5 seconds) mediaElement1,5sec.Position = TimeSpam.Zero; 和 MediElement3sec.Position = TimeSpam.Zero,当我单击按钮时,我只是评估变量(布尔值)并播放 3 秒的完整视频。

但是,源代码在这里: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;
    }


}

}

于 2012-07-04T18:07:59.713 回答
0

接受的解决方案似乎更像是一种解决方法而不是解决方案.. 万一您需要使用的不是 1.5 而是其他时间,例如。2.5秒?你需要改变视频吗?解决方案可能是使用 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

当达到timer_Tick时,只需将位置设置为零并再次调用 Play() :

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

并且当单击第二个按钮时,分离计时器(...可以在需要时附加):

timer.Tick -= timer_Tick;
于 2021-10-13T09:33:49.340 回答