Question

How to play different sounds with computer speaker in VB?

Asked by: d_chenxin

I used Beep function(API),beep has two parameters,but I can't play different sounds.
I want to ask how to play different sounds in VB,that is to say,how to play different HZ.
My program likes following:
   
private sub command1()
    Beep 1000,1000
end sub

The two parameters 1000,1000 are not useful.Anyother API can help me?

Thank you very much!!!
                                        David Chen

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2001-02-02 at 05:24:36ID20068950
Topic

Visual Basic Programming

Participating Experts
6
Points
300
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. PC Speakers
    When I use the Beep procedure, it hardly gives a beep at all. How do I get a decent and loud BUZZ or BEEP from the PC Speakers? (I am using the MessageBeep if the user's computer has a multimedia card, otherwise I have to use the PC speakers...) Thanks in advance... Keith
  2. How to produce a beep with the PC Speaker
    How do I produce A beep from the PC Speaker using Visual Basic 4? Beep produces a default wav sound (not PC speaker), MessageBeep API call does not work (gives an error of "Specified DLL function not found").
  3. Emitting a beep on the internal speaker
    Hi all, I'm looking for a way to emit a beep on the internal speaker no matter what the multimedia configuration is (sound card installed or not). Under Windows NT, there is Beep() that does the job nicely, but under Win95/98, it doesn't work the same. Is there a way to achie...
  4. Music On Command1
    When the user presses Command1, I want a MP3 to play. Give me code I need to use in the Command1_Click. In your answer, Please use C:\Music.MP3 as the MP3 file name.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: d_chenxinPosted on 2001-02-02 at 05:28:09ID: 5805911

sound

 

by: NinibiniPosted on 2001-02-02 at 05:43:39ID: 5805955

what OS do you have?

-->found the following in the API-Guide from allapi.net:

' Attempt to play a note at 800 Hz for 2 seconds.  This will only behave this way on Windows NT/2000; users of Windows 95/98 will only hear the  default sound.

Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Private Sub Command1_Click()
    Dim retval As Long  ' return value
    retval = Beep(800, 2000)  ' ideally, an 800 Hz tone for 2 seconds
End Sub

 

by: bhanu54Posted on 2001-02-02 at 05:48:44ID: 5805978

i remember using a BASIC program where the keyboard had its keys mapped to different frequencies.

the user was actually able to play simple songs like "happy bithday" using the keyboard.

i think that can be done in VB too.

this was just to fire the imagination of some experts.

Sorry i didnt give the solution.

 

by: hesPosted on 2001-02-02 at 07:53:20ID: 5806388

 

by: rspahitzPosted on 2001-02-02 at 08:38:38ID: 5806611

In Quick BASIC, it was real easy:
  PLAY "A A# B C C#" etc.

In VB, I think you can do this by tapping in to the Multimedia control.  If you create a wav file which contains all of the notes you need, use MM to play back certain parts of it.

I've also seen it done (by a company called "Ice", or something like that) by tapping into the Midi controls of Windows.  They gave the source code for a cool VB piano, but I don't have the code handy, and it was somewhat complicated.  If you really want it, let me know and I'll look it up over the weekend.

 

by: chadhaajayPosted on 2001-02-02 at 09:58:09ID: 5806946

Here are some guaranteed codes to play sounds in Visual Basic

1. For Playing .WAV sounds

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

' Now declare certain constants for sndplaysound function

Const snd_async = &H1
Const snd_loop = &H8
Const snd_memory = &H4
Const snd_nodefault = &H2
Const snd_nostop = &H10
Const snd_sync = &H0


Private Sub Command1_Click()
Dim var As Long
Dim filepath As String 'This variable tells the path of the sound file stored on the computer
'Now tell the path of the sound to be played
filepath = "c:\hello.wav" 'You can also let the user open a sound file through common dialog control with the statement path=commondialog1.filename
var = sndPlaySound(filepath, snd_async)
End Sub

2. For Playing .MIDI Sounds

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Sub Command1_Click()
FileName = "c:\9c.mid"
Call mciSendString("Open " & FileName & " Alias MM", 0, 0, 0)
Call mciSendString("Play MM", 0, 0, 0)
End Sub
Private Sub Command3_Click()
Call mciSendString("Stop MM", 0, 0, 0)
End Sub
Private Sub Form_Load()
Call mciSendString("Stop MM", 0, 0, 0)
Call mciSendString("Close MM", 0, 0, 0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call mciSendString("Stop MM", 0, 0, 0)
Call mciSendString("Close MM", 0, 0, 0)
End Sub

3. For Playing .MP3 Sounds

Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long

Private Sub Command1_Click()
PlayMP3 ("e:\soni.mp3")
End Sub

Public Function PlayMP3(Filename As String)
Dim dwreturn As Long
Dim ret As String * 128
Dim tmp As String * 255
Dim lenShort As Long
Dim ShortPathAndFie As String
'send a filename to the function
If Dir(Filename) = "" Then
MsgBox "Error with input file"
Exit Function
End If
lenShort = GetShortPathName(Filename, tmp, 255)
ShortPathAndFie = Left$(tmp, lenShort)
glo_hWnd = hWnd
cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias mpeg"
dwreturn = mciSendString(cmdToDo, 0&, 0&, 0&)

If dwreturn <> 0 Then  'not success
mciGetErrorString dwreturn, ret, 128
MsgBox ret, vbCritical
Exit Function
End If
'then to play just send this
dwreturn = mciSendString("play mpeg", 0&, 0&, 0&)

If dwreturn <> 0 Then
mciGetErrorString dwreturn, ret, 128
MsgBox ret, vbCritical
Exit Function
End If
End Function

Good Luck.....

Ajay chadha .... :-)

 

by: chadhaajayPosted on 2001-02-02 at 10:01:44ID: 5806957

For a Nice Beep Effect

Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long

Code :-

Dim X%
X% = Beep(1500, 300)

Good Luck......

Ajay Chadha ..... :-)

 

