通过按钮单击以编程方式停止AxWindowsMediaPlayer

时间:2016-05-23 11:14:30

标签: c# wpf axwindowsmediaplayer

如何在按钮点击时手动停止AxWindowsMediaPlayer?我正在使用VS 2015开发一个应用程序并使用VS 2015。

 preceeding_breed   frequency
 breed_eagles         1
 breed_lappets        0
 breed_hyenas         1

1 个答案:

答案 0 :(得分:1)

如果您在按钮上有命令绑定,或以编程方式观看按钮单击事件,则可以执行以下命令:

axWindowsMediaPlayerObject.Ctlcontrols.stop();

这是我的代码,我是如何尝试的:

WPF - MainWindow

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <Button Grid.Row="0" Click="ButtonBase_OnClick">Test</Button>

    <WindowsFormsHost Grid.Row="1" x:Name="winhost" HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="Transparent">
        <ax:AxWindowsMediaPlayer x:Name="axwmp" />
    </WindowsFormsHost>
</Grid>

代码背后

public partial class MainWindow : Window
{
    private AxWindowsMediaPlayer mPlayer;

    public MainWindow()
    {
        InitializeComponent();
        mPlayer = axwmp;
        mPlayer.BeginInit();
        mPlayer.EndInit();
        mPlayer.Ctlenabled = true;
        mPlayer.URL = @"http://www.mediacollege.com/audio/tone/files/100Hz_44100Hz_16bit_30sec.mp3";
        mPlayer.Ctlcontrols.play();
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        mPlayer.Ctlcontrols.stop();
    }
}

顺便说一句,您需要将MediaPlayer.exe输出到您的exe。