1

How can I stop the AxWindowsMediaPlayer manually on button click? I am developing an application in WPF and using VS 2015.

<WindowsFormsHost x:Name="winhost" HorizontalAlignment="Stretch" VerticalAlignment="Center" Background="Transparent" Visibility="Hidden">
    <ax:AxWindowsMediaPlayer x:Name="axwmp" /> 
</WindowsFormsHost>
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
stack_91
  • 31
  • 1
  • 9

1 Answers1

2

If you have a commandbinding on the button, or watching the button click event programmatically, you can execute this command:

axWindowsMediaPlayerObject.Ctlcontrols.stop();

Here is my code, how i tried it:

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>

Code Behind

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();
    }
}

Btw, you need to have the MediaPlayer.exe outputed to your exe.

Marius
  • 562
  • 5
  • 26
  • @DebugMe_77, please provide us your code, we could find where it stucks. Maybe you are getting an error message? Provide what you have, to get good help. – Marius May 23 '16 at 11:28
  • i am getting error like "A Handled Exception just occured : Exception of type ' System.Windows.Forms.Axhost + InvalidActiveXstateException' was thrown. This Error is coming when i click on button , on which i write the code for stop the axWindowsMediaPlayerObject. Please help. – stack_91 May 23 '16 at 11:34
  • @DebugMe_77, please read [THIS](http://stackoverflow.com/a/22727826/4198052) answer. Maybe this could help you. If not, edit your QUESTION and add some sample code. – Marius May 23 '16 at 11:36
  • Please Review following code : - this is my Xaml Page . – stack_91 May 23 '16 at 11:43
  • On Xaml.cs page I write the code for stop the axWindowsMediaPlayerObject i,e Axwmp and the code on button click is given below. i,e axWmp.Ctlcontrols.stop(); could you please tell me the where i am wrong. – stack_91 May 23 '16 at 11:48
  • Hello Marius , have you find Something. – stack_91 May 23 '16 at 12:03
  • @DebugMe_77, I just tried it myself, but I dont get any error. Maybe you can try to initialise the player manually. So you need to do these steps: mPlayer.BeginInit(); mPlayer.EndInit(); mPlayer.Ctlenabled = true; – Marius May 23 '16 at 12:05
  • Ok thanx.but would u plz tell me that where i am stucking. – stack_91 May 23 '16 at 12:09
  • this problem is not fixed yet. problem is persist right now.i donot know what is the problem here. – stack_91 May 23 '16 at 12:56
  • @DebugMe_77, do you have the exe and all files (dlls) which are mentioned in [THIS](http://www.codeproject.com/Tips/497004/Using-Windows-Media-Player-Control-in-WPF) article? Maybe you can try to add this line to your initialisation of the player " mPlayer.CreateControl();" – Marius May 23 '16 at 13:01