本帖最后由 许庭洲 于 2014-8-18 08:21 编辑
1.在XAML 中加入AxWMPLib 命名空间,并将上篇MediaElement 替换为AxWindowsMediaPlayer 控件,注意此处是将WinForm 控件嵌入WPF 程序,所以要将AxWindowsMediaPlayer 控件放到<WindowsFormsHost>标签中。
<Window x:Class="WPFWMP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mediaControl="clr-namespace:AxWMPLib;assembly=AxInterop.WMPLib"
Title="WPF Media Player" Height="450" Width="520" Background="#FF554D4D">
<Window.Resources>
<Style x:Key="btnStyle" TargetType="Button">
… … </Style>
</Window.Resources>
<StackPanel HorizontalAlignment="Center" Margin="10">
<Border BorderThickness="3" Background="Black">
… … <WindowsFormsHost Height="340" Width="450">
<mediaControl:AxWindowsMediaPlayer x:Name="wpfMediaPlayer"/>
</WindowsFormsHost>
</Border>
<Button Content="Open File" Click="openFile_Click" Margin="10"
Width="80" Style="{StaticResource btnStyle}"/>
</StackPanel>
</Window>
2.通过Windows API Code Pack 为“Open File” 按键添加点击事件,默认打开Sample Video 文件夹,选择视频文件后自动播放。
private void openFile_Click(object sender, RoutedEventArgs e)
{
ShellContainer selectedFolder = null;
selectedFolder = KnownFolders.SampleVideos as ShellContainer;
CommonOpenFileDialog cfd = new CommonOpenFileDialog();
cfd.InitialDirectoryShellContainer = selectedFolder;
cfd.EnsureReadOnly = true;
cfd.Filters.Add(new CommonFileDialogFilter("WMV Files", "*.wmv"));
cfd.Filters.Add(new CommonFileDialogFilter("AVI Files", "*.avi"));
cfd.Filters.Add(new CommonFileDialogFilter("MP3 Files", "*.mp3"));
if (cfd.ShowDialog() == CommonFileDialogResult.OK)
{
wpfMediaPlayer.URL = cfd.FileName;
}
}
|