Exchange Forum > ActiveX and VBA
Playing Sounds in AutoCAD
Page 1 of 2
1 2
Playing Sounds in AutoCAD

#1

dOrsborn
Join Date:
05-10-2008
We have a long script that we run on a completed folder of project drawings. When the script is completed it would be nice to have AutoCAD play a sound or a music file. We've looked into AutoCAD help but don't know what to search for. Any help would be appreciated.
Playing Sounds in AutoCAD

#2

X-Calibur
Join Date:
12-04-2007
You can use the VBA beep sound using one of these commands. I think the second example is safer in case there's a problem with the commands issued.
(command "vbastmt" "beep")
(vl-cmdf "vbastmt" "beep")
Playing Sounds in AutoCAD

#3

Hayes
Join Date:
07-28-2007
Have you tried using the Windows Mplay32.exe with the startapp function? It's fairly easy to use. The only drawback is that it displays a small Mplay32 dialog while the wav file is being played. If this is not obtrusive in your script here is an example. Make sure the wav-file path and filename exist on your computer terminals.
Code:
(startapp "mplay32.exe /play /close C:\\Windows\\Media\\Notify.wav")
Playing Sounds in AutoCAD

#4

Dante
Join Date:
08-13-2007
VBA has some built in modules for playing wav files. Here's one example that you can play around with. I'm not sure about all the options for the Module and Flags parameters, but you can experiment with those as well. You can change the wav file path and filename to one that you want to test out.
Dante
 
Code:
Private Declare Function PlaySound Lib "Winmm.dll" _
(ByVal SoundName As String, ByVal Module As Long, ByVal Flags As Long) As Long
Sub Play()
  'Change the wav sound file to one that you prefer.
  PlaySound "C:\Program Files\NetMeeting\TestSnd.wav", 0, &H20000
End Sub
Playing Sounds in AutoCAD

#5

Nugent
Join Date:
10-05-2007
Dante,
Why not have the VBA code driven by AutoLISP similar to the thread on VBA code with AutoLISP? This way someone could easily try a new wav sound from the command line without knowing any VBA. They could also easily use it in a script. Here’s my version of PlayWav.
Code:
Example for testing on the command line:
(PlayWav "C:\\Windows\\Media\\tada.wav")
 
Example for writing to a script file:
(write-line "(PlayWav \"C:\\\\Windows\\\\Media\\\\tada.wav\")" ScriptFile)
AutoLISP code
Code:
(defun PlayWav (WavFilename / UserS5)
  (if (and (findfile WavFilename)(= (strcase (vl-filename-extension WavFilename) t) ".wav"))
    (progn
      (setq UserS5 (getvar "USERS5"))
      (setvar "USERS5" WavFilename)
      (command "vbaload" "PlayWav.dvb")
      (command "-vbarun" "thisdrawing.PlayWav")
      (command "vbaunload" "PlayWav.dvb")
      (setvar "USERS5" UserS5)
    )
    (princ (strcat "\n" WavFilename " file not found or is not a wav file."))
  )
  (princ)
)
VBA code
Code:
Private Declare Function PlaySound Lib "Winmm.dll" _
(ByVal SoundName As String, ByVal Module As Long, ByVal Flags As Long) As Long
Sub PlayWav()
  Dim WavFilename As String
  WavFilename = ThisDrawing.GetVariable("USERS5")
  PlaySound WavFilename, 0, &H20000
End Sub

Attachments  PlayWav.lsp    PlayWav.dvb  
Playing Sounds in AutoCAD

#6

dOrsborn
Join Date:
05-10-2008
The PlayWav function is alright! Is there a way to have it play in the background?
Thanks for all your help.
Playing Sounds in AutoCAD

#7

Adesu
Join Date:
10-03-2007
Hi dOrsborn,
Here my code to play a music
Code:
; pwva is stand for Play Winamp Via Autolisp
;        Design by  : Adesu <Ade Suharna>
;        Email      : mteybid@yuasabattery.co.id
;        Homepage   : http://www.yuasa-battery.co.id
;        Create     : 07 February 2007
;        Program no.: 0527/02/2007
;        Edit by    : Adesu  08/02/2007   1).
;                            19/02/2007   2).
(defun c:pwva (/ file file_name fn lst opt opt1 opt2 winamp)
  (setq file "D:/YBI/General/Music/")
  (setq opt
  (strcase
    (getstring "\nSelect your music favourite[Dangdut,Instrument,Keroncong,Pop,West]<D>: "))) ;1).
  (cond ((or (= opt "")(= opt "D"))
  (setq opt2
  (strcase
    (getstring "\nSelect your music Dangdut favourite[Campuran,Ike Nurjanah,Rhoma,Uut permatasari]<C>: "))) ; 2).
  (cond
    ((or (= opt2 "")(= opt2 "C"))(setq file "D:/YBI/General/Music/Dangdut/Campuran/"))
    ((= opt2 "I")(setq file "D:/YBI/General/Music/Dangdut/Ike Nurjanah/"))
    ((= opt2 "R")(setq file "D:/YBI/General/Music/Dangdut/Rhoma/"))
    ((= opt2 "U")(setq file "D:/YBI/General/Music/Dangdut/Uut Permatasari/"))
    )              ; cond
  )
  ((= opt "I")(setq file "D:/YBI/General/Music/Instrument/"))
 ((= opt "K")(setq file "D:/YBI/General/Music/Keroncong/"))
 ((= opt "P")
  (setq opt1
  (strcase
    (getstring "\nSelect your music Pop favourite[Dlloyd,Panbers]<P>: ")))
  (cond
    ((or (= opt1 "")(= opt1 "P"))(setq file "D:/YBI/General/Music/Pop/Panbers/"))
    ((= opt1 "D")(setq file "D:/YBI/General/Music/Pop/Dlloyd/"))    
    )              ; cond
  )
 ((= opt "W")(setq file "D:/YBI/General/Music/West/Elvis Presley/"))
 )                 ; cond 1).
  (if
    (setq lst (cddr (vl-directory-files file)))
    (progn
      (foreach x lst
 (setq file_name (strcat file x))
 (setq fn (append fn (list file_name)))
 )
      (setq fn (vl-string-trim "()" (vl-princ-to-string fn)))
      (setq winamp "C:/Program Files/Winamp/winamp.exe")
      (startapp winamp fn)
      )                    ; progn
    (alert "\nInvalid,there is not selected file")
    )                      ; if
  (princ)
  )
 
