locked
How to add music and sound effects to a Windows Form Application?

    Question

  • Hey guys, I'm new to Visual Basic and to this forum.

    I'm trying to make a simple game, and as the title suggests, I'd like to know how to add music to it.  Help?

    Friday, April 22, 2011 1:04 AM

Answers

  • for .wavs:

    Dim player As New Media.SoundPlayer
    player.Stream = My.Resources.[resourceName]
    player.Play()
    

    or:

    Dim player As New Media.SoundPlayer
    player.SoundLocation = "path"
    player.Play()
    

     

    for .mp3s:

    Public Class Form1
    
      Private Declare Function mciSendString Lib "Winmm.dll" Alias "mciSendStringA" _
      (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
      ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim file As String = Chr(34) & "filename.mp3" & Chr(34)
        mciSendString("open " & file & " alias track", Nothing, 0, 0)
        mciSendString("play track", Nothing, 0, 0)
      End Sub
    
    End Class
    

     

     


    thanks for any help
    • Marked as answer by Dasas Friday, April 22, 2011 3:23 AM
    Friday, April 22, 2011 1:52 AM

All replies

  • what type of music files do you want to add?

    the simplest are .wav files, but .mp3s are not much more difficult


    thanks for any help
    Friday, April 22, 2011 1:32 AM
  • If .wav files are the simplest, then I'll convert everything to .wav - I just want to know how it's done (:
    Friday, April 22, 2011 1:48 AM
  • for .wavs:

    Dim player As New Media.SoundPlayer
    player.Stream = My.Resources.[resourceName]
    player.Play()
    

    or:

    Dim player As New Media.SoundPlayer
    player.SoundLocation = "path"
    player.Play()
    

     

    for .mp3s:

    Public Class Form1
    
      Private Declare Function mciSendString Lib "Winmm.dll" Alias "mciSendStringA" _
      (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
      ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim file As String = Chr(34) & "filename.mp3" & Chr(34)
        mciSendString("open " & file & " alias track", Nothing, 0, 0)
        mciSendString("play track", Nothing, 0, 0)
      End Sub
    
    End Class
    

     

     


    thanks for any help
    • Marked as answer by Dasas Friday, April 22, 2011 3:23 AM
    Friday, April 22, 2011 1:52 AM
  • Wow, thanks!

    Just one more thing-

    What does the stuff in red mean?  "path"? "alias track"?

    Could I just copy/paste this into my program and have it work?

    • Proposed as answer by Zeldacat Friday, April 22, 2011 1:42 PM
    • Unproposed as answer by Zeldacat Friday, April 22, 2011 1:42 PM
    Friday, April 22, 2011 2:29 AM
  • Hi Dasas

    You can copy and paste that into your program. The red text just denotes that it is inside Quotations that is all


    Regards ~~ Zeldacat
    Friday, April 22, 2011 1:44 PM