by: chadhaajayPosted on 2001-02-02 at 10:11:51ID: 5807001

Hey Check this one...

Declare Function Beep Lib "kernel32" Alias "Beep" As Long

ByVal dwFreq As Long
ByVal dwDuration As Long

'**************************************
' Name: Beep()
' Description:Generates simple tones on
'     the speaker.
' By: Limelight VB Page
'
' Inputs:

dwFreq :-

Specifies the frequency, In hertz, of the sound. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).

dwDuration :-

Specifies the duration, In milliseconds, of the sound.
One value has a special meaning: If dwDuration is - 1, the Function operates asynchronously and produces sound until called again.
'
' Returns:If the function succeeds, the return value is TRUE.

If the Function fails, the return value is FALSE. To Get extended Error information, call GetLastError.

'
' Assumes:
The Beep Function is synchronous In all but one case; the function does Not generally return control To its caller until the sound finishes. The exception to this occurs when dwDuration has the value - 1. In that case, Beep is asynchronous, returning control immediately to its caller While the sound continues playing. The sound continues until the Next call to Beep.
'


Good Luck.....

Ajay Chadha .... :-)

 

by: chadhaajayPosted on 2001-02-02 at 10:23:07ID: 5807045

Here is what I did to make my PC speaker beep at the frequency and length of time I want, using hardware direct control. It works fine in Win95 and Win98. Not in WinNT.

Just follow these steps:

1) Download the library WIN95IO.DLL from http://www.softcircuits.com (Free software).

2) Copy this DLL to your System folder

3) Put a command buton on your form named cmdStartSound

4) Put a timer on your form and name it as TimerSound

5) Copy all of this code to your form

6) Run it !!!

Have a nice sound and make your own fiesta with tequila and señoritas...!

'*********************************************************

Option Explicit
Dim SoundEnd As Boolean

'If you wish, put this declarations on a module, deleting "Private".

'Write a byte to port:


Private Declare Sub vbOut Lib "WIN95IO.DLL" (ByVal nPort As Integer, ByVal nData As Integer)

    'Read a byte from port:


Private Declare Function vbInp Lib "WIN95IO.DLL" (ByVal nPort As Integer) As Integer
    'These are standard freqs of music. You
    '     can set any freq.
    Const C = 523'Do In spanish
    Const D = 587.33'Re
    Const E = 659.26'Mi
    Const F = 698.46'Fa
    Const G = 783.99'Sol
    Const A = 880'La
    Const B = 987.77'Si


Private Sub cmdStartSound_Click()
    Dim i As Integer
    'This is all you have to do to simulate
    '     a phone ring sound.


    For i = 1 To 12
        Sounds C, 20'Sounds 523 Hz In 20 miliseconds
        Sounds F, 20'Sounds 698.46 Hz In 20 miliseconds
    Next i
    'Need to go up an octave? Just double th
    '     e frequency or viceversa.
    ' example:
    'Sounds C * 2, 500'An octave up
    'Sounds C / 2, 500'An octave down
    'Yes, you can do a funny piano using you
    '     r programming skills !
