我在WPF Media Player中有问题[已解决] [英] I have a problem in WPF Media Player [SOLVED]

查看:127
本文介绍了我在WPF Media Player中有问题[已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个带有SeekBar的WPF媒体播放器.我有以下代码以寻求不同的位置.此代码已在Slider.ValueChanged事件下编写.

I have made a WPF Media Player that has SeekBar. I have following code to seek to different position. This code has been written under Slider.ValueChanged event.

mePlayerMain.Position = TimeSpan.FromSeconds(sliderSeekBar.Value);



我还具有计时器控件,以控制每个刻度中搜索栏的进度.我已经在MediaElement.MediaOpened
中编写了以下代码



I have also timer control to control the progress of the seek bar in each tick. I have written following code in MediaElement.MediaOpened

if (mePlayerMain.NaturalDuration.HasTimeSpan)
            {
                TimeSpan ts = new TimeSpan();
                ts = mePlayerMain.NaturalDuration.TimeSpan;
                sliderSeekBar.Maximum = ts.TotalSeconds;
                sliderSeekBar.SmallChange = 1;
            }
            timer.Start();


并为每个计时器滴答声更新搜索栏位置


and for each timer tick, I update the seekbar position

sliderSeekBar.Value = mePlayerMain.Position.TotalSeconds;



但是我的问题是,只要播放器上播放了媒体文件,媒体文件就会中断几秒钟,然后再次播放.一切正常,但媒体文件播放不流畅.请帮助我.



But my problem is whenever a media file is playing on the player, media file is disrupted for few seconds and then it plays again. Everythings fine but media file is not smoothly played. Please help me.

推荐答案

您快到了.

问题在于,每次计时器计时时,您都会更新滑块值,这会触发更改了滑块值的处理程序触发,从而设置视频上的位置,从而导致视频跳过.

在更新报价处理程序中的滑块值之前,请使用一个变量来抑制滑块值更改处理程序中的代码,如下所示:

You are almost there.

The problem is that everytime the timer ticks you update the slider value, which causes your slider value changed handler to fire, which sets the position on the video, causing it to skip.

Use a variable to suppress the code in the slider value changed handler before you update your slider value in the tick handler, like so:

bool suppressMediaPositionUpdate = false;

void TimerTickHandler(object sender, EventArgs e)
{
   suppressMediaPositionUpdate = true;
   sliderSeekBar.Value = mePlayerMain.Position.TotalSeconds;
}


void SliderValueChangedHandler(object sender, EventArgs e)
{
   if(suppressMediaPositionUpdate)
   {   
      suppressMediaPositionUpdate = false;
   }
   else
   {
      mePlayerMain.Position = TimeSpan.FromSeconds(sliderSeekBar.Value);
   }
}



-----

很抱歉,格式化是通过手机写的.
修复了格式:)



-----

Sorry about the formatting, wrote this from my phone.
fixed the formatting :)


这篇关于我在WPF Media Player中有问题[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