You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

229 lines
7.9 KiB

using LibVLCSharp.Shared;
using LibVLCSharp.WPF;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace IOTContainer.Common
{
public class ProgrammeControl : UserControl
{
private VideoView _videoView;
private LibVLC _libVlc;
private MediaPlayer _mediaPlayer;
private Image _image;
private BitmapImage _bitmapImage;
private Grid _grid;
private MemoryStream _stream;
public ProgrammeControl()
{
try
{
Core.Initialize();
_grid = new Grid();
#region 视频
_libVlc = new LibVLC();
_mediaPlayer = new MediaPlayer(_libVlc);
_videoView = new VideoView();
_videoView.Loaded += (sender, e) => _videoView.MediaPlayer = _mediaPlayer;
_grid.Children.Add(_videoView);
#endregion
#region 图片
_image = new Image();
_grid.Children.Add(_image);
#endregion
this.AddChild(_grid);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("组件初始化出错" + ex.Message, "ProgrammeError");
}
}
/// <summary>
/// 清屏
/// </summary>
public void Flush()
{
try
{
this.Dispatcher.Invoke(() =>
{
_grid.Children.Clear();
_grid = new Grid();
_libVlc = new LibVLC();
_mediaPlayer = new MediaPlayer(_libVlc);
_videoView = new VideoView();
_videoView.Loaded += (sender, e) => _videoView.MediaPlayer = _mediaPlayer;
_grid.Children.Add(_videoView);
_image = new Image();
_grid.Children.Add(_image);
});
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("清屏出错" + ex.Message, "ProgrammeError");
}
}
/// <summary>
/// 封装动画方法
/// </summary>
/// <param name="uIElement">动画生效的界面元素</param>
/// <param name="autoReverse">是否倒序</param>
/// <param name="repeatBehavior">执行次数</param>
/// <param name="propertyPath">动画生效的依赖属性</param>
/// <param name="by"></param>
/// <returns></returns>
private Timeline CreateDoubleAnimation(UIElement uIElement, bool autoReverse, RepeatBehavior repeatBehavior, string propertyPath, double by)
{
DoubleAnimation doubleAnimation = new DoubleAnimation();
try
{
//doubleAnimation.By = by;
doubleAnimation.From = SystemParameters.WorkArea.Width;
doubleAnimation.To = 0;
doubleAnimation.Duration = TimeSpan.FromSeconds(10);
doubleAnimation.RepeatBehavior = repeatBehavior;
doubleAnimation.AutoReverse = autoReverse;
Storyboard.SetTarget(doubleAnimation, uIElement);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(propertyPath));
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("动画组件出错" + ex.Message, "ProgrammeError");
}
return doubleAnimation;
}
#region 媒体组件
/// <summary>
/// 播放视频
/// </summary>
/// <param name="filePath"></param>
public void PlayVideo(string filePath)
{
try
{
//Log.MyLog.WriteLogFile($"播放视频:{filePath}", "Programme");
if (!_videoView.MediaPlayer.IsPlaying)
{
using (var media = new Media(_libVlc, new Uri(filePath)))
{
_videoView.MediaPlayer.Play(media);
}
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("播放 视频 组件出错" + ex.Message, "ProgrammeError");
}
}
/// <summary>
/// 停止播放
/// </summary>
public void StopVideo()
{
try
{
_mediaPlayer.Stop();
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("停止 视频 组件出错" + ex.Message, "ProgrammeError");
}
}
/// <summary>
/// 播放gif
/// </summary>
/// <param name="filePath"></param>
public void PlayGif(string filePath)
{
try
{
//Log.MyLog.WriteLogFile($"播放gif:{filePath}", "Programme");
if (File.Exists(filePath))
{
_stream = new MemoryStream(File.ReadAllBytes(filePath));
XamlAnimatedGif.AnimationBehavior.SetSourceStream(_image, _stream);
//XamlAnimatedGif.AnimationBehavior.SetSourceUri(_image, new Uri(filePath));
XamlAnimatedGif.AnimationBehavior.SetRepeatBehavior(_image, RepeatBehavior.Forever);
XamlAnimatedGif.AnimationBehavior.SetAutoStart(_image, true);
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("播放 gif 组件出错" + ex.Message, "ProgrammeError");
}
}
/// <summary>
/// 播放图片
/// </summary>
/// <param name="filePath"></param>
public void PlayPic(string filePath)
{
try
{
//Log.MyLog.WriteLogFile($"播放图片:{filePath}", "Programme");
if (File.Exists(filePath))
{
using (var stream = new MemoryStream(File.ReadAllBytes(filePath)))
{
_bitmapImage = new BitmapImage();
_bitmapImage.BeginInit();
_bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
_bitmapImage.StreamSource = stream;
_bitmapImage.EndInit();
_bitmapImage.Freeze();
_image.Source = _bitmapImage;
}
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("播放 图片 组件出错" + ex.Message, "ProgrammeError");
}
}
/// <summary>
/// 停止图片
/// </summary>
public void StopPic()
{
try
{
_image.Source = null;
if (_stream != null)
{
_stream.Dispose();
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("停止 图片 组件出错" + ex.Message, "ProgrammeError");
}
}
///// <summary>
///// 播放字幕
///// </summary>
///// <param name="item"></param>
//public void PlaySubtitle(SubtitleModel item)
//{
// _label.Content = item.newsText;
// _label.FontSize = item.fontSize;
// _label.Foreground = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(item.fontColor));
// var storyboard = new Storyboard();
// storyboard.Children.Add(CreateDoubleAnimation(_label, false, RepeatBehavior.Forever, "(UIElement.RenderTransform).(TranslateTransform.X)", 1));
// storyboard.Begin();
//}
#endregion
}
}