End Sub


Private Sub Sounds(Freq, Length)
    Dim LoByte As Integer
    Dim HiByte As Integer
    Dim Clicks As Integer
    Dim SpkrOn As Integer
    Dim SpkrOff As Integer
    '"I didn't tested if this is exactly the
    '     frequency,
    'but it's ok to start here. I you wish m
    '     ore precision,
    'try with a piano or another reference t
    '     o adjust the clicks.
    'For example, "A" has a frequency of 880
    '     Hertz. If you have
    'a good ear, it may be adjusted very clo
    '     se by
    'changing the 1193280 number up or down.
    '    
    'Of course, you can use a frequency mete
    '     r.
    'I didn't tested the frequency limits to
    '     o. Test it by yourself."
    'Length precision is the same as the tim
    '     er control precision.
    'Ports 66, 67, and 97 control timer and
    '     speaker
    'Divide clock frequency by sound frequen
    '     cy
    'to get number of "clicks" clock must pr
    '     oduce.
    Clicks = CInt(1193280 / Freq)
    LoByte = Clicks And &HFF
    HiByte = Clicks \ 256
    'Tell timer that data is coming
    vbOut 67, 182
    'Send count to timer
    vbOut 66, LoByte
    vbOut 66, HiByte
    'Turn speaker on by setting bits 0 and 1
    '     of PPI chip.
    SpkrOn = vbInp(97) Or &H3
    vbOut 97, SpkrOn'My speaker is sounding !
    'Leave speaker on (while timer runs)
    SoundEnd = False'Do Not finish yet
    TimerSound.Interval = Length'Time To sound
    TimerSound.Enabled = True'Begin To count time


    Do While Not SoundEnd
        'Let processor do other tasks


        DoEvents
        Loop
        'Turn speaker off resetting bit 0 and 1.
        '    
        SpkrOff = vbInp(97) And &HFC
        vbOut 97, SpkrOff
    End Sub


Private Sub TimerSound_Timer()
    'Time is over
    SoundEnd = True 'Finish sound now
    TimerSound.Enabled = False
End Sub

Good Luck.....

Ajay Chadha ..... :-)

 

by: chadhaajayPosted on 2001-02-02 at 10:27:53ID: 5807061

Here is another one....

" To play RTTTL (nokia ring tone) tunes "

If you have a nokia mobile phone and looked at ring tones you will have come across RTTTL, the text format for the tunes. This is a stand alone module with one public function PlayRTTTL. You give it a tune as a string in RTTTL format and it plays it using beeps. Note that this only works on NT as the Beep function is different on windows. If you are wondering what it could be used for, here is an example, at work we have written a phone book system for staff extension numbers and when you click on an entry you see details about the person and a picture. I wanted to let staff also give themselves a theme song that would play when you clicked on them. Since there are hundreds of RTTTL tunes available on the internet I decided to use that format as it is easily edited by users and saved to the database, and users can add new ones whenever they like. The code could have been written better, but I wanted to keep it in a self contained single module that you could plug and play into any project. This has nothing to do with Nokia mobile phones, it just uses the same format for the tunes. If you have not seen them, this is an example of the format: Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#, 8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6 The Simpsons are probably copyrighted so don't use that one

http://www.planetsourcecode.com/xq/ASP/txtCodeId.5645/lngWId.1/qx/vb/scripts/ShowCode.htm

Good Luck....

Ajay Chadha ... :-)

 

by: Richie_SimonettiPosted on 2001-02-02 at 20:38:32ID: 5808504

Ping

 

by: bhanu54Posted on 2001-02-02 at 21:13:07ID: 5808533

ajay chaddha that surely was a blast of info from you.Best lucks for your source code web site.

 

by: chadhaajayPosted on 2001-02-02 at 21:24:54ID: 5808539

Thanks bahnu54,

I'm really glad to know that people like ou are really looking forward for my website. I assure you that that would be the supereb site for all of you. You'll not be dissapponted....

Good Luck....

Ajay Chadha .... :-)

 

by: d_chenxinPosted on 2001-02-04 at 22:28:04ID: 5812082

Thank you very much!


                     u r an experts!!!!!!!!!!!!!!


                                       David Chen
                                       from china

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...