0

主窗口.Xaml

<MediaElement Margin="10,10,10,0 " Name="McMediaElement" Width="450" Height="250" LoadedBehavior="Manual" Stretch="Fill"
              MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded" UnloadedBehavior="Stop" />
    <Label Grid.Column="1" Grid.Row="1" Height="28" Margin="82,0,99,3.52" 
           Name="FileNameLabel" VerticalAlignment="Bottom" Background="LightGreen">
    </Label>
    <Button Height="23" Name="BrowseButton" Width="113" Grid.Column="1" HorizontalAlignment="Right" 
            Margin="0,0,182,7.04" Grid.Row="1" VerticalAlignment="Bottom" Grid.ColumnSpan="2"
            FontWeight="Bold" Click="BrowseButtonClick">
        Browse a Media
    </Button>

当单击 BrowseButton 它从打开的对话框中获取文件并将其作为 FileNameLabel 放入 Lable 控件中

private void BrowseButtonClick(object sender, RoutedEventArgs e)
{
    System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
    dlg.InitialDirectory = "c:\\";
    dlg.Filter = "Media files (*.wmv)|*.wmv|All Files (*.*)|*.*";
    dlg.RestoreDirectory = true;
    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        string selectedFileName = dlg.FileName;
        FileNameLabel.Content = selectedFileName;
        McMediaElement.Source = new Uri(selectedFileName);
        McMediaElement.UpdateLayout();
        McMediaElement.Play();
    }
}
    private void Element_MediaOpened(object sender, EventArgs e)
    {
        timelineSlider.Maximum = McMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
    }

    // When the media playback is finished. Stop() the media to seek to media start.
    private void Element_MediaEnded(object sender, EventArgs e)
    {
        McMediaElement.Stop();
    }

它不播放媒体文件。所以 Plaese 建议代码中的问题。

4

0 回答 0