0

Can I please have some help to play a audio file with the AxWindowsMediaPlayer.

I am using Visual Basic.net 2012 Ultimate.

Here is my code:

Imports AxWMPLib

Public Class Form1

Public WithEvents MediaPlayerObject As AxWindowsMediaPlayer = New AxWindowsMediaPlayer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MediaPlayerObject = New AxWindowsMediaPlayer
    MediaPlayerObject.Ctlenabled = True
    MediaPlayerObject.URL = "C:\Users\Simon\Music\Music\CanLucidDream\108761__soundbytez__birds-late-morning.wav"
    MediaPlayerObject.Ctlcontrols.play()
End Sub

End Class

I get the following error when trying to access any of the objects properties or methods:

An unhandled exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.WMPLib.dll

user2023359
  • 289
  • 2
  • 10
  • 18

2 Answers2

0

You need to pay attention that

-MediaPlayerObject.URL = "C:\Users\Simon\Music\Music\CanLucidDream\108761__soundbytez__birds-late-morning.wav"- 

should be like this;

MediaPlayerObject.URL = "C:\\\Users\\\Simon\\\Music\\\Music\\\CanLucidDream\\\108761__soundbytez__birds-late-morning.wav"

or like this;

MediaPlayerObject.URL = @"C:\Users\Simon\Music\Music\CanLucidDream\108761__soundbytez__birds-late-morning.wav"
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
-1

You need to call BeginInit() and EndInit() before using any properties or methods.

MediaPlayerObject.BeginInit()
MediaPlayerObject.EndInit()
MediaPlayerObject.CtlEnabled = True
Community
  • 1
  • 1
Jeff
  • 1