Solved

playing sound files

Posted on 2001-08-05
25
221 Views
Last Modified: 2008-02-01
How do I play a sound file?  I have "Programming Microsoft Visual Basic 6.0" which mentions nothing at all about multimedia.
0
Comment
Question by:2001gig
  • 9
  • 7
  • 4
  • +3
25 Comments
 
LVL 3

Accepted Solution

by:
andysalih earned 100 total points
ID: 6354042
ttry this


Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
    Then to call the API type:
    Variable = sndPlaySound (Location, 1)
    So for example to play a .wav file located at C:\Sounds\sound.wav type:
    Variable = sndPlaySound ("C:\Sounds\sound.wav, 1)


thats it

andy
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6354045
or

sndPlaySound("me.wav",SND_SYNC)


cheers
andy
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6354049
or



Private Sub Command1_Click()
    On Error Goto ErrHandler
    CommonDialog1.CancelError = True
    CommonDialog1.Filter = "MIDI Sequence (*.mid; *.rmi)|*.mid; *.rmi|WAV Sound (*.wav)|*.wav|MP3 (*.mp3)|*.mp3"
    CommonDialog1.ShowOpen


    If CommonDialog1.Filename <> "" Then
        Mediaplayer1.Filename = CommonDialog1.Filename
        Mediaplayer1.Autostart = False
        Mediaplayer1.Stop


        Form1.Caption = CommonDialog1.FileTitle
        End If
        Exit Sub
        ErrHandler:
        'If an error occurs, exit the procedure.
        '    
    End Sub


Private Sub Command2_Click()
    Mediaplayer1.Play
End Sub

there should be enough here to help you


cheers
Andy

0
 
LVL 5

Expert Comment

by:jklmn
ID: 6354174
Hi 2001qiq,

Get MMControl from menu Project | Components | Microsoft Multimedia Control 6.0
and try following:

Private Sub Form_Load()
With MMControl1
.filename = "c:\yourMusic.mid"
.Command = "open"
.Command = "play"
End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
MMControl1.Command = "close"
End Sub
0
 
LVL 2

Expert Comment

by:dmarco
ID: 6354275
Hi 2001,

What kind of sound have you to play? If you need a background music (e.g. a midi file), jkimm comment suggests a good solution.
If you need a short (.wav file) sound "comment" which is played when your application is running, use the SndPlaySound API call as suggested by andysalih. Use the  constant SND_ASYNC (&H1), if you want to play your wav file without stopping your application.

Another way to do the same thing:
Add OLE control to your form, choose MediaPlayer from the dialog box and click on the option button "Create from file", browse to find your sound file and add it.
When you need your sound played add this code:

OLE1.DoVerb(0)

(where OLE1 is the name property of your OLE object).

hope this helps.
0
 
LVL 27

Expert Comment

by:Ark
ID: 6354474
Hi
Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpszCommand As String, lpszReturnString As Any, ByVal cchReturnLength 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)

Private Sub cmdPlay_Click()
  cmdPlay.Enabled = False
  cmdStopPlay.Enabled = True
'You can use any multimedia sound files - wav, midi etc.
  PlaySoundFile "c:\windows\media\start.wav"
'or PlaySoundFile "c:\MySounds\music.mid", "sequencer"
'or PlaySoundFile "", "cdaudio"
  cmdStopPlay.Enabled = False
  cmdPlay.Enabled = True
End Sub

Private Sub cmdStopPlay_Click()
   mciSendString "stop sound", 0&, 0, 0
   cmdStopPlay.Enabled = False
   cmdPlay.Enabled = True
End Sub

Private Sub PlaySounFile (sFileName As String, Optional sFileType As String = "waveaudio")
  mciSendString "open " & sFileType & "!" & ShortFileName(sFilePath) & " alias sound1", 0&, 0, 0
  mciSendString "play sound1", 0&, 0, 0
  Do While IsWavePlaying("sound1")
    DoEvents
  Loop
  mciSendString "close sound1", 0&, 0, 0
End Sub

Private Function ShortFileName(sLongName As String) As String
  Dim s As String * 260, k As Long
  k = GetShortPathName(sLongName, s, Len(s))
  If k > 0 Then ShortFileName = Left$(s, k)
End Function

Private Function IsWavePlaying(sAlias As String) As Boolean
   Dim i As Long, RS As String, k As Long
   RS = Space$(128)
   mciSendString "status " & sAlias & " mode", ByVal RS, 128, k
   RS = Left$(RS, InStr(1, RS, Chr$(0)) - 1)
   IsWavePlaying = (RS = "playing")
End Function
For more advanced samples take alook at http://blackbeltvb.com/index.htm?free/mcisamp.htm

Cheers

0
 

Author Comment

by:2001gig
ID: 6356343
andysalih,

I probably don't know quite how to use this.  

My application contains just one form.  At the top of the code file, I list a bunch of public variables.  Right under that, I have

Public SoundRetVal As Long

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


Then, when I call the function from a subroutine, I have
SoundRetVal = sndPlaySound("G:\staroffice\gallery\sounds\gong.wav", 1)

I'm getting the following error when I try to run the application

Compile error:

Constants, fixed-length strings, arrays, user-defined types, and Declare statements
not allowed as Public members of object modules


What did I do wrong?
0
 
