wp7 -MediaElement播放
namespace TestControl.Test_Control
{
public partial class Control_Slide : PhoneApplicationPage
{
public Control_Slide()
{
InitializeComponent();
//定义多媒体流可用并被打开时触发的事件
myMediaElement.MediaOpened += new RoutedEventHandler(myMediaElement_MediaOpened);
//定义多媒体停止时触发的事件
myMediaElement.MediaEnded += new RoutedEventHandler(myMediaElement_MediaEnded);
//定义多媒体播放状态改变时触发的事件
myMediaElement.CurrentStateChanged += new RoutedEventHandler(myMediaElement_CurrentStateChanged);
//设置多媒体控件的网络视频资源
myMediaElement.Source = new Uri("Hair.mp3", UriKind.Relative);
DispatcherTimer tmr = new DispatcherTimer();//创建计时器
tmr.Interval = TimeSpan.FromSeconds(1);//TimeSpan 结构 表示一个时间间隔。
tmr.Tick += OnTimerTick;//DispatcherTimer.Tick 事件 超过计时器间隔时发生。
tmr.Start();//计时器开始
}
void OnTimerTick(object sender, EventArgs e)
{
UpdateClock();
}
void UpdateClock()
{
slider1.Value = (int)myMediaElement.Position.TotalSeconds;
}
private bool _sliderpressed = false;
void videoMediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
//StopTimer();
slider1.Value = 0.0;
}
//视频状态改变时的处理事件
void myMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
if (myMediaElement.CurrentState == MediaElementState.Playing)
{
if (!_sliderpressed)
{
//_timer.Stop();
}
else
{
//_timer.Start();
}
}
if (myMediaElement.CurrentState == MediaElementState.Paused)
{
//_timer.Stop();
}
if (myMediaElement.CurrentState == MediaElementState.Stopped)
{
//_timer.Stop();
slider1.Value = 0;
}
}
//多媒体停止时触发的事件
void myMediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
_sliderpressed = false;
//停止播放
myMediaElement.Stop();
}
//多媒体流可用并被打开时触发的事件
void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
slider1.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalSeconds;
_sliderpressed = true;
}
private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
textblock.Text=slider1.Value.ToString();
}
//定义获取时间函数,返回值为字符串
public string getTime(int t)
{
//计算小时,用毫秒总数除以(1000*60*24),后去掉小数点
int hour = t / (1000 * 60 * 24);
//计算分钟,用毫秒总数减去小时乘以(1000*60*24)后,除以(1000*60),再去掉小数点
int min = (t - hour * (1000 * 60 * 24)) / (1000 * 60);
//同上
int sec = (t - hour * (1000 * 60 * 24) - min * (1000 * 60)) / 1000;
int msec = t - hour * (1000 * 60 * 24) - min * (1000 * 60) - sec * 1000;
//拼接字符串
string timeString = hour.ToString() + ":" + min.ToString() + ":" + sec.ToString() + ":" + msec.ToString();
return timeString;
}
private double SliderFrequency(TimeSpan timevalue)
{
double stepfrequency = -1;
double absvalue = (int)Math.Round(timevalue.TotalSeconds, 1);
stepfrequency = (int)(Math.Round(absvalue / 100));
if (timevalue.TotalMinutes >= 10 && timevalue.TotalMinutes < 30)
{
stepfrequency = 10;
}
else if (timevalue.TotalMinutes >= 30 && timevalue.TotalMinutes < 60)
{
stepfrequency = 30;
}
else if (timevalue.TotalHours >= 1)
{
stepfrequency = 60;
}
if (stepfrequency == 0) stepfrequency += 1;
if (stepfrequency == 1)
{
stepfrequency = absvalue / 100;
}
return stepfrequency;
}
//播放视频菜单事件
private void Play_Click(object sender, EventArgs e)
{
myMediaElement.Play();
}
//暂停视频菜单事件
private void Pause_Click(object sender, EventArgs e)
{
myMediaElement.Pause();
}
//停止视频菜单事件
private void Stop_Click(object sender, EventArgs e)
{
myMediaElement.Stop();
}
}
}