实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace LJ.RIA.MediaPlayer
{
public partial class Player : UserControl
{
#region 成员
private IList<PlayListItem> playList = null;
private Popup FullPopup = null;
private bool isListBoxShow = false;
private List<DateTime> mouseClickList = null;
#endregion
#region 属性
/// <summary>
/// 获取或设置一个值,是否单文件循环播放
/// </summary>
public bool IsReplay { get; set; }
/// <summary>
/// 获取或设置一个值,是否自动播放
/// </summary>
public bool IsAutoPaly
{
get
{
return this.mediaElement.AutoPlay;
}
set
{
this.mediaElement.AutoPlay = value;
}
}
/// <summary>
/// 播放列表
/// </summary>
public IList<PlayListItem> PlayList
{
get
{
return this.playList;
}
set
{
this.playList = value;
this.playListBox.PlayListSource = value;
}
}
#endregion
#region 构造函数
public Player()
{
// Required to initialize variables
InitializeComponent();
mouseClickList = new List<DateTime>();
this.mediaElement.MediaOpened = new RoutedEventHandler(mediaElement_MediaOpened);
this.mediaElement.MediaEnded = new RoutedEventHandler(mediaElement_MediaEnded);
this.mediaElement.CurrentStateChanged = new RoutedEventHandler(mediaElement_CurrentStateChanged);
this.mediaElement.BufferingProgressChanged = new RoutedEventHandler(mediaElement_BufferingProgressChanged);
this.mediaElement.MediaFailed = new EventHandler<ExceptionRoutedEventArgs>(mediaElement_MediaFailed);
CompositionTarget.Rendering = new EventHandler(CompositionTarget_Rendering);
this.playTools.ClickFullScreenButton = new Action<bool>(playTools_ClickFullScreenButton);
this.playTools.ClickPlayPauseStopButton = new Action<MediaElementState>(playTools_ClickPlayPauseStopButton);
this.playTools.ClickProcessBar = new Action<TimeSpan>(playTools_ClickProcessBar);
this.playTools.ChangeVolumeValue = new Action<double>(playTools_ChangeVolumeValue);
this.playTools.ClickListButton = new Action(playTools_ClickListButton);
this.playListBox.SelectPlayItemChanged = new Action<MediaPlayer.PlayListItem>(playListBox_SelectPlayItemChanged);
Application.Current.Host.Content.FullScreenChanged = new EventHandler(Content_FullScreenChanged);
this.mediaElement.MouseLeftButtonDown = new System.Windows.Input.MouseButtonEventHandler(mediaElement_MouseLeftButtonDown);
}
#endregion
#region 事件处理
void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
this.playTools.CurrentMessage = e.ErrorException.Message;
}
void mediaElement_BufferingProgressChanged(object sender, RoutedEventArgs e)
{
if (this.mediaElement.CurrentState == MediaElementState.Buffering)
{
this.playTools.CurrentMessage = this.mediaElement.CurrentState " " Math.Round(this.mediaElement.BufferingProgress * 100, 0).ToString() "%";
}
}
void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
this.playTools.CurrentState = this.mediaElement.CurrentState;
}
void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
this.mediaElement.Stop();
if (this.IsReplay)
{
this.mediaElement.Play();
}
}
void CompositionTarget_Rendering(object sender, EventArgs e)
{
int currentTime = (int)this.mediaElement.Position.TotalSeconds;
this.playTools.CurrentPlayTime = currentTime;
}
void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
this.playTools.TotaPlayTime = (int)this.mediaElement.NaturalDuration.TimeSpan.TotalSeconds;
}
void playTools_ChangeVolumeValue(double value)
{
this.mediaElement.Volume = value;
}
void playTools_ClickPlayPauseStopButton(MediaElementState state)
{
switch (state)
{
case MediaElementState.Playing:
this.mediaElement.Play();
break;
case MediaElementState.Paused:
this.mediaElement.Pause();
break;
case MediaElementState.Stopped:
this.mediaElement.Stop();
break;
}
}
void playTools_ClickProcessBar(TimeSpan time)
{
this.mediaElement.Position = time;
}
void playTools_ClickFullScreenButton(bool isScreenFull)
{
Application.Current.Host.Content.IsFullScreen = isScreenFull;
if (isScreenFull)
{
this.FullPopup = new Popup();
double width = Application.Current.Host.Content.ActualWidth;
double height = Application.Current.Host.Content.ActualHeight;
VideoFullPlayer videoFullPlayer = new VideoFullPlayer();
videoFullPlayer.FullScreenChange = new Action<bool>(videoFullPlayer_FullScreenChange);
videoFullPlayer.Width = width;
videoFullPlayer.Height = height;
videoFullPlayer.SetVideoBrush(this.mediaElement);
PlayTools playTools = this.playTools;
this.LayoutRoot.Children.Remove(this.playTools);
videoFullPlayer.CurrentPlayTools = playTools;
FullPopup.Child = videoFullPlayer;
FullPopup.IsOpen = true;
}
}
void videoFullPlayer_FullScreenChange(bool obj)
{
this.playTools.IsScreenFull = obj;
Application.Current.Host.Content.IsFullScreen = obj;
}
void Content_FullScreenChanged(object sender, EventArgs e)
{
if (!Application.Current.Host.Content.IsFullScreen)
{
if (this.FullPopup != null)
{
PlayTools playTools = (this.FullPopup.Child as VideoFullPlayer).CurrentPlayTools;
playTools.IsScreenFull = false;
this.LayoutRoot.Children.Add(playTools);
Grid.SetRow(playTools, 1);
this.FullPopup.IsOpen = false;
this.FullPopup = null;
}
}
}
void playTools_ClickListButton()
{
if (this.isListBoxShow)
{
this.isListBoxShow = false;
this.playListBox.Visibility = Visibility.Collapsed;
}
else
{
this.isListBoxShow = true;
this.playListBox.Visibility = Visibility.Visible;
}
}
void playListBox_SelectPlayItemChanged(PlayListItem listItem)
{
this.PlayListItem(listItem);
}
void mediaElement_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (mouseClickList.Count == 0)
{
mouseClickList.Add(DateTime.Now);
}
else
{
if (mouseClickList.Count == 1)
{
if (DateTime.Now.Subtract(mouseClickList[0]).TotalMilliseconds <= 300)
{
this.playTools.IsScreenFull = true;
playTools_ClickFullScreenButton(true);
}
mouseClickList.Clear();
}
}
}
#endregion
#region 公共方法
/// <summary>
/// 顺序播放列表文件
/// </summary>
public void Play()
{
if (this.playList != null && this.playList.Count > 0)
{
this.mediaElement.Source = this.playList[0].MediaSource;
this.playTools.CurrentPlayingMediaName = this.playList[0].DisplayName;
}
}
/// <summary>
/// 播放指定文件
/// </summary>
/// <param name="playListItem"></param>
public void Play(PlayListItem playListItem)
{
if (playListItem != null)
{
this.playList.Add(playListItem);
this.mediaElement.Source = playListItem.MediaSource;
this.playTools.CurrentPlayingMediaName = playListItem.DisplayName;
}
}
#endregion
#region 私有方法
private void PlayListItem(PlayListItem playListItem)
{
this.mediaElement.Source = playListItem.MediaSource;
this.mediaElement.Play();
this.playTools.CurrentPlayingMediaName = playListItem.DisplayName;
}
#endregion
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论