Click to See Complete Forum and Search --> : Playing sound files


DaddyGweedo
January 23rd, 2003, 01:55 PM
What is the appropriate control or class to use in order to play wav or mp3 files.

Currently the only method I have found is using the Microsoft Media Player COM object and hiding it.

It works, but I assume that there is a new multimedia class or control.

Anyone know what it is?

Thanks,

DG.

Maddog32
January 24th, 2003, 04:15 PM
I got this code a for vb6 a long time ago from vb square. Hope it works in vb.net!

'Sound API
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Const SND_SYNC = &H0 ' play synchronously (default)
Public Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy

Sub PlaySound(zSound As String)
Dim lRetVal As Long
On Error Resume Next
lRetVal = sndPlaySound(zSound, SND_SYNC)
End Sub:)

DaddyGweedo
January 25th, 2003, 04:17 PM
It does work, however it holds up the whole program until the sound completes. So that'll work for short sounds, but longer ones cause the program not to respond.

Iouri
January 27th, 2003, 10:10 AM
You can play sound in a different thread

'Form
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PlayS("c:\windows\media\tada.wav")
End Sub

Private Sub PlayS(ByVal sound As String)
Dim thread As New SoundThread()
Dim mThread As New System.Threading.ThreadStart(AddressOf thread.newThread)
Dim oThread As New System.Threading.Thread(mThread)
thread.sound = sound
oThread.Start()
End Sub

End Class

Class SoundThread
Public sound As String
Sub newThread()
PlaySound(sound, 0, 0)
End Sub
End Class


'Module
Module Module1
Public Declare Function PlaySound Lib "winmm.dll" (ByVal filename As String,ByVal hmodule As Long, ByVal dword As Integer) As Boolean
End Module

Maddog32
January 27th, 2003, 12:50 PM
Cool! I'm in the process of upgrading to vb.net so I copied this snippet for my projects! Thanks guys! Bob.