以下代码无延迟地工作,有必要添加一个停止命令。
private static OpenFileDialog OpenFile()
{
OpenFileDialog ofd;
ofd = new OpenFileDialog();
ofd.AddExtension = true;
ofd.DefaultExt = "*.*";
ofd.Filter = "media (*.*)|*.*";
ofd.ShowDialog();
return ofd;
}
private void loadm1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = OpenFile();
M1.Source = new Uri(ofd.FileName, UriKind.Relative);
M1.Position = TimeSpan.Zero;
M1.LoadedBehavior = MediaState.Stop;
}
private void loadm2_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = OpenFile();
M2.Source = new Uri(ofd.FileName, UriKind.Relative);
M2.LoadedBehavior = MediaState.Stop;
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
M1.LoadedBehavior = MediaState.Play;
M2.LoadedBehavior = MediaState.Play;
}
private void btnPause_Click(object sender, RoutedEventArgs e)
{
M1.LoadedBehavior = MediaState.Pause;
M2.LoadedBehavior = MediaState.Pause;
}
XAML
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="252*"/>
<ColumnDefinition Width="265*"/>
</Grid.ColumnDefinitions>
<MediaElement x:Name="M1" Margin="0,0,5,126" />
<MediaElement x:Name="M2" Grid.Column="1" Margin="0,0,15,126"/>
<Button x:Name="loadm1" Content="Load" HorizontalAlignment="Left" Margin="10,0,0,99" Width="75" Height="22" VerticalAlignment="Bottom" Click="loadm1_Click"/>
<Button x:Name="loadm2" Content="Load" Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,99" Width="75" Height="22" VerticalAlignment="Bottom" Click="loadm2_Click"/>
<Button x:Name="btnStart" Content="Start" Margin="10,0,20,20" Grid.ColumnSpan="2" Height="22" VerticalAlignment="Bottom" Click="btnStart_Click"/>
<Button x:Name="btnPause" Content="Pause" HorizontalAlignment="Left" Margin="10,289,0,0" VerticalAlignment="Top" Width="75" Click="btnPause_Click"/>
</Grid>
什么是你的问题? – sr28
对不起,我添加了一个问题 – XmaksasX
你是否在任何东西中持有MediaElements帆布? – sr28