CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2000
    Posts
    70

    Playing sound files

    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.

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    6
    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

  3. #3
    Join Date
    Jul 2000
    Posts
    70

    It does work...

    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.

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878
    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
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    6
    Cool! I'm in the process of upgrading to vb.net so I copied this snippet for my projects! Thanks guys! Bob.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured