
























1、基于Windows meida player的视频播放,应用于最早的EXPO项目内的视频播放:
VideoWindow
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MvcNetwork;
using log4net;
using NetworkHelper;
using ExpoSignageShare;
using System.Configuration;
using Helper;
using WMPLib;
using PlayModel;
using System.IO;namespace PlayApplication.MediaPlayers {
public partial class VideoWindow : MediaWindow {
static readonly ILog logger = LogManager.GetLogger(typeof(VideoWindow));private Delay delay;
private EventHandler handler;
private System.Timers.Timer videoTimer = new System.Timers.Timer(15);
private System.Timers.Timer liveTimer = new System.Timers.Timer(15);
private MediaItem item;public VideoWindow() {
InitializeComponent();
axWindowsMediaPlayer1.settings.setMode(
"loop", false); videoTimer.Start();
}
WMP最大的缺点是不稳定,有些功能不是很合理,而且对于重复播放的处理也比较扭曲;
基于Direct X开发的一个视频播放控件,控件代码如下:
ShgbitVideo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;
using System.IO;
using System.Diagnostics;namespace ShgbitVideo {
public partial class VideoBox : UserControl {
public string FileName { get { return fileName; } }
//public bool Loop { get { return loop; } set { loop = value; } }
public int Volumn {
get {
int result = 0;
if (IsVideo() && audio != null && !audio.Disposed) {
result = (audio.Volume + 10000) / 100;
}
return result;
}
set {
if (IsVideo() && audio != null && !audio.Disposed) {
int arg = value;
if (arg > 100) {
arg = 100;
} else if (arg < 0) {
arg = 0;
}
audio.Volume = arg * 100 - 10000;
}
}
}
public double Duration {
get {
double result = 0.0f;
if (IsVideo()) {
result = video.Duration;
}
return result;
}
}
public double CurrentPosition {
get {
double result = 0.0f;
if (IsVideo()) {
result = video.CurrentPosition;
}
return result;
}
set {
if (IsVideo()) {
double arg = value;
if (arg < 0) {
arg = 0;
}
if (loop) {
try {
arg = arg % Duration;
} catch {
arg = 0;
}
} else {
if (arg > Duration) {
arg = Duration;
}
}
video.CurrentPosition = arg;
}
}
}
public Size DefaultSize {
get {
Size result = new Size(0, 0);
if (IsVideo()) {
result = video.DefaultSize;
}
return result;
}
}
public bool Playing {
get {
bool result = false;
if (IsVideo()) {
result = video.Playing;
}
return result;
}
}
public ShgbitPlayState PlayState {
get {
ShgbitPlayState result = ShgbitPlayState.Stopped;
if (IsVideo()) {
result = (ShgbitPlayState)((int)video.State);
}
return result;
}
}
public ShgbitMediaStyle MediaStyle {
get {
return GetMediaStyle(fileName);
}
}public VideoBox() {
InitializeComponent();
}private void VideoBox_Load(object sender, EventArgs e) {
//
}public void Open(string fileName) {
Open(fileName, false);
}public void Open(string fileName, bool autoRun) {
Close();
if (File.Exists(fileName)) {
if (GetMediaStyle(fileName) == ShgbitMediaStyle.Video) {
this.fileName = fileName;
video = new Video(fileName, autoRun);
video.Owner = this;
video.Size = this.ClientSize;
try {
audio = video.Audio;
} catch (Exception ex) { }
}
} else {
throw new FileNotFoundException(null, fileName);
}
}public void Play() {
if (IsVideo() && PlayState != ShgbitPlayState.Running) {
video.Play();
}
}public void Stop() {
if (IsVideo() && PlayState != ShgbitPlayState.Stopped) {
video.Stop();
}
fileName = string.Empty;
}public void Close() {
Stop();
if (video != null && !video.Disposed) {
if (audio != null && !audio.Disposed) {
audio.Dispose();
audio = null;
}
video.Dispose();
video = null;
}
}private bool IsVideo() {
bool result = false;if (video != null && !video.Disposed) {
result = MediaStyle == ShgbitMediaStyle.Video;
}return result;
}public static ShgbitMediaStyle GetMediaStyle(string arg) {
ShgbitMediaStyle result = ShgbitMediaStyle.Others;
if (!string.IsNullOrEmpty(arg)) {
switch (Path.GetExtension(arg).ToLower()) {
case ".wmv":
case ".avi":
case ".rm":
case ".rmvb":
result = ShgbitMediaStyle.Video;
break;
case ".jpg":
case ".jpeg":
case ".bmp":
case ".png":
result = ShgbitMediaStyle.Picture;
break;
}
} else {
result = ShgbitMediaStyle.None;
}
return result;
}private void VideoBox_Resize(object sender, EventArgs e) {
if (video != null && !video.Disposed) {
video.Size = this.ClientSize;
}
}private Video video;
private Audio audio;
private string fileName;
private bool loop;
}public enum ShgbitPlayState {
Stopped = 0,
Paused = 1,
Running = 2,
}public enum ShgbitMediaStyle {
Others = 0,
Video = 1,
Picture = 2,
None = 9
}
}
基于此控件开发的视频播放窗口,应用在了NUOVA,Odin和Signage中:
VideoVS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OVNShare;
using System.IO;
using OVNShare.Helper;
using System.Threading;
using System.Diagnostics;
using log4net;
using WMPLib;namespace NuovaVSImpl {
[VS(Name = "视频", Order = 2)]
public partial class VideoVS : VisualSourceBase {
static readonly ILog logger = LogManager.GetLogger(typeof(VideoVS));
[VSDescription(
"视频文件列表", "内容设置")][VSDescription(
"基准时间", "内容设置")]roundtime
= videoFiles.Sum(p => p.Duration);其优点在于稳定,并且在循环播放等的处理上要优于WMP控件,缺点在于因为是基于.NET3.5开发的WinForm控件,因此在显示特效的处理上平淡无奇,无法做出绚丽的效果,已经无法满足高要求用户的需求;
3、基于.NET4.0和WPF技术的视频播放:
基于WPF和MediaElement来实现视频播放,不但稳定,而且能够显示华丽的特效处理,这在WPF初体验里已经有所展示;但那只是一个特效的展示,而我下一步要做的,则是通过WPF来封装一个稳定并且绚丽的视频播放控件……
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。