Search code examples
vb.netaxwindowsmediaplayer

Why can I not set the volume of an AxWindowsMediaPlayer object with a slider?


Whenever I try to say the volume of the media player like this:

Private Sub SndMasterSlider_ValueChanged(sender As Object, e As EventArgs) Handles SndMasterSlider.ValueChanged
    Player1.settings.volume = SndMasterSlider.Value
End Sub

I get this error: An exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.WMPLib.dll but was not handled in user code


Solution

  • Assuming that the Slider is the TrackBar control, you will get that exception when creating the form (you did not say where, but I'll bet that is where). This is because the Designer code will set the Value thus firing the ValueChanged event before it has created your AxWMP control:

     Me.TrackBar1.Value = 50
    

    This will fire the event even though the form is being created. You can set a flag to indicate when to process the ValueChanged event, manually add the handler or just use the Scroll event which will fire when the user actually moves the thumb.

    Private Sub TrackBar1_Scroll(...
        AxWMP.settings.volume = TrackBar1.Value
    End Sub