2013-03-04 48 views
1

我在Windows 8商店應用程序的GridApp的itemsdetail頁面的flipview內使用MediaElement控件,並且我面臨着一個特殊類型的問題使用它。 MediaElement不顯示視頻,只有預期視頻輸出的黑屏,但播放視頻的音頻非常好。MediaElement控件不顯示視頻只播放Grid應用程序中視頻的音頻

現在我的問題的特殊部分是,在網格應用程序中有一組項目的集合(我知道每個人都可能知道這一點,只是爲了讓自己清楚),每組的第一項播放視頻就好了我的意思是它顯示的視頻和音頻很好,但組的其他項目只是不顯示視頻只是播放視頻的音頻。有人知道爲什麼會發生這種情況嗎?

這裏是XAML代碼:

<FlipView 
x:Name="flipView" 
AutomationProperties.AutomationId="ItemsFlipView" 
AutomationProperties.Name="Item Details" 
TabIndex="1" 
Grid.RowSpan="2" 
ItemsSource="{Binding Source={StaticResource itemsViewSource}}"> 

<FlipView.ItemContainerStyle> 
    <Style TargetType="FlipViewItem"> 
     <Setter Property="Margin" Value="0,137,0,0"/> 
    </Style> 
</FlipView.ItemContainerStyle> 

<FlipView.ItemTemplate> 
    <DataTemplate> 
     <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates"> 
      <ScrollViewer x:Name="scrollViewer" Style="{StaticResource VerticalScrollViewerStyle}" Grid.Row="1"> 
       <Grid Margin="120,0,20,20"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="400" /> 
         <ColumnDefinition Width="40" /> 
         <ColumnDefinition Width="360" /> 
         <ColumnDefinition Width="40" /> 
         <ColumnDefinition /> 
        </Grid.ColumnDefinitions> 
        <Border BorderBrush="Black" BorderThickness="1" Width="350" HorizontalAlignment="Left" Grid.Row="0"> 
         <MediaElement x:Name="VideoSource" AutomationProperties.Name="VideoSource" Source="/Assets/Big_Buck_Bunny.mp4" HorizontalAlignment="Center" VerticalAlignment="Stretch" Height="250" Width="350" AutoPlay="False" IsLooping="True" /> 
        </Border> 
        <Border BorderBrush="Black" BorderThickness="1" Height="65" Width="350" HorizontalAlignment="Left" Grid.Row="1"> 
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> 
          <Button x:Name="playButton" Margin="0,0" Click="playButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          <Button x:Name="pauseButton" Margin="0,0" Click="pauseButton_Click" Style="{StaticResource PauseAppBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
         </StackPanel> 
        </Border> 
       </Grid> 
      </ScrollViewer> 
     </UserControl> 
    </DataTemplate> 
</FlipView.ItemTemplate> 

這裏是後面的代碼:

private void playButton_Click(object sender, RoutedEventArgs e) 
    { 
     MediaElement media = FindControl<MediaElement>(this, "VideoSource") as MediaElement; 
     media.Play(); 
    } 

    private void pauseButton_Click(object sender, RoutedEventArgs e) 
    { 
     MediaElement media = FindControl<MediaElement>(this, "VideoSource") as MediaElement; 
     media.Pause(); 
    } 

    private void stopButton_Click(object sender, RoutedEventArgs e) 
    { 
     MediaElement media = FindControl<MediaElement>(this, "VideoSource") as MediaElement; 
     media.Stop(); 
    } 

    private DependencyObject FindControl<T>(DependencyObject controlType, string ctrlName) 
    { 
     int childNumber = VisualTreeHelper.GetChildrenCount(controlType); 
     for (int i = 0; i < childNumber; i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(controlType, i); 
      FrameworkElement fe = child as FrameworkElement; 
      // Not a framework element or is null 
      if (fe == null) return null; 

      if (child is T && fe.Name == ctrlName) 
      { 
       // Found the control so return 
       return child; 
      } 
      else 
      { 
       // Not found it - search children 
       DependencyObject nextLevel = FindControl<T>(child, ctrlName); 
       if (nextLevel != null) 
        return nextLevel; 
      } 
     } 
     return null; 
    } 

我希望我自己對這個問題不清楚。

+1

視頻是否在另一個應用中播放?嘗試http://code.msdn.microsoft.com/windowsapps/Classic-Video-Player-de838b19。 – 2013-03-04 17:25:23

+0

@Matt Harrington我不確定你在問什麼,如果你問mediaelement是否支持這種格式的視頻,那麼它確實播放該視頻格式。 – Justice 2013-03-05 17:42:58

回答

2

所以,你正在嘗試將工作。不過,爲了幫助,我寫了一個小原型。

使用此代碼背後:

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
    } 

    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     // pause 
     var _Media = GetMediaElement(sender as Button); 
     _Media.Pause(); 
    } 

    private void Button_Click_2(object sender, RoutedEventArgs e) 
    { 
     // play 
     var _Media = GetMediaElement(sender as Button); 
     _Media.Play(); 
    } 

    MediaElement GetMediaElement(Button button) 
    { 
     var _Parent = button.Parent as Grid; 
     var _GrandParent = _Parent.Parent as Grid; 
     return _GrandParent.Children.First() as MediaElement; 
    } 

    private void FlipView_SelectionChanged_1(object sender, SelectionChangedEventArgs e) 
    { 
     var _FlipView = sender as FlipView; 
     foreach (var item in _FlipView.Items) 
     { 
      var _Container = _FlipView.ItemContainerGenerator.ContainerFromItem(item); 
      var _Children = AllChildren(_Container); 
      foreach (var media in _Children) 
       media.Pause(); 
     } 
    } 

    public List<MediaElement> AllChildren(DependencyObject parent) 
    { 
     if (parent == null) 
      return (new MediaElement[] { }).ToList(); 
     var _List = new List<MediaElement> { }; 
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
     { 
      var _Child = VisualTreeHelper.GetChild(parent, i); 
      if (_Child is MediaElement) 
       _List.Add(_Child as MediaElement); 
      _List.AddRange(AllChildren(_Child)); 
     } 
     return _List; 
    } 
} 

