如何在WPF中播放图像和视频播放列表 [英] How to Play Image and Video playlist in WPF
问题描述
我尝试在单媒体元素中绑定两者但在切换之间有一些时间差距:
这是我的拥有到目前为止:
使用System;
使用System.Collections.Generic;
使用System.Linq ;
使用System.Text;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows。媒体;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
使用System.Reflection;
使用System.Windows.Threading;
使用System.Threading;
使用System .Data;
命名空间ImageVideoWPF
{
///< summary>
///交互逻辑for ImageVideo.xaml
///
publi c partial class ImageVideo:Window
{
DataTable table = new DataTable();
DispatcherTimer timer = new DispatcherTimer();
int i = 0;
public static bool ContentPlaying {get;组; }
public ImageVideo()
{
InitializeComponent();
table.Columns.Add(Type,typeof(string));
table.Columns.Add(Path,typeof(string));
//这里我们添加五个DataRows。
table.Rows.Add(Image,@C:\Users\dell\Desktop\Awards\pic1.png);
table.Rows.Add(Image,@C:\ Users \ dell\Desktop\Awards\pic2.bmp);
table.Rows。添加(图像,@C:\ Users \ dell \Desktop \ Awards \ pic3.gif);
table.Rows.Add(Image,@ C:\ Users \ dell\Desktop\Awards\pic4.jpg);
table.Rows.Add(Image,@C:\ Users \ dell \Desktop\IMG_4691.JPG);
table.Rows.Add(Video ,@C:\ Users \ dell \Desktop \ Blast_Video1_LU32-43.wmv);
table.Rows.Add(Image,@C:\ Users \ dell\Desktop\IMG_4693.JPG);
table.Rows.Add(Video,@C:\ Users \ dell \Desktop\future_en.mp4 );
table.Rows.Add(Image,@C:\ Users \ dell \Desktop \ IMG_4694.JPG);
}
private void Window_Loaded(对象发送者,RoutedEventArgs e)
{
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.Tick + = timer_Tick;
timer.Start();
}
void timer_Tick(对象发送者,EventArgs e)
{
MaintainPlaylist();
}
private void MaintainPlaylist()
{
if(ContentPlaying!= true)
{
timer.Stop();
RunPlayList(表。行[i] [0] .ToString(),table.Rows [i] [1] .ToString());
i ++;
}
}
private void RunPlayList(string _Type,string _Path)
{
if(_Path!= null || _Path!=)
{
ContentPlaying = true;
MediaElemVideo.Source = new Uri(_Path);
MediaElemVideo.Play();
} < br $>
}
private void MediaElemVideo_MediaEnded(object sender,RoutedEventArgs e)
{
ContentPlaying = false;
timer.Start();
}
}
}
-------------- -----------------------------
< window x:class =ImageVideoWPF.ImageVideoxmlns :x =#unknown>
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
Title =ImageVideoHeight =300Width =300Loaded =Window_Loaded>
< grid>
< grid.columndefinitions>
< columndefinition> < columndefinition width =Auto> < columndefinition width =2 *>
< wrappanel name =wpmargin =5grid.column =0>
<按钮高度=20Width =50Name =Button1Margin =5,5>< / Button>
< Button Height =20Name =Button2Width =50保证金=5,5>< /按钮>
< gridsplitter grid.column = 1width =2>
HorizontalAlignment =StretchVerticalAlignment =Stretch/>
<! - < groupbox x:name = containergrid.column =2margin =5> - >
< grid grid.column =2margin = 5>
<! - < stackpanel name =SPImagevisibility =Collapsed>
< textblock dockpanel.dock =Top removed =SteelBlueheight =25>嗨这是图片
< Image Name =ImageContainer>< / Image>
- > ;
< stackpanel name =SPvisibility =Visible>
<! - < textblock dockpanel.dock =Topremoved =Green height =25>嗨这是视频 - >
< mediaelement horizontalalignment =Stretchverticalalignment =Stretchname =MediaElemVideomediaended =MediaElemVideo_MediaEndedloadedbehavior =手册unloadedbehavior =手册>
I tried To bind up both in Single Media Element but there some time gap between switching:
Here is what i have done till now:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows.Threading;
using System.Threading;
using System.Data;
namespace ImageVideoWPF
{
/// <summary>
/// Interaction logic for ImageVideo.xaml
///
public partial class ImageVideo : Window
{
DataTable table = new DataTable();
DispatcherTimer timer = new DispatcherTimer();
int i = 0;
public static bool ContentPlaying { get; set; }
public ImageVideo()
{
InitializeComponent();
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Path", typeof(string));
// Here we add five DataRows.
table.Rows.Add("Image", @"C:\Users\dell\Desktop\Awards\pic1.png");
table.Rows.Add("Image", @"C:\Users\dell\Desktop\Awards\pic2.bmp");
table.Rows.Add("Image", @"C:\Users\dell\Desktop\Awards\pic3.gif");
table.Rows.Add("Image", @"C:\Users\dell\Desktop\Awards\pic4.jpg");
table.Rows.Add("Image", @"C:\Users\dell\Desktop\IMG_4691.JPG");
table.Rows.Add("Video", @"C:\Users\dell\Desktop\Blast_Video1_LU32-43.wmv");
table.Rows.Add("Image", @"C:\Users\dell\Desktop\IMG_4693.JPG");
table.Rows.Add("Video", @"C:\Users\dell\Desktop\future_en.mp4");
table.Rows.Add("Image", @"C:\Users\dell\Desktop\IMG_4694.JPG");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
MaintainPlaylist();
}
private void MaintainPlaylist()
{
if (ContentPlaying != true)
{
timer.Stop();
RunPlayList(table.Rows[i][0].ToString(), table.Rows[i][1].ToString());
i++;
}
}
private void RunPlayList( string _Type, string _Path )
{
if (_Path != null || _Path != "")
{
ContentPlaying = true;
MediaElemVideo.Source = new Uri(_Path);
MediaElemVideo.Play();
}
}
private void MediaElemVideo_MediaEnded(object sender, RoutedEventArgs e)
{
ContentPlaying = false;
timer.Start();
}
}
}
-------------------------------------------
<window x:class="ImageVideoWPF.ImageVideo" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ImageVideo" Height="300" Width="300" Loaded="Window_Loaded">
<grid>
<grid.columndefinitions>
<columndefinition> <columndefinition width="Auto"> <columndefinition width="2*">
<wrappanel name="wp" margin="5" grid.column="0">
<Button Height="20" Width="50" Name="Button1" Margin="5,5" ></Button>
<Button Height="20" Name="Button2" Width="50" Margin="5,5" ></Button>
<gridsplitter grid.column="1" width="2">
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<!--<groupbox x:name="container" grid.column="2" margin="5">-->
<grid grid.column="2" margin="5">
<!--<stackpanel name="SPImage" visibility="Collapsed">
<textblock dockpanel.dock="Top" removed="SteelBlue" height="25">Hi this is Image
<Image Name="ImageContainer"></Image>
-->
<stackpanel name="SP" visibility="Visible">
<!--<textblock dockpanel.dock="Top" removed="Green" height="25">Hi this is Video-->
<mediaelement horizontalalignment="Stretch" verticalalignment="Stretch" name="MediaElemVideo" mediaended="MediaElemVideo_MediaEnded" loadedbehavior="Manual" unloadedbehavior="Manual">
推荐答案
C#及其他替代方案中的并行编程 [ ^ ]
这篇关于如何在WPF中播放图像和视频播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!