home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter20 / SoundTest / Form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  2004-11-12  |  2.8 KB  |  88 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   4065
  6.    ClientLeft      =   45
  7.    ClientTop       =   420
  8.    ClientWidth     =   5175
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   271
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   345
  15.    StartUpPosition =   2  'CenterScreen
  16. Attribute VB_Name = "Form1"
  17. Attribute VB_GlobalNameSpace = False
  18. Attribute VB_Creatable = False
  19. Attribute VB_PredeclaredId = True
  20. Attribute VB_Exposed = False
  21. '----------------------------------------------------------------------
  22. ' Visual Basic Game Programming With DirectX
  23. ' SoundTest Program
  24. '----------------------------------------------------------------------
  25. Option Explicit
  26. Option Base 0
  27. 'program variables
  28. Dim dx As DirectX8
  29. Dim ds As DirectSound8
  30. Dim Sound1 As DirectSoundSecondaryBuffer8
  31. Private Sub Form_Load()
  32.     'create the DirectX8 object
  33.     Set dx = New DirectX8
  34.     'create the DirectSound8 object
  35.     Set ds = dx.DirectSoundCreate("")
  36.     If Err.Number <> 0 Then
  37.         MsgBox "Error creating DirectSound object"
  38.         Shutdown
  39.     End If
  40.     'set the priority level for DirectSound
  41.     ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY
  42.     'load the wave files
  43.     Set Sound1 = LoadSound(App.Path & "\halleluja.wav")
  44.     'play the halleleuia sound
  45.     PlaySound Sound1, False, False
  46. End Sub
  47. Public Function LoadSound(ByVal sFilename As String) _
  48.     As DirectSoundSecondaryBuffer8
  49.     Dim dsBuf As DSBUFFERDESC
  50.     'set up sound buffer for normal playback
  51.     dsBuf.lFlags = DSBCAPS_STATIC
  52.     'load wave file into DirectSound buffer
  53.     Set LoadSound = ds.CreateSoundBufferFromFile(sFilename, dsBuf)
  54.     If Err.Number <> 0 Then
  55.         MsgBox "Error loading sound file: " & sFilename
  56.         Shutdown
  57.     End If
  58. End Function
  59. Public Sub PlaySound(ByRef Sound As DirectSoundSecondaryBuffer8, _
  60.     ByVal bCloseFirst As Boolean, ByVal bLoopSound As Boolean)
  61.     'stop currently playing waves?
  62.     If bCloseFirst Then
  63.         Sound.Stop
  64.         Sound.SetCurrentPosition 0
  65.     End If
  66.     'loop the sound?
  67.     If bLoopSound Then
  68.         Sound.Play DSBPLAY_LOOPING
  69.     Else
  70.         Sound.Play DSBPLAY_DEFAULT
  71.     End If
  72. End Sub
  73. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  74.     If KeyCode = 27 Then Shutdown
  75. End Sub
  76. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  77.     Shutdown
  78. End Sub
  79. Private Sub Shutdown()
  80.     'stop sound playback
  81.     Sound1.Stop
  82.     'delete DirectX Audio objects
  83.     Set dx = Nothing
  84.     Set ds = Nothing
  85.     Set Sound1 = Nothing
  86.     End
  87. End Sub
  88.