代码不好,大家拍砖轻点啊~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Interop;
using System.Windows.Threading;
using System.ComponentModel;
using System.Collections;
namespace PlayerSite
{
public partial class Player : UserControl
{
bool trackingMouseMove = false;
Point mousePosition = new Point();
private TimeSpan timeDuration;
private DispatcherTimer timer = new DispatcherTimer();
public List<string> MusicList
{
get;
set;
}
private int index=0;
public int CurrentIndex
{
set
{
if (value < 0)
{
index = 0;
}
else if (value > MusicList.Count - 1)
{
index = MusicList.Count - 1;
}
else
{
index = value;
}
}
get
{
return index;
}
}
public Player()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Player_Loaded);
MediaPlayer.MediaOpened += new RoutedEventHandler(MediaPlayer_MediaOpened);
MediaPlayer.MediaEnded += new RoutedEventHandler(MediaPlayer_MediaEnded);
}
void MediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
++CurrentIndex;
MediaPlayer.Source = new Uri(MusicList[CurrentIndex]);
MediaPlayer.Play();
}
void MediaPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
timeDuration = MediaPlayer.NaturalDuration.HasTimeSpan ? MediaPlayer.NaturalDuration.TimeSpan : TimeSpan.FromMilliseconds(1);
}
void Player_Loaded(object sender, RoutedEventArgs e)
{
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
if (MusicList.Count > 0 && MediaPlayer.AutoPlay)
{
PlaySymbol_Hide.Begin();
PauseSymbol_Show.Begin();
MediaPlayer.Source = new Uri(MusicList[0]);
}
else
{
MediaPlayer.Stop();
PauseSymbol_Hide.Begin();
PlaySymbol_Show.Begin();
}
}
void timer_Tick(object sender, EventArgs e)
{
if (MediaPlayer.CurrentState == MediaElementState.Playing)
{
CurrentTimeText.Text = string.Format("{0:00}:{1:00}", MediaPlayer.Position.Minutes, MediaPlayer.Position.Seconds);
double value = (MediaPlayer.Position.TotalSeconds / timeDuration.TotalSeconds) * TimeSliderDecoration.Width;
FrameworkElement element = TimeThumb as FrameworkElement;
if (element != null)
{
element.SetValue(Canvas.LeftProperty, value);
}
}
}
private void PlayerControlsMouseEnter(object sender, MouseEventArgs e)
{
PlayerControls_MouseEnter.Begin();
}
private void PlayerControlsMouseLeave(object sender, MouseEventArgs e)
{
PlayerControls_MouseLeave.Begin();
}
private void PlayPauseButtonMouseEnter(object sender, MouseEventArgs e)
{
PlayPauseButton_MouseEnter.Begin();
}
private void PlayPauseButtonMouseLeave(object sender, MouseEventArgs e)
{
PlayPauseButton_MouseLeave.Begin();
}
private void PreviousButtonMouseEnter(object sender, MouseEventArgs e)
{
PreviousButton_MouseEnter.Begin();
}
private void PreviousButtonMouseLeave(object sender, MouseEventArgs e)
{
PreviousButton_MouseLeave.Begin();
}
private void PreviousButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
--CurrentIndex;
MediaPlayer.Source = new Uri(MusicList[CurrentIndex]);
if (MediaPlayer.CurrentState == MediaElementState.Paused)
{
MediaPlayer.Pause();
}
else if (MediaPlayer.CurrentState == MediaElementState.Stopped)
{
MediaPlayer.Stop();
}
}
private void NextButtonMouseEnter(object sender, MouseEventArgs e)
{
NextButton_MouseEnter.Begin();
}
private void NextButtonMouseLeave(object sender, MouseEventArgs e)
{
NextButton_MouseLeave.Begin();
}
private void NextButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
++CurrentIndex;
MediaPlayer.Source = new Uri(MusicList[CurrentIndex]);
if (MediaPlayer.CurrentState==MediaElementState.Paused)
{
MediaPlayer.Pause();
}
else if (MediaPlayer.CurrentState == MediaElementState.Stopped)
{
MediaPlayer.Stop();
}
}
private void StopButton_MouseEnter(object sender, MouseEventArgs e)
{
StopButtonMouseEnter.Begin();
}
private void StopButton_MouseLeave(object sender, MouseEventArgs e)
{
StopButtonMouseLeave.Begin();
}
private void StopButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MediaPlayer.Stop();
FrameworkElement element = TimeThumb as FrameworkElement;
if (element != null)
{
element.SetValue(Canvas.LeftProperty,(double)0);
}
PauseSymbol_Hide.Begin();
PlaySymbol_Show.Begin();
}
private void PlayPauseSymbolMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (MediaPlayer.CurrentState == MediaElementState.Paused || MediaPlayer.CurrentState==MediaElementState.Stopped)
{
PlaySymbol_Hide.Begin();
PauseSymbol_Show.Begin();
MediaPlayer.Play();
}
else
{
PauseSymbol_Hide.Begin();
PlaySymbol_Show.Begin();
MediaPlayer.Pause();
}
}
private void MuteButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (waves_icon.Opacity == 0)
{
MuteOffSymbol_Show.Begin();
MediaPlayer.IsMuted = false;
}
else
{
MuteOffSymbol_Hide.Begin();
MediaPlayer.IsMuted = true;
}
}
private void VolumeThumb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
mousePosition = e.GetPosition(null);
if (null != element)
{
trackingMouseMove = true;
element.CaptureMouse();
element.Cursor = Cursors.Hand;
}
}
private void VolumeThumb_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (trackingMouseMove)
{
double deltaH = e.GetPosition(null).X - mousePosition.X;
double newLeft = deltaH + (double)element.GetValue(Canvas.LeftProperty);
if (newLeft < 0)
{
newLeft = 0;
}
else if (newLeft > VolumeSliderDecoration.Width)
{
newLeft = VolumeSliderDecoration.Width;
}
MediaPlayer.Volume = (newLeft / 70);
element.SetValue(Canvas.LeftProperty, newLeft);
//CurrentTimeText.Text = ((int)newLeft).ToString()+":"+MediaPlayer.Volume.ToString();
mousePosition = e.GetPosition(null);
}
}
private void VolumeThumb_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = false;
element.ReleaseMouseCapture();
mousePosition.X = mousePosition.Y = 0;
element.Cursor = null;
}
private void TimeSliderDecoration_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
private void TimeThumb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
timer.Stop();
FrameworkElement element = sender as FrameworkElement;
mousePosition = e.GetPosition(null);
if (null != element)
{
trackingMouseMove = true;
element.CaptureMouse();
element.Cursor = Cursors.Hand;
}
}
private void TimeThumb_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (trackingMouseMove)
{
double deltaH = e.GetPosition(null).X - mousePosition.X;
double newLeft =deltaH + (double)element.GetValue(Canvas.LeftProperty);
if (newLeft < 0)
{
newLeft = 0;
}
else if (newLeft > TimeSliderDecoration.Width)
{
newLeft = TimeSliderDecoration.Width;
}
element.SetValue(Canvas.LeftProperty, newLeft);
mousePosition = e.GetPosition(null);
}
}
private void TimeThumb_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
FrameworkElement thumb = TimeThumb as FrameworkElement;
element.ReleaseMouseCapture();
mousePosition.X = mousePosition.Y = 0;
element.Cursor = null;
double value=0;
if (thumb != null)
{
value = (double)thumb.GetValue(Canvas.LeftProperty);
}
trackingMouseMove = false;
if (MediaPlayer.CanSeek)
{
MediaPlayer.Pause();
MediaPlayer.Position = TimeSpan.FromSeconds(timeDuration.TotalSeconds *value / TimeSliderDecoration.Width);
MediaPlayer.Play();
}
timer.Start();
}
}
}
在这里下载源代码 源代码