PLAY (MUSIC) command.

Posted on 1998-07-14
Last Modified: 2008-02-26
In QBasic there is the command PLAY to play musical tunes e.g.

PLAY "CDEFGAB"  - How do I replicate this in VB4?

Question by:deighton
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Expert Comment

ID: 1465678
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_LOOP = &H8
Const SND_NOSTOP = &H10

Function PlaySound(PathFileName as string) as boolean
Dim RetVal as Long
Dim iFlags as Integer

   RetVal = sndPlaySound(PathFileName, iFlags)
   If RetVal Then PlaySound = True

End Function

You must add the following declare to your project in a module (All on one line):

Declare Function sndPlaySound Lib "WINMM.DLL" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Call this function with a statement like:

PlaySound "D:\Sounds\Fire.Wav"

If you have a need, it is a function that will return true if the sound was played successfully.  Check out the other flags, you might like some of the other things you can do with this API function.
LVL 18

Author Comment

ID: 1465679
I've used your code but there is no sound and
  RetVal = sndPlaySound(PathFileName, iFlags)

RetVal = 0 ??  

The path name is definitel correct and I've used windows wav files such as bell.wav

LVL 18

Author Comment

ID: 1465680
I forgot to mention I've not got multimedia I'm using the PC speaker - I've managed to play windows default sounds using the MessageBeep API so far.  Can I play WAV files thru the speaker?
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Accepted Solution

clifABB earned 50 total points
ID: 1465681
I'm sorry to inform you, but there is no equivilant of QB's PLAY command in VB.  There used to be an API call for Windows 3.x that allowed for frequency and duration changes but it is no longer supported (and in fact doesn't work).

In answer to your second question, there is a 16-bit driver that allows you to play sounds (wav as well as midi) through your speaker.  I believe the driver is still available at the Microsoft download site.
LVL 18

Author Comment

ID: 1465682
ClifABB - I can't find the down load - do you know the URL by any chance?

Expert Comment

ID: 1465683
I would like to point out, once again, that this is a 16-bit driver.  It's original intent was for Windows 3.x.  I cannot guarantee that it will work.

Now, you need to install the driver. In Windows, from the Program Manager choose successively Main/Control Panel/Drivers/Add/Unlisted or updated drivers/(enter path of SPEAK.EXE)/PC Speaker. At this point some strange sounds come out as the driver is initialized. Change the settings to improve the sound quality on the various sounds: tada, chimes, etc. Click OK when you are finished and choose the Restart windows option.

Having installed the speaker driver, you will now get sounds whenever you start Windows, make a mistake, or exit Windows. If you do not want this, from the Main/Control Panel/Sounds menu, make sure there is no X next to "Enable System Sounds."
LVL 18

Author Comment

ID: 1465684
Thanks for your help - I'll see how it goes!

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 11 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question