A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© heima-shzhch 中级黑马   /  2014-4-8 22:28  /  1327 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在wpf中的windows.mediaplayer怎样用啊?请教高手

1 个回复

倒序浏览
本帖最后由 许庭洲 于 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;
    }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马