2012-06-25 20 views
2

我有一個單一的視頻(時間:3秒),我需要創建2個國家怎麼做比較的MediaElement的確切位置,而打

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 ?? ....我想到了一種方法,如玩或類似的東西。

回答

0

我解決了問題... :) :) ....

我決定讓我自己的應用程序與採取了其他論壇的很多想法。

我的解決方案比我更容易的計劃,我用了2個視頻,2個mediaElements,一個mediaEnded事件和布爾變量來恰克視頻....

和完美的作品!我沒有使用像Clocks,TimeLines,DispatcherTimer或類似CurrentTimeInvalidate的任何事件的屬性,我只是使用了MediaEnded事件和一個布爾變量。 :) 不再。我有2個視頻(1,5秒和3秒)。當MediaEnded(媒體1,5秒)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; 
    } 


} 

}

3

如果您獲得MediaElementClock屬性,您可以附加到CurrentTimeInvalidated事件並觀察時間達到1.5秒。這個事件有很多精確性(即它經常被提高),所以除非必須,否則你不想做太多的事情來回應事件。

+0

我怎麼能做到這一點.. 。我不明白如何 – Makito

+0

哪一部分你不明白? – Tim

+0

MediaElement.Clock。默認值爲空並且與控制媒體播放的MediaTimeline關聯。這意味着我創建了一個MediaTimeLine?,我不知道如何將MediaTimeLine與MediaElement相關聯,還初始化時鐘(哪個值?),何時觸發MediaElement.Clock.CurrentTimeInvalidated方法? - 對不起,時鐘屬性對我來說是新的。 – Makito