在特定时间启动和停止 MediaElement

发布于 2024-12-03 10:00:28 字数 405 浏览 1 评论 0原文

我正在将应用程序更改为 WPF,它根据定时事件播放音频、视频和图片。我使用了 "string MciCommand = string.Format("播放帧从 {0} 到 {1} {2}"" ,之前

我一直在搜索并尝试解决这个问题,现在在 WPF c# 中

  1. 我有一个视频(wmv)并且我有多个部分需要一起播放。 示例..section1开始(200ms)停止(250ms)section2开始(5000ms)停止(15000ms)
  2. 最后我需要它在静止帧上暂停..

我尝试使用计时器和空的while循环,但它没有正确寻求。 我很困惑媒体元素似乎不支持这种类型的使用。我想到了 wpfmediakit 和 direct show 很难掌握我的 wpf 新手技能。

任何帮助将不胜感激...

I am change an app over to WPF, it plays audio and video and pictures all based on timed events .. I used "string MciCommand = string.Format("play frames FROM {0} to {1} {2}"" , before.

I have been searching and trying to fix this problem now in WPF c#

  1. I have a video (wmv) and I have multiple sections that need to be played together.
    example .. section1 start(200ms) stop(250ms) section2 start(5000ms) stop(15000ms)
  2. finally I need it to pause on a a still frame ..

I have tried using a timer, and a empty while loop and it doesn't seek properly.
I am at a loss media element doesn't seem to support this type of use. I thought about wpfmediakit and direct show is very hard to wrap my novice wpf skills around.

any help would be greatly appreciated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-12-10 10:00:28

这就是我最终解决问题的方式。我使用了一个属性列表,并将其用作循环的脚本。如果仓位大于结束时间。它触发计时器并转到列表中的下一个项目..仍然有一些事情可以做得更精致..比如textbox4更改事件也会触发next_item但它完成了工作..现在..
希望这可以帮助遇到同样问题的人...