LVL 2

Expert Comment

by:bhh
ID: 6356465
Listening...
0
 
LVL 2

Expert Comment

by:dmarco
ID: 6357418
Add a standard module (.bas) to your project. Declare the playSound API there and when you need sound in your form use this code:
call sndPlaySoun("G:\staroffice\gallery\sounds\gong.wav", 1)

If this works the points are for andysalih, who has suggested the correct answer.
cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 6357442
If your app contain only one form, Just change Public keyword to Private

Cheers
0
 

Author Comment

by:2001gig
ID: 6357836
OK! I tried Ark's suggestion, and now I get a wave file, but not the one I specified!  Instead of gong.wav, I'm getting the Windows ding sound, ding.wav.  

The laptop my VB is installed on doesn't seem to have sound capability, so I made an .exe and ran it on a different computer.

When it played ding.wav instead of gong.wav, I made another .exe with a different .wav file specified, and that .exe still played ding.wav.

Any ideas?
0
 
LVL 27

Expert Comment

by:Ark
ID: 6357903
Seems "ding" means "Error", not file to play. Try mciSendString instead. Copy/Paste my code above (change Public in first declare to Private).

Cheers
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:2001gig
ID: 6358061
Ark, I tried your code, it runs, but no file plays.

I'm calling PlaySoundFile from the  Clock_Timer() subroutine.  I'm counting down to
zero on a timer.  When the timer reaches zero, the file plays:

Private Sub Clock_Timer()
  ' If we haven't reached zero yet, update the display
   Else
     Clock.Enabled = False
      PlaySoundFile ("g:\gong.wav")
   End If
End Sub

Is there a way to step through my code to determine where the problem develops?

BTW, I know the Else section is being reached because before, I used to have the background flash different colors when zero was reached.
0
 
LVL 27

Expert Comment

by:Ark
ID: 6358084
Oops, sorry, typo here:

 mciSendString "open " & sFileType & "!" & ShortFileName(sFilePath) & " alias sound1", 0&, 0, 0

Change sFilePath to sFileName

Cheers
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6359065
OKEY OKEY,

just use this tried and tested a small

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


Variable = sndPlaySound(Location, 1)
Variable = sndPlaySound("C:\WINDOWS\MEDIA\DING.WAV", 1)
End Sub
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6359071
cut and paste the the above code,

it works straight away on mine.

if you want to play a wav then change the path where it says C:\WINDOWS\MEDIA\DING.WAV" change that to where your WAV file is on your hard drive.

hope this helps

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


Variable = sndPlaySound(Location, 1)
Variable = sndPlaySound("C:\WINDOWS\MEDIA\DING.WAV", 1)
End Sub


andy
0
 

Author Comment

by:2001gig
ID: 6359815
Ark, I did change sFilePath to sFileName.....  I happen to know someone who puts little things like that in code to keep people on their toes  :-)  

andysalih, I'm trying your suggestion.... back later.  
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6359854
2001gig, just cut and paste the code it works fine for me, dont forget to put the path valid to where your wave files are.

cheers
Andy
0
 
LVL 2

Expert Comment

by:dmarco
ID: 6360738
'ding' sound is the default windows sound played by sndPlaySound when no file is found.
It seems a problem of path/filename...check it.
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6360754
dmarco,
he's currently trying my code, ive told him about the path,

my code will work if he cuts and pastes it without a shadow of douwt.

cheers
Andy
0
 

Author Comment

by:2001gig
ID: 6361027
It's working now!  The sound quality is terrible, but that's another question.......

Here's what the problem was:
My laptop (with no sound capability) is connected to a computer in another room(that computer doesn't have VB installed) via a Windows network, a local intranet.  So I had to create an exe, then walk into the other room to run the .exe.  The problem was that the G drive on the laptop is the E drive in the other room.  That's why the wav file was not found.  When I changed G:\ to E:\, I heard the wav file (in the other room).

One last detail:  I thought that an exe is self-contained, like, the wave file would somehow be included in the exe so when I went to the other room the exe would not have to search for the file again.  Obviously that is not true.  So my question is, how do I created a completely self-contained executable in VB that someone else  could run on their machine?
0
 

Author Comment

by:2001gig
ID: 6361030
It's working now!  The sound quality is terrible, but that's another question.......

Here's what the problem was:
My laptop (with no sound capability) is connected to a computer in another room(that computer doesn't have VB installed) via a Windows network, a local intranet.  So I had to create an exe, then walk into the other room to run the .exe.  The problem was that the G drive on the laptop is the E drive in the other room.  That's why the wav file was not found.  When I changed G:\ to E:\, I heard the wav file (in the other room).

One last detail:  I thought that an exe is self-contained, like, the wave file would somehow be included in the exe so when I went to the other room the exe would not have to search for the file again.  Obviously that is not true.  So my question is, how do I created a completely self-contained executable in VB that someone else  could run on their machine?
0
 

Author Comment

by:2001gig
ID: 6361041
Also, Ark, thanks for the link to more code -- something I definitely need......
0
 

Author Comment

by:2001gig
ID: 6361433
Also, Ark, thanks for the link to more code -- something I definitely need......
0
 

Author Comment

by:2001gig
ID: 6361843
Also, Ark, thanks for the link to more code -- something I definitely need......
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now