[英]Wpf Custom Media element
Hey guys I'm fairly green when it comes to wpf, so I need a little help, here is what im looking to do. 大家好,我在wpf方面还是很环保的,所以我需要一点帮助,这是我想要做的。
I want to create a custom media element, I want to be able to load a video and play any where from 15 secs to 1 min of the video, I want to be able to dynamically set this on load up based on user settings. 我想创建一个自定义媒体元素,我希望能够加载视频并播放15秒到1分钟的视频,我希望能够根据用户设置在加载时动态设置该元素。 I'm loading tons of videos essentially into a list view control and I want the video to play, but im trying to save on resources by playing only a small preview of the video. 我实际上是将大量视频加载到列表视图控件中,并且我希望视频能够播放,但是我试图通过仅播放视频的小预览来节省资源。
Things I've looked into 我研究过的事情
Im just really unsure on where to go next. 我真的不确定下一步要去哪里。 I would greatly appericate any help you can give me. 我会很感激您能给我的任何帮助。
Given your comments expanding on the requirement, I'd suggest that you use the normal MediaElement, but assigning it a "preview" version of the video that only includes the fragment you want to show and has reduced resolution so as to keep load footprint down. 考虑到您对需求的评论,建议您使用普通的MediaElement,但为它分配视频的“预览”版本,该版本仅包含您要显示的片段,并降低了分辨率,以减少负载占用。
Thus, your model will have two properties, say PreviewUri and SourceUri. 因此,您的模型将具有两个属性,例如PreviewUri和SourceUri。 At the PreviewUri, you store the "preview" version of the video; 在PreviewUri,您可以存储视频的“预览”版本; at the SourceUri, you store the "full" version. 在SourceUri,您存储“完整”版本。 In your ListBox or ItemsControl, you'll use MediaElements bound to the PreviewUri. 在ListBox或ItemsControl中,将使用绑定到PreviewUri的MediaElement。 When the user makes a selection, you'll set the Source of the main MediaElement to the SourceUri. 当用户进行选择时,将主MediaElement的Source设置为SourceUri。 So your ListBox will look something like this: 因此,您的ListBox将如下所示:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<MediaElement Source="{Binding PreviewUri}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
your model will look something like this: 您的模型将如下所示:
public class Video // ideally implement INotifyPropertyChanged - not shown
{
public Uri PreviewUri { get; set; }
public Uri SourceUri { get; set; }
public static ObservableCollection<Video> LoadVideoInfo()
{
/* pseudocode
new up a collection
foreach (file in videoFolder)
collection.Add(new Video { PreviewUri = smallFileUri, SourceUri = bigFileUri });
return collection;
*/
}
}
and your code behind will look something like this: 您后面的代码将如下所示:
DataContext = Video.LoadVideoInfo();
How you show the full-size video will depend on what you want to trigger this and where the full-size video displays. 完整尺寸视频的显示方式取决于您要触发的内容以及完整尺寸视频的显示位置。 Using a ListBox rather than looping and adding children to a StackPanel may help with this because you can use the SelectedItemChanged event, databind to SelectedItem or use the IsSynchronizedWithCurrentItem property. 使用ListBox而不是循环并将子代添加到StackPanel可能会对此有所帮助,因为您可以使用SelectedItemChanged事件,将数据绑定到SelectedItem或使用IsSynchronizedWithCurrentItem属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.