Quote:
The PlayWav function is alright! Is there a way to have it play in the background?  
Thanks for all your help.
Playing Sounds in AutoCAD

#8

dOrsborn
Join Date:
05-10-2008
I don't seem to have the WinAmp program installed on my computer. When I searched on the internet I found a few websites to download the program, but I also came across a few websites to fix problems with WinAmp.exe. Have you found it safe to use on your computer, and have they fixed these problems that these websites mentioned?
 
Your program is specific to your computer's file structure. If I understand the syntax you are using, I just need to use something like the following to play my music file. Is this correct?
 
(startapp "C:/Program Files/Winamp/winamp.exe" "C:/Music/Song1.wav")
 
Also, what music file extension types does it play?
Thanks for your help.
dOrsborn
 
P.S. I forgot to ask, does it play in the background so the AutoLISP routine can finish, without waiting for the song to finish?
Playing Sounds in AutoCAD

#9

Addison
Join Date:
07-25-2007
I just downloaded WimAmp from the link below. Everything seems to be working and playing fine. In the download screens I always check that I'm not interested in emails about other related products etc. It will play just about any sound file including avi, mid, midi, mp3, wma, wav and many more types. And the best thing, is that it's free!
 
http://www.winamp.com/
Playing Sounds in AutoCAD

#10

Nugent
Join Date:
10-05-2007
I've been doing some research after testing somethings in PlayWav and came up with this new version named PlaySound. Unlike PlayWav this version plays in the background. It can play several types of sound files. However, I have only tested wav, mp3, mid, cda, and wma sound files so far. You can add other extensions to test out, by adding them to the lisp file. The StopSound function is useful for stopping one song before playing the next one.
Nugent
 
AutoLISP code
Code:
(defun PlaySound (SoundFilename / Ext UserS5)
  (if (setq Ext (vl-filename-extension SoundFilename))
    (setq Ext (strcase Ext t))
  )
  (if (and (findfile SoundFilename)(member Ext (list ".wav" ".mp3" ".mid" ".cda" ".wma")))
    (progn
      (setq UserS5 (getvar "USERS5"))
      (setvar "USERS5" SoundFilename)
      (command "vbaload" "PlaySound.dvb")
      (command "-vbarun" "thisdrawing.PlaySound")
      (command "vbaunload" "PlaySound.dvb")
      (setvar "USERS5" UserS5)
    )
    (princ (strcat "\n" SoundFilename " file not found or is not a valid sound file."))
  )
  (princ)
)
(defun StopSound (SoundFilename / Ext UserS5)
  (if (setq Ext (vl-filename-extension SoundFilename))
    (setq Ext (strcase Ext t))
  )
  (if (and (findfile SoundFilename)(member Ext (list ".wav" ".mp3" ".mid" ".cda" ".wma")))
    (progn
      (setq UserS5 (getvar "USERS5"))
      (setvar "USERS5" SoundFilename)
      (command "vbaload" "PlaySound.dvb")
      (command "-vbarun" "thisdrawing.StopSound")
      (command "vbaunload" "PlaySound.dvb")
      (setvar "USERS5" UserS5)
    )
    (princ (strcat "\n" SoundFilename " file not found or is not a valid sound file."))
  )
  (princ)
)
VBA code
Code:
Private Declare Function PlaySounds Lib "Winmm.dll" Alias "mciSendStringA" _
(ByVal strCommand As String, ByVal ReturnString As String, _
ByVal ReturnLength As Long, ByVal CallBack As Long) As Long
Function PlaySound()
  Dim Filename As String
  Dim lngReturn As Long
  Dim str256 As String
  str256 = String$(256, 0)
  Filename = ThisDrawing.GetVariable("USERS5")
  lngReturn = PlaySounds("Play " & Filename, str256, 255, 0)
End Function
Function StopSound()
  Dim Filename As String
  Dim lngReturn As Long
  Dim str256 As String
  str256 = String$(256, 0)
  Filename = ThisDrawing.GetVariable("USERS5")
  lngReturn = PlaySounds("Stop " & Filename, str256, 255, 0)
End Function

Attachments  PlaySound.dvb  
Page 1 of 2
1 2