在此处输入图像描述

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Media.Animation;
    using System.ComponentModel;
    using System.Threading;
    namespace WpfApplication6
    {
        public partial class MainWindow : Window
        {
            BackgroundWorker position = new BackgroundWorker();
            BackgroundWorker test_position = new BackgroundWorker();
            public List<video_script> script_list = new List<video_script>();
            int scrip_index;
    public class video_script
        {
        public string action { get; set; }
        public TimeSpan start_time { get; set; }
        public TimeSpan endtime { get; set; }
        public string filename { get; set; }

    }
    private void position_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(100);
    }
    private void position_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      if (mediaElement1.Position > TimeSpan.FromMilliseconds(Convert.ToInt32(tb_endtime.Text)))
        {
            next_item();
        }
        else position.RunWorkerAsync();
        textBox4.Text = "Play";
        textBox3.Text = mediaElement1.Position.ToString();
    }
    public MainWindow()
    {
        InitializeComponent();
        position.DoWork += new DoWorkEventHandler(position_DoWork);
        position.RunWorkerCompleted += new RunWorkerCompletedEventHandler(position_RunWorkerCompleted);
        position.WorkerSupportsCancellation = true;
        test_position.DoWork += new DoWorkEventHandler(test_position_DoWork);
        test_position.RunWorkerCompleted += new RunWorkerCompletedEventHandler(test_position_RunWorkerCompleted);
        test_position.WorkerSupportsCancellation = true;
    }

    private void Mediasource_Click(object sender, RoutedEventArgs e)
    {
        if (!position.IsBusy) position.RunWorkerAsync();
        mediaElement1.Source = new Uri(tb_filename.Text);
        mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.ScrubbingEnabled = true;
        mediaElement1.Play();
    }
    private void stopbutton_Click(object sender, RoutedEventArgs e)
    {
        mediaElement1.Stop();
    }

    private void Playbutton_Click(object sender, RoutedEventArgs e)
    {
        scrip_index = 0;
        mediaElement1.Play();
        mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.ScrubbingEnabled = true;
    }







    private void pausebutton_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement1.CanPause)
        {
            mediaElement1.Pause();
        }
    }


    private void AddToListbutton_Click(object sender, RoutedEventArgs e)
    {
        video_script temp_item = new video_script();
        temp_item.filename = tb_filename.Text;
        temp_item.start_time = TimeSpan.FromMilliseconds(Convert.ToInt32(tb_starttime.Text));
        temp_item.endtime = TimeSpan.FromMilliseconds(Convert.ToInt32(tb_endtime.Text));
        temp_item.action = tb_action.Text;

        script_list.Add(temp_item);

        listBox1.Items.Add(temp_item.filename + " | " + tb_starttime.Text + " | " + tb_endtime.Text + " | " + tb_action.Text);
    }





    private void positionbox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (script_list.Count != 0)
        {
            if (script_list[scrip_index].endtime < mediaElement1.Position) next_item();
        }
    }



    #region test button area
    private void next_item()
    {
        if (scrip_index < script_list.Count() - 1)
        {
            scrip_index++; 




            switch (script_list[scrip_index].action)
            {
                case "Load":

              mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
              mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
              if (mediaElement1.Source != new Uri(script_list[scrip_index].filename)) mediaElement1.Source = new Uri(script_list[scrip_index].filename);
              mediaElement1.ScrubbingEnabled = true;
              playing = false;
                    next_item();
                    break;

                case "Play":
                    mediaElement1.Play();
                    playing = true;
                    if(!test_position.IsBusy) test_position.RunWorkerAsync();

                    break;
                case "Pause":
                    mediaElement1.Pause();
                    playing = false;
                    break;
                case "Seek":
                    mediaElement1.Position = script_list[scrip_index].start_time;
                    playing = true;
                    break;
                case "Stop":
                    mediaElement1.Stop();
                    playing = false;
                    break;
            }

        }
    }


    private void test_position_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(100);
    }
    private void test_position_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {



        if (mediaElement1.Position > script_list[scrip_index].endtime )
        {
            next_item();
        }
        else test_position.RunWorkerAsync();

        textBox4.Text = "Play";
        textBox3.Text = mediaElement1.Position.ToString();
        if (playing && !test_position.IsBusy) test_position.RunWorkerAsync();

    }
    private void testbutton_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement1.Source != new Uri(tb_filename.Text)) mediaElement1.Source = new Uri(tb_filename.Text);
                    mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.Play();

        mediaElement1.ScrubbingEnabled = true;
        mediaElement1.Position = TimeSpan.FromMilliseconds(Convert.ToInt32(tb_starttime.Text));
        if (test_position.IsBusy) test_position.CancelAsync();

        if (!test_position.IsBusy) test_position.RunWorkerAsync();
    }
    bool playing;
    #endregion
    #region slider region

    private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (slider1.Value > slider2.Value) slider2.Value = slider1.Value - 1;
    }
    private void slidermax_values_TextChanged(object sender, TextChangedEventArgs e)
    {
        slider1.Maximum = Convert.ToInt32(tb_slider_maxvalue.Text);
        slider2.Maximum = Convert.ToInt32(tb_slider_maxvalue.Text);
    }
    private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (slider2.Value < slider1.Value) slider1.Value = slider2.Value - 1;
    }
    #endregion

    private void start_script_Click(object sender, RoutedEventArgs e)
    {
        scrip_index = -1;
        next_item(); 
    }

    private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
    {

    }



   }
}

