Question Audio file plays only when execution paused - threading issue?

gusset25

Member
Joined
Apr 29, 2010
Messages
12
Programming Experience
5-10
I have a form and a class module.

the application plays a sound file from using a windows media player control on the form. it seems silly to me that you need to have a hidden visual control on a form rather than just using code, but that's another gripe.

previously, the code (in the form module) was :

VB.NET:
Private Sub Something()
Call PlayMusic("filepath/filename.mp3")
End Sub

Private Sub PlayMusic(strPathName As String)
Me.AxWindowsMediaPlayer1.URL = strPathName
End Sub

this worked perfectly.

i then changed things so that this calling code is in a class module:

VB.NET:
Public Class Music
    Private WithEvents frm1 As frmMain


    Sub Play(ByVal strPathName As String) 'this proc uses its own thread
        frmMain.AxWindowsMediaPlayer1.URL = strPathName

    End Sub
End Class

and this code is in the form module:

VB.NET:
Private Snippet As Music
Private Sub Form1_Load(etc) Handles MyBase.Load
        Snippet = New Music
        Snippet.Play(filepath/filename.mp3")
End Sub

and i hear no music

the funny thing is: if i put
VB.NET:
msgbox("test")"

in Sub Play, the audio file plays, until i dismiss the msgbox.
 
Last edited:
it seems silly to me that you need to have a hidden visual control on a form rather than just using code, but that's another gripe.

You don't need a hidden control. Add a reference to the Windows Media Player COM library and create an instance of the IWMPPlayer interface. It's basically a non-visual equivalent of the ActiveX control.
 
JMC's answer solved the actual problem as well as answering my throwaway question. Here is the now-working code:
VB.NET:
Private Sub Something()
        Dim FormLoadSnippet As New Music
        FormLoadSnippet.Play(filename/path.mp3")
End Sub

and the class module:

VB.NET:
Public Class Music
    Sub Play(ByRef strPathName As String)
        Dim wmPlayer As New WMPLib.WindowsMediaPlayer
        wmPlayer.URL = strPathName
    End Sub
End Class

Thanks again!

ps i downloaded microsoft's wmplayer sdk, which is free and helpful
 
Back
Top