,並使用此XAML:

<FlipView SelectionChanged="FlipView_SelectionChanged_1"> 
    <FlipView.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <MediaElement Source="{Binding}" AutoPlay="False" /> 
       <Grid Margin="20" VerticalAlignment="Bottom" Background="Black"> 
        <Button Click="Button_Click_1" HorizontalAlignment="Left">Pause</Button> 
        <Button Click="Button_Click_2" HorizontalAlignment="Right">Play</Button> 
       </Grid> 
      </Grid> 
     </DataTemplate> 
    </FlipView.ItemTemplate> 
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String> 
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String> 
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String> 
    <x:String>ms-appx:/Assets/BestSingingVideoEver.wmv</x:String> 
</FlipView> 

確保和更新自己的視頻路徑。當然,你也可以列表數據綁定。我的代碼只是一個原型來證明你想要做的事情是可能的。祝你好運!

我也想我可能已經解決了你的代碼中的一個或兩個錯誤。尤其是你在列表中尋找子控件的部分。以下是我的參考:http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

+0

傑瑞感謝您的回覆,我正在處理您給出的原型,如果出現任何問題,將會通知您。 在同一時間,我在想,該網頁中的所有其他元素從數據綁定接收他們的數據,這可能是我沒有綁定媒體元素的源路徑,我面臨我的問題? – Justice 2013-03-05 17:34:48

+0

感謝您的原型代碼所做的工作,並且您對我的代碼的錯誤是正確的。我認爲這是我搜索mediaelement控件的方式,反正非常感謝您的幫助 – Justice 2013-03-06 16:52:42

相關問題