iOS Reference Library Apple Developer
Search

Adding Voice Chat

Voice chat is implemented on top of a network connection provided by your application. The following example uses a GKSession object to provide a network to the client. For more information on GKSession objects, see “Peer to Peer Connectivity.” To implement voice chat, perform the following steps:

  1. Configure the audio session to allow playback and recording.

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:myErr];
    [audioSession setActive: YES error: myErr];
  2. Implement the client’s participantID method.

    - (NSString *)participantID
    {
        return session.peerID;
    }

    The participant identifier is a string that uniquely identifies the client. As a session’s peerID string already uniquely identifies the peer, the client reuses it as the participant identifier.

  3. Implement the client’s voiceChatService:sendData:toParticipantID: method.

    - (void)voiceChatService:(GKVoiceChatService *)voiceChatService sendData:(NSData *)data toParticipantID:(NSString *)participantID
    {
        [session sendData: data toPeers:[NSArray arrayWithObject: participantID] withDataMode: GKSendDataReliable error: nil];
    }

    The service calls the client when it needs to send data to other participants in the chat. Most commonly, it does this to establish its own real-time connection with other participants. As both the GKSession and GKVoiceChatService use an NSData object to hold their data, simply pass it on to the session.

    If the same network is being used to transmit your own information, you may need to add an identifier before the packet to differentiate your data from voice chat data.

  4. Implement the session’s receive handler to forward data to the voice chat service.

    - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context;
    {
        [[GKVoiceChatService defaultVoiceChatService] receivedData:data fromParticipantID:peer];
    }

    This function mirrors the client’s voiceChatService:sendData:toParticipantID: method, forwarding the data received from the session to the voice chat service.

  5. Attach the client to the voice chat service.

    MyChatClient *myClient = [[MyChatClient alloc] initWithSession: session];
    [GKVoiceChatService defaultVoiceChatService].client = myClient;
  6. Connect to the other participant.

    [[GKVoiceChatService defaultVoiceChatService] startVoiceChatWithParticipantID: otherPeer error: nil];

    Your application may want to do this automatically as part of the connection process, or offer the user an opportunity to create a voice chat separately. An appropriate place to automatically create a voice chat would be in the session delegate’s session:peer:didChangeState: method.

  7. Implement optional client methods.

    If your application doesn’t rely on the network connection to validate the other user, you may need to implement additional methods of the GKVoiceChatClient protocol. The GKVoiceChatClient protocol offers many methods that allow your client to be notified as other participants attempt to connect or otherwise change state.




Last updated: 2009-05-28

Did this document help you? Yes It's good, but... Not helpful...