CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Sunny South Africa

    [2005] How do I Play Music from my program?

    Three of the most common methods used to play music from within your program are the following

    Q: How do I play music?

    A: Option 1:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Works in 2003 and 2005
            Dim MyProcess As New Process
            MyProcess.StartInfo.FileName = SoundFileName
            MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        End Sub
    Option 2:
    Imports System.Runtime.InteropServices 'for api
    Private Shared Function PlaySound(ByVal lpszName As String, ByVal hModule As Int32, ByVal dwFlags As Int32) As Int32
        End Function 'PlaySound API
        Private Const SND_FILENAME As Integer = &H20000
        Private SoundFileName As String 'sound file name & location
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'Works in 2003 and 2005
            'API ( PlaySound )
            PlaySound(SoundFileName, 0, SND_FILENAME)
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            SoundFileName = "C:\WINDOWS\MEDIA\chimes.wav"
        End Sub
    Option 3:
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            'Works in 2005 ONLY
            'My Object
            My.Computer.Audio.Play(SoundFileName, AudioPlayMode.Background)
        End Sub
    Last edited by HanneSThEGreaT; June 9th, 2008 at 02:35 AM.

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