这是我使用的表单的 Xaml测试....

    <Window x:Class="WpfApplication6.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="402" Width="922"   WindowStyle="ThreeDBorderWindow">
    <Grid>
    <Button Content="mediasource load" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="Mediasource_Click" />
    <Button Content="play" Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="Playbutton_Click" />
    <Button Content="stop" Height="23" HorizontalAlignment="Left" Margin="10,142,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="stopbutton_Click" />
    <Button Content="Pause" Height="23" HorizontalAlignment="Left" Margin="12,171,0,0" Name="button6" VerticalAlignment="Top" Width="75" Click="pausebutton_Click" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="165,296,0,0" Name="tb_starttime" VerticalAlignment="Top" Width="120"   Text="{Binding ElementName=slider1, Path=Value, Mode=TwoWay}" />
    <TextBox Height="23" HorizontalAlignment="Right" Margin="0,296,426,0" Name="tb_endtime" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=slider2, Path=Value, Mode=TwoWay}" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="630,281,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" TextChanged="positionbox_TextChanged" IsReadOnly="False" Text="{Binding ElementName=mediaElement1, Path=Position.Milliseconds, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="490,281,0,0" Name="textBox4" VerticalAlignment="Top" Width="134" />
    <Label Content="filename" Height="28" HorizontalAlignment="Left" Margin="104,325,0,0" Name="label1" VerticalAlignment="Top" />
    <ListBox Height="215" HorizontalAlignment="Left" Margin="469,10,0,0" Name="listBox1" VerticalAlignment="Top" Width="431" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="165,325,0,0" Name="tb_filename" VerticalAlignment="Top" Width="213" Text="C:\Data\001femod1\vid\dat1.avi" AcceptsTab="False" />
    <Label Content="end time" Height="28" HorizontalAlignment="Left" Margin="291,294,0,0" Name="label2" VerticalAlignment="Top" />
    <Label Content="start time" Height="28" HorizontalAlignment="Left" Margin="104,296,0,0" Name="label3" VerticalAlignment="Top" />
    <Button Content="add to list" Height="23" HorizontalAlignment="Left" Margin="11,315,0,0" Name="button7" VerticalAlignment="Top" Width="75" Click="AddToListbutton_Click" />
    <Label Content="Action" Height="28" HorizontalAlignment="Left" Margin="384,325,0,0" Name="label4" VerticalAlignment="Top" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="434,326,0,0" Name="tb_action" VerticalAlignment="Top" Width="120">
        <ComboBoxItem Content="Play" />
        <ComboBoxItem Content="Load" />
        <ComboBoxItem Content="Stop" />
        <ComboBoxItem Content="Pause" />
        <ComboBoxItem Content="Seek" />
    </ComboBox>
    <Button Content="test times" Height="23" HorizontalAlignment="Left" Margin="50,246,0,0" Name="tb_test" VerticalAlignment="Top" Width="75" Click="testbutton_Click" />
    <Slider Height="23" Margin="147,231,170,0" Name="slider1" VerticalAlignment="Top" SmallChange="1" LargeChange="10" IsSelectionRangeEnabled="False" IsMoveToPointEnabled="True" IsSnapToTickEnabled="True" ValueChanged="slider1_ValueChanged" />
    <Slider Height="23" HorizontalAlignment="Left" Margin="147,260,0,0" Name="slider2" VerticalAlignment="Top" Width="583" SmallChange="1" IsSnapToTickEnabled="True" ValueChanged="slider2_ValueChanged" />
    <Label Content="max value" Height="28" HorizontalAlignment="Left" Margin="736,226,0,0" Name="label5" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="736,247,0,0" Name="tb_slider_maxvalue" VerticalAlignment="Top" Width="120" Text="74000" TextChanged="slidermax_values_TextChanged" />
    <Button Content="start script" Height="23" HorizontalAlignment="Left" Margin="10,281,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="start_script_Click" />
    <MediaElement Height="215" HorizontalAlignment="Left" Margin="91,10,0,0" Name="mediaElement1" VerticalAlignment="Top" Width="372" MediaOpened="mediaElement1_MediaOpened" />
    </Grid>
    </Window>

this was how I ended up solving my problem .. I used a list of properties and used it like a script that I loop through . and if the position in larger than the end time. it trigger the timer and goes to the next item in the list .. there is still some things that could be made a bit more refined .. like the textbox4 changed event also triggers next_item but it gets the job done .. for now ..
Hopefully this helps someone with the same issue ...

enter image description here

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Media.Animation;
    using System.ComponentModel;
    using System.Threading;
    namespace WpfApplication6
    {
        public partial class MainWindow : Window
        {
            BackgroundWorker position = new BackgroundWorker();
            BackgroundWorker test_position = new BackgroundWorker();
            public List<video_script> script_list = new List<video_script>();
            int scrip_index;
    public class video_script
        {
        public string action { get; set; }
        public TimeSpan start_time { get; set; }
        public TimeSpan endtime { get; set; }
        public string filename { get; set; }

    }
    private void position_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(100);
    }
    private void position_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      if (mediaElement1.Position > TimeSpan.FromMilliseconds(Convert.ToInt32(tb_endtime.Text)))
        {
            next_item();
        }
        else position.RunWorkerAsync();
        textBox4.Text = "Play";
        textBox3.Text = mediaElement1.Position.ToString();
    }
    public MainWindow()
    {
        InitializeComponent();
        position.DoWork += new DoWorkEventHandler(position_DoWork);
        position.RunWorkerCompleted += new RunWorkerCompletedEventHandler(position_RunWorkerCompleted);
        position.WorkerSupportsCancellation = true;
        test_position.DoWork += new DoWorkEventHandler(test_position_DoWork);
        test_position.RunWorkerCompleted += new RunWorkerCompletedEventHandler(test_position_RunWorkerCompleted);
        test_position.WorkerSupportsCancellation = true;
    }

    private void Mediasource_Click(object sender, RoutedEventArgs e)
    {
        if (!position.IsBusy) position.RunWorkerAsync();
        mediaElement1.Source = new Uri(tb_filename.Text);
        mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.ScrubbingEnabled = true;
        mediaElement1.Play();
    }
    private void stopbutton_Click(object sender, RoutedEventArgs e)
    {
        mediaElement1.Stop();
    }

    private void Playbutton_Click(object sender, RoutedEventArgs e)
    {
        scrip_index = 0;
        mediaElement1.Play();
        mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.ScrubbingEnabled = true;
    }







    private void pausebutton_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement1.CanPause)
        {
            mediaElement1.Pause();
        }
    }


    private void AddToListbutton_Click(object sender, RoutedEventArgs e)
    {
        video_script temp_item = new video_script();
        temp_item.filename = tb_filename.Text;
        temp_item.start_time = TimeSpan.FromMilliseconds(Convert.ToInt32(tb_starttime.Text));
        temp_item.endtime = TimeSpan.FromMilliseconds(Convert.ToInt32(tb_endtime.Text));
        temp_item.action = tb_action.Text;

        script_list.Add(temp_item);

        listBox1.Items.Add(temp_item.filename + " | " + tb_starttime.Text + " | " + tb_endtime.Text + " | " + tb_action.Text);
    }





    private void positionbox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (script_list.Count != 0)
        {
            if (script_list[scrip_index].endtime < mediaElement1.Position) next_item();
        }
    }



    #region test button area
    private void next_item()
    {
        if (scrip_index < script_list.Count() - 1)
        {
            scrip_index++; 




            switch (script_list[scrip_index].action)
            {
                case "Load":

              mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
              mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
              if (mediaElement1.Source != new Uri(script_list[scrip_index].filename)) mediaElement1.Source = new Uri(script_list[scrip_index].filename);
              mediaElement1.ScrubbingEnabled = true;
              playing = false;
                    next_item();
                    break;

                case "Play":
                    mediaElement1.Play();
                    playing = true;
                    if(!test_position.IsBusy) test_position.RunWorkerAsync();

                    break;
                case "Pause":
                    mediaElement1.Pause();
                    playing = false;
                    break;
                case "Seek":
                    mediaElement1.Position = script_list[scrip_index].start_time;
                    playing = true;
                    break;
                case "Stop":
                    mediaElement1.Stop();
                    playing = false;
                    break;
            }

        }
    }


    private void test_position_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(100);
    }
    private void test_position_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {



        if (mediaElement1.Position > script_list[scrip_index].endtime )
        {
            next_item();
        }
        else test_position.RunWorkerAsync();

        textBox4.Text = "Play";
        textBox3.Text = mediaElement1.Position.ToString();
        if (playing && !test_position.IsBusy) test_position.RunWorkerAsync();

    }
    private void testbutton_Click(object sender, RoutedEventArgs e)
    {
        if (mediaElement1.Source != new Uri(tb_filename.Text)) mediaElement1.Source = new Uri(tb_filename.Text);
                    mediaElement1.LoadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.UnloadedBehavior = System.Windows.Controls.MediaState.Manual;
        mediaElement1.Play();

        mediaElement1.ScrubbingEnabled = true;
        mediaElement1.Position = TimeSpan.FromMilliseconds(Convert.ToInt32(tb_starttime.Text));
        if (test_position.IsBusy) test_position.CancelAsync();

        if (!test_position.IsBusy) test_position.RunWorkerAsync();
    }
    bool playing;
    #endregion
    #region slider region

    private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (slider1.Value > slider2.Value) slider2.Value = slider1.Value - 1;
    }
    private void slidermax_values_TextChanged(object sender, TextChangedEventArgs e)
    {
        slider1.Maximum = Convert.ToInt32(tb_slider_maxvalue.Text);
        slider2.Maximum = Convert.ToInt32(tb_slider_maxvalue.Text);
    }
    private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (slider2.Value < slider1.Value) slider1.Value = slider2.Value - 1;
    }
    #endregion

    private void start_script_Click(object sender, RoutedEventArgs e)
    {
        scrip_index = -1;
        next_item(); 
    }

    private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
    {

    }



   }
}

