我想要一个MediaElement应用程序。我想在指定的时间使用此应用程序暂停MediaElement。理论上,我应该使用2个DispatcherTimer。我使用DispatcherTimer暂停,但第二个计时器不运行。我的代码有问题吗?这是我的代码。谢谢inadvance
static DispatcherTimer dispatcherTimer, dispatcherTimer2;
private void MediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
if (((MediaElement)sender).CurrentState == MediaElementState.Playing)
{
tbMessage.Text = "playing";
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler<object>(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
dispatcherTimer2 = new DispatcherTimer();
dispatcherTimer2.Tick += dispatcherTimer2_Tick;
dispatcherTimer2.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer2.Start();
}
}
void dispatcherTimer2_Tick(object sender, object e)
{
tb2.Text = dispatcherTimer2.Interval.Seconds.ToString();
if (dispatcherTimer2.Interval.Seconds == 12)
{
mp.Play();
//dispatcherTimer.Start();
}
}
private void dispatcherTimer_Tick(object sender, object e)
{
// Updating the Label which displays the current second
tbMessage.Text = DateTime.Now.Second.ToString();
tbMessage.Text = mp.Position.Seconds.ToString() + " " + dispatcherTimer.Interval.Seconds.ToString();
if (mp.Position.Seconds == 3)
{
mp.Pause();
}
}
答案 0 :(得分:0)
dispatcherTimer2.Interval.Seconds
是时间间隔之间的时间 因为这是1秒而不是12,所以if语句将始终为false
你可以试试这个:
int ticks = 0;
void dispatcherTimer2_Tick(object sender, object e)
{
ticks++;
tb2.Text = ticks.ToString();
if (ticks == 12)
{
mp.Play();
//dispatcherTimer.Start();
ticks = 0;
}
}
或类似的东西
答案 1 :(得分:0)
我用以下解决方案解决了我的问题。也许它可以归于某人。
DispatcherTimer dispatcherTimer = new DispatcherTimer();
private void Page_Loaded(object sender, RoutedEventArgs e)
{
dispatcherTimer.Tick += new EventHandler<object>(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
int QuestionCount = AppConfiguration.QuestionList.allsecs.Split('-').Length;
QuestionTimes = new int[QuestionCount];
for (int i = 0; i < QuestionTimes.Length; i++)
{
if (AppConfiguration.QuestionList.allsecs.Split('-')[i] != null && AppConfiguration.QuestionList.allsecs.Split('-')[i] != "")
{
QuestionTimes[i] = Convert.ToInt32(AppConfiguration.QuestionList.allsecs.Split('-')[i]);
}
}
string QuestionVideoLink = AppConfiguration.QuestionList.VideoLink;
VideoPlayer.Source = new Uri(AppConfiguration.TestVideoLink + QuestionVideoLink, UriKind.Absolute);
}
static bool flag = false;
private void dispatcherTimer_Tick(object sender, object e)
{
count++;
tbMessage.Text = count.ToString();
//tbMessage.Text = DateTime.Now.Second.ToString();
//tbMessage.Text = mp.Position.Seconds.ToString() + " " + dispatcherTimer.Interval.Seconds.ToString();
if (sn.Contains(mp.Position.Seconds) && !flag)
{
mp.Pause();
dispatcherTimer.Stop();
tbDenemeSoru.Text = "Deneme Soru 1";
tbMessage.Text = "Video Durdu";
pnlProgressBar.Visibility = Windows.UI.Xaml.Visibility.Visible;
count = 0;
}
if (sn.Contains(mp.Position.Seconds) && !flag)
{
mp.Pause();
dispatcherTimer.Stop();
tbDenemeSoru.Text = "Deneme Soru 2";
tbMessage.Text = "Video Durdu";
pnlProgressBar.Visibility = Windows.UI.Xaml.Visibility.Visible;
count = 0;
}
if (mp.Position.Seconds > 3 && count == 0)
{
flag = true;
}
// Updating the Label which displays the current second
// Forcing the CommandManager to raise the RequerySuggested event
//CommandManager.InvalidateRequerySuggested();
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
pnlProgressBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
mp.Play();
dispatcherTimer.Start();
flag = false;
}