home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / directplay / simplevoice / frmapp.frm (.txt) next >
Encoding:
Visual Basic Form  |  2000-10-11  |  15.2 KB  |  332 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form frmApp 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Simple Voice"
  6.    ClientHeight    =   3465
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4755
  10.    Icon            =   "frmApp.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   3465
  15.    ScaleWidth      =   4755
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin MSComctlLib.ListView lvMembers 
  18.       Height          =   3075
  19.       Left            =   120
  20.       TabIndex        =   0
  21.       Top             =   300
  22.       Width           =   4575
  23.       _ExtentX        =   8070
  24.       _ExtentY        =   5424
  25.       View            =   3
  26.       LabelEdit       =   1
  27.       LabelWrap       =   -1  'True
  28.       HideSelection   =   -1  'True
  29.       _Version        =   393217
  30.       ForeColor       =   -2147483640
  31.       BackColor       =   -2147483643
  32.       BorderStyle     =   1
  33.       Appearance      =   1
  34.       NumItems        =   2
  35.       BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  36.          Text            =   "Name"
  37.          Object.Width           =   2540
  38.       EndProperty
  39.       BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  40.          SubItemIndex    =   1
  41.          Text            =   "Status"
  42.          Object.Width           =   2469
  43.       EndProperty
  44.    End
  45.    Begin VB.Label lblInfo 
  46.       BackStyle       =   0  'Transparent
  47.       Caption         =   "Members of this conversation:"
  48.       Height          =   255
  49.       Left            =   180
  50.       TabIndex        =   1
  51.       Top             =   60
  52.       Width           =   3855
  53.    End
  54. Attribute VB_Name = "frmApp"
  55. Attribute VB_GlobalNameSpace = False
  56. Attribute VB_Creatable = False
  57. Attribute VB_PredeclaredId = True
  58. Attribute VB_Exposed = False
  59. Option Explicit
  60. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  61. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  62. '  File:       frmApp.frm
  63. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  64. Implements DirectPlay8Event
  65. Implements DirectPlayVoiceEvent8
  66. Private Sub Form_Load()
  67.     'Init our vars
  68.     InitDPlay
  69.     'Now we can create a new Connection Form (which will also be our message pump)
  70.     Set DPlayEventsForm = New DPlayConnect
  71.     'First lets get the dplay connection started
  72.     If Not DPlayEventsForm.StartConnectWizard(dx, dpp, AppGuid, 10, Me) Then
  73.         Unload Me
  74.     End If
  75.     'Am I the host?
  76.     fAmHost = DPlayEventsForm.IsHost
  77.     'First let's set up the DirectPlayVoice stuff since that's the point of this demo
  78.     If fAmHost Then
  79.         'After we've created the session and let's start
  80.         'the DplayVoice server
  81.         Dim oSession As DVSESSIONDESC
  82.         
  83.         'Create our DPlayVoice Server
  84.         Set dvServer = dx.DirectPlayVoiceServerCreate
  85.             
  86.         'Set up the Session
  87.         oSession.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  88.         oSession.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  89.         oSession.lSessionType = DVSESSIONTYPE_PEER
  90.         oSession.guidCT = vbNullString
  91.         
  92.         'Init and start the session
  93.         dvServer.Initialize dpp, 0
  94.         dvServer.StartSession oSession, 0
  95.         Dim oSound As DVSOUNDDEVICECONFIG
  96.         Dim oClient As DVCLIENTCONFIG
  97.         'Now create a client as well (so we can both talk and listen)
  98.         Set dvClient = dx.DirectPlayVoiceClientCreate
  99.         'Now let's create a client event..
  100.         dvClient.StartClientNotification Me
  101.         dvClient.Initialize dpp, 0
  102.         'Set up our client and sound structs
  103.         oClient.lFlags = DVCLIENTCONFIG_AUTOVOICEACTIVATED Or DVCLIENTCONFIG_AUTORECORDVOLUME
  104.         oClient.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  105.         oClient.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  106.         oClient.lNotifyPeriod = 0
  107.         oClient.lThreshold = DVTHRESHOLD_UNUSED
  108.         oClient.lPlaybackVolume = DVPLAYBACKVOLUME_DEFAULT
  109.         oSound.hwndAppWindow = Me.hwnd
  110.         
  111.         On Error Resume Next
  112.         'Connect the client
  113.         dvClient.Connect oSound, oClient, 0
  114.         If Err.Number = DVERR_RUN_SETUP Then    'The audio tests have not been run on this
  115.                                                 'machine.  Run them now.
  116.             'we need to run setup first
  117.             Dim dvSetup As DirectPlayVoiceTest8
  118.             
  119.             Set dvSetup = dx.DirectPlayVoiceTestCreate
  120.             dvSetup.CheckAudioSetup vbNullString, vbNullString, Me.hwnd, 0 'Check the default devices since that's what we'll be using
  121.             Set dvSetup = Nothing
  122.             dvClient.Connect oSound, oClient, 0
  123.         ElseIf Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  124.             MsgBox "Could not start DirectPlayVoice.  This sample must exit." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbCritical, "Exiting"
  125.             Cleanup
  126.             Unload Me
  127.             End
  128.         End If
  129.     End If
  130. End Sub
  131. Private Sub UpdateList(ByVal lPlayerID As Long, fTalking As Boolean)
  132.     Dim lCount As Long
  133.     For lCount = lvMembers.ListItems.Count To 1 Step -1
  134.         If lvMembers.ListItems.Item(lCount).Key = "K" & CStr(lPlayerID) Then
  135.             'Change this guys status
  136.             If fTalking Then
  137.                 lvMembers.ListItems.Item(lCount).SubItems(1) = "Talking"
  138.             Else
  139.                 lvMembers.ListItems.Item(lCount).SubItems(1) = "Silent"
  140.             End If
  141.         End If
  142.     Next
  143. End Sub
  144. Private Sub Form_Unload(Cancel As Integer)
  145.     Me.Hide
  146.     DPlayEventsForm.DoSleep 50
  147.     Cleanup
  148. End Sub
  149. Public Sub UpdatePlayerList()
  150.     'Get everyone who is currently in the session and add them if we don't have them currently.
  151.     Dim lCount As Long
  152.     Dim Player As DPN_PLAYER_INFO
  153.     ' Enumerate players
  154.     For lCount = 1 To dpp.GetCountPlayersAndGroups(DPNENUM_PLAYERS)
  155.         If Not (AmIInList(dpp.GetPlayerOrGroup(lCount))) Then 'Add this player
  156.             Dim lItem As ListItem, sName As String
  157.             Player = dpp.GetPeerInfo(dpp.GetPlayerOrGroup(lCount))
  158.             sName = Player.Name
  159.             If sName = vbNullString Then sName = "Unknown"
  160.             If (Player.lPlayerFlags And DPNPLAYER_LOCAL = DPNPLAYER_LOCAL) Then glMyID = dpp.GetPlayerOrGroup(lCount)
  161.             Set lItem = lvMembers.ListItems.Add(, "K" & CStr(dpp.GetPlayerOrGroup(lCount)), sName)
  162.             lItem.SubItems(1) = "Silent"
  163.         End If
  164.     Next lCount
  165. End Sub
  166. Private Function AmIInList(ByVal lPlayerID As Long) As Boolean
  167.     Dim lCount As Long, fInThis As Boolean
  168.     For lCount = lvMembers.ListItems.Count To 1 Step -1
  169.         If lvMembers.ListItems.Item(lCount).Key = "K" & CStr(lPlayerID) Then
  170.             fInThis = True
  171.         End If
  172.     Next
  173.     AmIInList = fInThis
  174. End Function
  175. Private Sub RemovePlayer(ByVal lPlayerID As Long)
  176.     Dim lCount As Long
  177.     For lCount = lvMembers.ListItems.Count To 1 Step -1
  178.         If lvMembers.ListItems.Item(lCount).Key = "K" & CStr(lPlayerID) Then
  179.             lvMembers.ListItems.Remove lCount
  180.         End If
  181.     Next
  182. End Sub
  183. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  184.     'VB requires that we must implement *every* member of this interface
  185. End Sub
  186. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  187.     'VB requires that we must implement *every* member of this interface
  188. End Sub
  189. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  190.     'VB requires that we must implement *every* member of this interface
  191. End Sub
  192. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  193.     'Now we're connected, start our voice session
  194.     Dim oSound As DVSOUNDDEVICECONFIG
  195.     Dim oClient As DVCLIENTCONFIG
  196.     If dpnotify.hResultCode <> 0 Then
  197.         'For some reason we could not connect.  All available slots must be closed.
  198.         MsgBox "Connect Failed.  Error: 0x" & CStr(Hex$(dpnotify.hResultCode)) & "  - This sample will now close.", vbOKOnly Or vbCritical, "Closing"
  199.         DPlayEventsForm.CloseForm Me
  200.     Else
  201.         'Now create a client as well (so we can both talk and listen)
  202.         Set dvClient = dx.DirectPlayVoiceClientCreate
  203.         
  204.         'Now let's create a client event..
  205.         dvClient.StartClientNotification Me
  206.         
  207.         dvClient.Initialize dpp, 0
  208.         'Set up our client and sound structs
  209.         oClient.lFlags = DVCLIENTCONFIG_AUTOVOICEACTIVATED Or DVCLIENTCONFIG_AUTORECORDVOLUME
  210.         oClient.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  211.         oClient.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  212.         oClient.lNotifyPeriod = 0
  213.         oClient.lThreshold = DVTHRESHOLD_UNUSED
  214.         oClient.lPlaybackVolume = DVPLAYBACKVOLUME_DEFAULT
  215.         oSound.hwndAppWindow = Me.hwnd
  216.         
  217.         On Error Resume Next
  218.         'Connect the client
  219.         dvClient.Connect oSound, oClient, 0
  220.         If Err.Number = DVERR_RUN_SETUP Then    'The audio tests have not been run on this
  221.                                                 'machine.  Run them now.
  222.             'we need to run setup first
  223.             Dim dvSetup As DirectPlayVoiceTest8
  224.             
  225.             Set dvSetup = dx.DirectPlayVoiceTestCreate
  226.             dvSetup.CheckAudioSetup vbNullString, vbNullString, Me.hwnd, 0 'Check the default devices since that's what we'll be using
  227.             Set dvSetup = Nothing
  228.             dvClient.Connect oSound, oClient, 0
  229.         ElseIf Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  230.             MsgBox "Could not start DirectPlayVoice.  This sample must exit." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbCritical, "Exiting"
  231.             DPlayEventsForm.CloseForm Me
  232.             Exit Sub
  233.         End If
  234.     End If
  235. End Sub
  236. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  237.     'VB requires that we must implement *every* member of this interface
  238. End Sub
  239. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  240.     'VB requires that we must implement *every* member of this interface
  241. End Sub
  242. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  243.     'VB requires that we must implement *every* member of this interface
  244. End Sub
  245. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  246.     'VB requires that we must implement *every* member of this interface
  247. End Sub
  248. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  249.     'VB requires that we must implement *every* member of this interface
  250. End Sub
  251. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  252.     'VB requires that we must implement *every* member of this interface
  253. End Sub
  254. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  255.     'VB requires that we must implement *every* member of this interface
  256. End Sub
  257. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  258.     'VB requires that we must implement *every* member of this interface
  259. End Sub
  260. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  261.     'VB requires that we must implement *every* member of this interface
  262. End Sub
  263. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  264.     'VB requires that we must implement *every* member of this interface
  265. End Sub
  266. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  267.     'VB requires that we must implement *every* member of this interface
  268. End Sub
  269. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  270.     'VB requires that we must implement *every* member of this interface
  271. End Sub
  272. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  273.     'VB requires that we must implement *every* member of this interface
  274. End Sub
  275. Private Sub DirectPlayVoiceEvent8_ConnectResult(ByVal ResultCode As Long)
  276.     Dim lTargets(0) As Long
  277.     If ResultCode = 0 Then
  278.         lTargets(0) = DVID_ALLPLAYERS
  279.         dvClient.SetTransmitTargets lTargets, 0
  280.         
  281.         'Update the list
  282.         UpdatePlayerList
  283.     Else
  284.         MsgBox "Could not start DirectPlayVoice.  This sample must exit." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbCritical, "Exiting"
  285.         DPlayEventsForm.CloseForm Me
  286.     End If
  287. End Sub
  288. Private Sub DirectPlayVoiceEvent8_CreateVoicePlayer(ByVal playerID As Long, ByVal flags As Long)
  289.     'Someone joined, update the player list
  290.     UpdatePlayerList
  291. End Sub
  292. Private Sub DirectPlayVoiceEvent8_DeleteVoicePlayer(ByVal playerID As Long)
  293.     'Someone quit, remove them from the session
  294.     RemovePlayer playerID
  295. End Sub
  296. Private Sub DirectPlayVoiceEvent8_DisconnectResult(ByVal ResultCode As Long)
  297.     'VB requires that we must implement *every* member of this interface
  298. End Sub
  299. Private Sub DirectPlayVoiceEvent8_HostMigrated(ByVal NewHostID As Long, ByVal NewServer As DxVBLibA.DirectPlayVoiceServer8)
  300.     'VB requires that we must implement *every* member of this interface
  301. End Sub
  302. Private Sub DirectPlayVoiceEvent8_InputLevel(ByVal PeakLevel As Long, ByVal RecordVolume As Long)
  303.     'VB requires that we must implement *every* member of this interface
  304. End Sub
  305. Private Sub DirectPlayVoiceEvent8_OutputLevel(ByVal PeakLevel As Long, ByVal OutputVolume As Long)
  306.     'VB requires that we must implement *every* member of this interface
  307. End Sub
  308. Private Sub DirectPlayVoiceEvent8_PlayerOutputLevel(ByVal playerID As Long, ByVal PeakLevel As Long)
  309.     'VB requires that we must implement *every* member of this interface
  310. End Sub
  311. Private Sub DirectPlayVoiceEvent8_PlayerVoiceStart(ByVal playerID As Long)
  312.     'Someone is talking, update the list
  313.     UpdateList playerID, True
  314. End Sub
  315. Private Sub DirectPlayVoiceEvent8_PlayerVoiceStop(ByVal playerID As Long)
  316.     'Someone stopped talking, update the list
  317.     UpdateList playerID, False
  318. End Sub
  319. Private Sub DirectPlayVoiceEvent8_RecordStart(ByVal PeakVolume As Long)
  320.     'I am talking, update the list
  321.     UpdateList glMyID, True
  322. End Sub
  323. Private Sub DirectPlayVoiceEvent8_RecordStop(ByVal PeakVolume As Long)
  324.     'I have quit talking, update the list
  325.     UpdateList glMyID, False
  326. End Sub
  327. Private Sub DirectPlayVoiceEvent8_SessionLost(ByVal ResultCode As Long)
  328.     'The voice session has exited, let's quit
  329.     MsgBox "The DirectPlayVoice session was lost.  This sample is exiting.", vbOKOnly Or vbInformation, "Session lost."
  330.     DPlayEventsForm.CloseForm Me
  331. End Sub
  332.