here is the Xaml for the form that I used for testing....

    <Window x:Class="WpfApplication6.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="402" Width="922"   WindowStyle="ThreeDBorderWindow">
    <Grid>
    <Button Content="mediasource load" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="Mediasource_Click" />
    <Button Content="play" Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="Playbutton_Click" />
    <Button Content="stop" Height="23" HorizontalAlignment="Left" Margin="10,142,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="stopbutton_Click" />
    <Button Content="Pause" Height="23" HorizontalAlignment="Left" Margin="12,171,0,0" Name="button6" VerticalAlignment="Top" Width="75" Click="pausebutton_Click" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="165,296,0,0" Name="tb_starttime" VerticalAlignment="Top" Width="120"   Text="{Binding ElementName=slider1, Path=Value, Mode=TwoWay}" />
    <TextBox Height="23" HorizontalAlignment="Right" Margin="0,296,426,0" Name="tb_endtime" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=slider2, Path=Value, Mode=TwoWay}" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="630,281,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" TextChanged="positionbox_TextChanged" IsReadOnly="False" Text="{Binding ElementName=mediaElement1, Path=Position.Milliseconds, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="490,281,0,0" Name="textBox4" VerticalAlignment="Top" Width="134" />
    <Label Content="filename" Height="28" HorizontalAlignment="Left" Margin="104,325,0,0" Name="label1" VerticalAlignment="Top" />
    <ListBox Height="215" HorizontalAlignment="Left" Margin="469,10,0,0" Name="listBox1" VerticalAlignment="Top" Width="431" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="165,325,0,0" Name="tb_filename" VerticalAlignment="Top" Width="213" Text="C:\Data\001femod1\vid\dat1.avi" AcceptsTab="False" />
    <Label Content="end time" Height="28" HorizontalAlignment="Left" Margin="291,294,0,0" Name="label2" VerticalAlignment="Top" />
    <Label Content="start time" Height="28" HorizontalAlignment="Left" Margin="104,296,0,0" Name="label3" VerticalAlignment="Top" />
    <Button Content="add to list" Height="23" HorizontalAlignment="Left" Margin="11,315,0,0" Name="button7" VerticalAlignment="Top" Width="75" Click="AddToListbutton_Click" />
    <Label Content="Action" Height="28" HorizontalAlignment="Left" Margin="384,325,0,0" Name="label4" VerticalAlignment="Top" />
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="434,326,0,0" Name="tb_action" VerticalAlignment="Top" Width="120">
        <ComboBoxItem Content="Play" />
        <ComboBoxItem Content="Load" />
        <ComboBoxItem Content="Stop" />
        <ComboBoxItem Content="Pause" />
        <ComboBoxItem Content="Seek" />
    </ComboBox>
    <Button Content="test times" Height="23" HorizontalAlignment="Left" Margin="50,246,0,0" Name="tb_test" VerticalAlignment="Top" Width="75" Click="testbutton_Click" />
    <Slider Height="23" Margin="147,231,170,0" Name="slider1" VerticalAlignment="Top" SmallChange="1" LargeChange="10" IsSelectionRangeEnabled="False" IsMoveToPointEnabled="True" IsSnapToTickEnabled="True" ValueChanged="slider1_ValueChanged" />
    <Slider Height="23" HorizontalAlignment="Left" Margin="147,260,0,0" Name="slider2" VerticalAlignment="Top" Width="583" SmallChange="1" IsSnapToTickEnabled="True" ValueChanged="slider2_ValueChanged" />
    <Label Content="max value" Height="28" HorizontalAlignment="Left" Margin="736,226,0,0" Name="label5" VerticalAlignment="Top" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="736,247,0,0" Name="tb_slider_maxvalue" VerticalAlignment="Top" Width="120" Text="74000" TextChanged="slidermax_values_TextChanged" />
    <Button Content="start script" Height="23" HorizontalAlignment="Left" Margin="10,281,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="start_script_Click" />
    <MediaElement Height="215" HorizontalAlignment="Left" Margin="91,10,0,0" Name="mediaElement1" VerticalAlignment="Top" Width="372" MediaOpened="mediaElement1_MediaOpened" />
    </Grid>
    </Window>
微暖i 2024-12-10 10:00:28

嗯,这对我来说太长了,所以我将提供一条对我来说适用于 UWP 项目的路线,该路线看起来简单得多,但不确定它是否适合您的情况:

要跳到正确的位置,请使用 MediaPlayer.PlaybackSession.Position = TimeSpan.FromMilliseconds(yourDesiredPosition);

用于了解何时跳到正确的位置(或执行任何操作)否则你需要启动、停止、切换源等等),只需附加一个位置更改事件:

    Player.MediaPlayer.PlaybackSession.PositionChanged += PlaybackSession_PositionChanged;

    private void PlaybackSession_PositionChanged(MediaPlaybackSession sender, object args)
    {
        var playa = sender.MediaPlayer;

        if(playa.Position >= YourSpecialTimeSpan)
        {
            //do something, note can check state with sender.PlaybackState
        }
    }

希望这有帮助......

Hmmm, that was tldnr for me so I'll offer up a route that worked for me for a UWP project that seems much simpler, but not sure if it'll work in your case:

For skipping to the right spots, use MediaPlayer.PlaybackSession.Position = TimeSpan.FromMilliseconds(yourDesiredPosition);

For knowing when to skip to the right spots (or do whatever else you need to, start, stop, switch source, whatever), just attach a position changed event:

    Player.MediaPlayer.PlaybackSession.PositionChanged += PlaybackSession_PositionChanged;

    private void PlaybackSession_PositionChanged(MediaPlaybackSession sender, object args)
    {
        var playa = sender.MediaPlayer;

        if(playa.Position >= YourSpecialTimeSpan)
        {
            //do something, note can check state with sender.PlaybackState
        }
    }

hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文